Topic was automatically imported from the old Question2Answer platform.
Asked By
javrocks
I instance the enemy scene using a timer, but since the enemy isn’t supposed to be in the main scene immediately i can’t put the signal function in the ready function. The signal emits when an enemy dies, but the signal isn’t connecting correctly and it’s only calling (reduce _num _of _enemy) once, when i want it to be called each time an enemy is removed from the scene.
var enemy = 0
var max_num_of_Enemy = 3
func _on_Enemy_timer_timeout():
var scene_instance = choose([Enemy_Scene.instance()])
add_child(scene_instance)
enemy = enemy + 1
if ships == max_num_of_Enemy:
$Enemy_timer.stop()
Enemy = get_tree().get_root().find_node("Enemy2D", true, false)
Enemy.connect("minus_Enemy", self, "reduce_num_of_enemy")
func reduce_num_of_enemy():
enmey = enemy - 1
I think it’d help to see a bit more of your code or try to figure out what you are needing the signal for. From this code it looks like the only reason you need a signal is to keep track of the number of active enemies in the scene.
If you just want to keep track of how many enemies are alive in your scene you might want to add the enemies to a group and call get_nodes_in_group like from the docs here:
and then you can use enemy_count to determine if you should spawn another enemy.
That said, maybe there’s a perfectly good reason your game needs to do it this way. But from looking at the code above it’s hard to say what it’s doing because there are a lot of variables either missing or out of context. For example, what is the choose method doing, etc.
I’m also curious, could you not connect the signal after you instance the node?
func _on_Enemy_timer_timeout():
var scene_instance = choose([Enemy_Scene.instance()])
add_child(scene_instance)
# Add connection here
scene_instance.connect("minus_Enemy", self, "reduce_num_of_enemy")
It is possible your find_node call is only returning either the same node, or the first node named Enemy2D. Instead of connecting all of your enemies when they are created.
And you were right. find_node() call was only returning the first node named Enemy2D. Instead of connecting all of my enemies when they are created. That was the problem