Can't control instantiated enemies

Godot Version

(Main scene)
func _on_enemy_spawn_timeout():
var spawn_x = rng.randf()
var spawn_y = rng.randi_range(0, 500)
var enemy = enemy_scene.instantiate()
if spawn_x > 0.5:
move_right.emit()
enemy.position = Vector2(0, spawn_y)
else:
move_left.emit()
enemy.position = Vector2(1400, spawn_y)
get_tree().current_scene.add_child(enemy)

(Enemy scene)
func _physics_process(delta):
if move_right == true:
position.x += 200 * delta
if move_left == true:
position.x -= 200 * delta

func _on_node_2d_move_left():
move_left = true
func _on_node_2d_move_right():
move_right = true

Question

I’m a total newbie so maybe this is stupid… I’m making a shmup where I want the enemy to move either left or right depending on which side of the screen they spawn on. I can’t seem to figure out how to control the enemies after they spawn. Can somebody give me some tips?

1 Like

Instead of using signals, I would just set the property when you spawn them.

E.g.

Enemy.move_left = true

1 Like

Would this be done in the enemy scene or in the main one?

Main scene. Line immediately after spawning them

1 Like

Omg thank you this makes so much sense. I didn’t know you could reference variables on another scene like that. Thank you so much

1 Like

Glad I could help.

In your case, the code that creates the enemy has a reference to it that can be used to manipulate it. E.g. the “enemy” variable.

If the code didnt have a reference to it, then you would have to find some other way to get a reference to it or signal to it. Such as signal, unique names, exports etc…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.