Search code examples
godotgdscript

Godot Script does not delete child nodes as expected


I am creating a small space shooter game with asteroids set up as Rigid Bodies. They just fall past the bottom of the screen and are then meant to be deleted and that's the part I am having trouble with. I have a print function to let me know when they are deleted that will say "Object Terminated". I have no errors when running it, but they do not get deleted when they fall past 560 on the Y-Axis.

I tried switching things around to see what would happen and I switched ">" to "<". This deleted them immediately since they are lower than 560 on the y-axis, so why does it not work the other way round when they are higher than 560 on the y-axis? Is it because the get_position function does not get an updated value for the position? If so how do I get that?

extends RigidBody2D

func _ready():
    var posY = get_position().y      #gets y-axis position
    randomize()
    add_torque(rand_range(20,60))

    if posY > 560:
        queue_free()
        print("Object Terminated")   #checks that they are removed

Solution

  • The _ready() function is only called once when the node is added to the scene. You should put the check on the Y position in the _process() function which is called every frame.