I’m new to Godot and have been trying to fix this for a little bit. The following code is in my main node, and I have a move_enemy function in the enemy node. Whenever I run the code, the enemy node that’s already in the main scene moves fine, but the copied one doesn’t move or function at all. I tried using a signal instead of a function but that didn’t do anything. Is there some other step I need to do in between or is there something else that could be wrong?
Signal should work fine, you just need to connect it whenever you add an enemy, so that it receives that signal. That needs to be done in code if you add an enemy during runtime.
$enemy only returns the one child of the main node that is actually called “enemy”. Because sibling nodes can’t have the same name, Godot will automatically change the name of the node in a very unpleasant way to make it different.
(By the way, you can switch to this live view of the scene tree at runtime by changing from Local to Remote in the hierarchy)
Both Node2Ds in the test are instances of a scene “enemy.tscn” where the root node is called “Enemy”. However, the second node can’t be named the same as the first so it gets a different name.
The way to fix this would be to store an Array of all enemies that is updated when an enemy is instantiated and then loop through all enemies in _physics_process. For example
var enemies = []
func _ready() -> void:
var new_enemy = enemy_scene.instantiate()
enemies.push_back(new_enemy)
new_enemy.position.x = 600
new_enemy.position.y = 170
new_enemy.velocity.x = 10
add_child(new_enemy)
func _physics_process(delta: float) -> void:
for enemy in enemies:
enemy.move_enemy($player.position, delta)