Menu
CoddyTech

Spawn on a Timer

Part of the Game Rules section of Coddy's Godot journey. Lesson 19 of 29.

To drop a block every second, combine a Timer with your spawn() function.

The scene has a Timer called SpawnTimer. It runs out every second and starts on its own. Connect it:

func _ready():
	$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)

func _on_spawn_timer_timeout():
	spawn(320)

Change SpawnTimer's wait_time to make the game faster or slower.

challenge icon

Challenge

Easy

Drop a block at x = 320 every second:

  1. In _ready(), connect $SpawnTimer.timeout to _on_spawn_timer_timeout.
  2. Write _on_spawn_timer_timeout(), which calls spawn(320).

Try it yourself

extends Node2D

var block_scene = preload("res://block.tscn")

func _ready():
	pass

func spawn(x):
	var block = block_scene.instantiate()
	block.position = Vector2(x, -30)
	add_child(block)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Rules

Practice on your own: Godot playground