Timer Difficulties

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

I am trying to make an enemy spawn whenever my timer reaches zero, but it isn’t working. I am pretty sure that it isn’t a problem with the variables, since I used add_child(slime) in _ready() and it works just fine. I’m very sure it is a problem with the timer. AutoStart is on and One Shot is off. Any suggestions? Here is my code:

extends Node2D

const Slime = preload("res://slime.tscn")
onready var slime = Slime.instance()

func _init():
	randomize()

func _ready():
	add_child(slime)

func _on_SlimeSpawnTimer_timeout():
	add_child(slime)
:bust_in_silhouette: Reply From: 2D

With timers you must make sure you connect the signals to the nodes where you want the functions called. If you made the timer in the editor, you will need to do this under the sub tab “node (signals)”. If you did it in code, you will need to connect the signal.

I recommend doing something in your code above as below. You want to instance the slime in your timeout function. Or have an object pool of slimes where they are all instanced at the beginning of the scene and then they are woken up and placed in the timeout function.

extends Node2D

const slime_scene = preload("res://slime.tscn")
var slime

func _ready():
    pass

func _on_SlimeSpawnTimer_timeout():
    print("Timer is up!")
    slime = slime_scene.instance()
    add_child(slime)