Sabir your approach is wrong. Checking whenever a node exits the ROOT and whenever a node is removed from the tree will lead to SERIOUS problems.
If "enemy" is a node that interacts with anything, it's going to have a script, or something is going to have a script that interacts with the node.
just put a function in any of the scripts that triggers when they are destroyed, before queue_free()
.
for keeping score, use an autoload. an autoload is a script you create in ProjectSettings, this script is global and you can access it's properties from any other script.
Ex:
Autoload named Stats:
var score : int = 0
Enemy:
func score_up() -> void:
Stats.score += 1
a better aproach is to use a function that adds to the score:
#autoload
var score : int = 0
func add_score(amount : int) -> void:
score += amount
#enemy destroyed
Stats.add_score(1)
enemy.queue_free()
that way you can trigger signals or changes whenever the variable has to be changed
then, in the label
, you get the score from the autoload:
text = "Points : " + str(Stats.score)
you can do this every frame:
func _process(_delta) -> void:
text = "Points : " + str(Stats.score)
or use a custom signal
emitted by Stats
whenever add_score
is triggered, but you would have to connect nodes to the signal in the autoload during ready.
to answer your question, you have the functions add_to_group
and is_in_group
to set and check the current group of a node.
https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-add-to-group
https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-is-in-group