0 votes

i have a code to spawn a pillar to damage the player on the player's position
here is the code of the spawner
node2D:

extends Node2D

var child_position
var current_pillars = 0
var pillar = preload("res://plant pillar.tscn").instance()

func _on_Timer_timeout():
    child_position = get_node("Position2D").position.x
    $".".position.x = child_position
    add_child(pillar)
    current_pillars + 1

    position2D:
        extends Position2D
        var pillar = preload("res://plant pillar.tscn").instance()

        func _process(delta):
            position.x = get_parent().get_parent().get_node("player").position.x

and here is the code for the pillar:

extends Area2D
var damage = 30
func _ready():
    $Sprite.play('growing')
func _on_plant_pillar_body_entered(body):
    if body.is_in_group("players"):
        body.on_hit(damage)
func _on_Sprite_animation_finished():
    $Sprite.play('full_grown')
    $CollisionShape2D.disabled = false
    $Timer2.start()
func _on_Timer2_timeout():
    $".".queue_free()

the biggest problem is that after the pillar is spawned for the first time he does not goes true the animation and so is already spwaned fully grown and the collision is set to true,
thank you

asked Apr 22 in Projects by i.want.to.die (28 points)

1 Answer

+1 vote

You should get an error on every timeout after the first:

add_child: Can't add child [...] to [...], already has a parent [...]

If you want to add multiple pillars, you need to create multiple instances of it:

var pillar = preload("res://plant pillar.tscn")

func _on_Timer_timeout():
    # ...
    var new_pillar = pillar.instance()
    add_child(new_pillar)
    # ...

However, you're reusing the same instance over and over again!

answered Apr 22 by njamster (8,990 points)

i absolutly love this community , thank you very much

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.