0 votes

I'm making a tower defence game, this is a tower script:

var target = null
var can_shoot = true


func _process(delta):
    if not can_shoot:

    var creeps = get_tree().get_nodes_in_group("Creeps")

    if creeps.size() == 0:
    # version A: ------------------------------------------------
#   target = null
    target = creeps[0]

   # version B: ------------------------------------------------
#   if target == null or target.is_queued_for_deletion():
#       print("####### IF")
#       target = creeps[0]
#   else:
#       print("####### ELSE")

    # shoot logic, including target properties + can_shoot, timer update

target is reference to a creep scene inscance. All creeps belong to "Creeps" group. I delete a creep with queue_free().

I get different errors, when I use code block VERSION B, but it works fine with VERSION A. Sometimes it's "already deleted node" error, sometimes var target holds children of creep scene. It looks like .is_queued_for_deletion() is not working (). Why? Is it normal that after queue_free target holds reference to childen nodes? What is best practice here?

in Engine by (42 points)

Please log in or register to answer this question.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.