How do i make a Timed Spawner?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chasethegamer

hi, so im trying to have meteors spawn from a moving spawn point and ive tried to use this code to have it spawn

extends Position2D

var meteor = preload ("res://Sprites/Meteors/Meteor.tscn")
var spawn_rate = 0.7

func _ready():
	$Timer.wait_time = spawn_rate
	$AnimationPlayer.play("Movement")
	pass

func _process(delta):
	$Timer.autostart

func _on_Timer_timeout():
	var m = meteor.instance()
	add_child(m)
	pass

But nothing is spawning at all

:bust_in_silhouette: Reply From: kidscancode

Note: I edited your post to fix the code formatting. In future, please use the correct formatting (you can click the “Code Sample” button when editing your post.

Your problem is that you’re not starting your timer. After setting wait_time, you need:

$Timer.start()

Second, remove that code in _process(). autostart is a boolean property that toggles whether the timer should start automatically when it enters the tree. You’re not doing anything with that code.

Finally, remove all those pass lines. They do literally nothing.

To improve this code, you can set “Autostart” to true and “Wait Time” to `0.7’ in the Inspector, and set your animation to “Autoplay” in the AnimationPlayer and you won’t need any of this code but the timeout callback.