0 votes

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)
asked Aug 11 in Engine by ThreeSpark (32 points)

1 Answer

0 votes

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)
answered Aug 11 by 2D||!2D (279 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.