how to make spawner in my 2d game

Godot Version

4.3

Question

I want to create a spawner in which from random place is created a random enemy


can someone help me, please?

1 Like

instance() is changed to instantiate() in Godot 4. After add_child(e), you have to set the position of the instance. To pick a random position, use
e.position = Vector2(randf_range(MinXPos, MaxXPos), randf_range(MinYPos, MaxYPos))
The above code may differ based on your use case, modify it if you want to spawn it with respect to some node.

func _on_timer_timeout() -> void:
    # Spawn a random enemy
    var enemy = enemy_list.pick_random().instantiate()
    add_child(enemy)

    # Set enemy position at the position of a random marker
    var marker = point_list.pick_random()
    enemy.position = marker.position

thanks you a lot!