#Why is my spawn_pillars function not working? [SOLVED]

3 messages · Page 1 of 1 (latest)

blissful ice
#

Hi all, I'm new to Godot and working on a little Flappy Bird clone. I'm wondering why this code isn't spawning another Pillar every 1s. I have a Timer, and my on_timer_timout function is a Signal.

Here is the code:


var pillars = preload("res://scenes/pillars.tscn")

func _ready():
    pass

func _process(delta):
    pass

func _on_timer_timeout():
    spawn_pillars()

func spawn_pillars():
    var p = pillars.instantiate()
    add_child(p)```

**EDIT:** I didn't have Autostart selected 🙂 I'm still curious to know if this is a good way to handle this.
#

Why is my spawn_pillars function not working? [SOLVED]

lavish plover
#

That's definitely a valid usecase for an instanced timer node.

If you prefer not having the timer node in the scene tree manually, you could also instantiate within the code:

extends Node2D

var pillars = preload("res://scenes/pillars.tscn")

func _ready():
    var timer: Timer = Timer.new()
    timer.timeout.connect(spawn_pillars)
    timer.wait_time = 1.0

    add_child(timer)
    timer.start()

func spawn_pillars():
    var p = pillars.instantiate()
    add_child(p)

This results in basically the same, but starts the timer manually instead of using autostart.
The timer gets created in code, appended to the scene tree as a child and then started with the start() function.