Menu
CoddyTech

A Packed Scene

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

Games make new things while they run, like enemies or falling blocks. You build the thing once as its own scene, then copy it from code.

res://block.tscn is a red block that falls until it breaks on the grass. Making one takes three steps:

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

func _ready():
	var block = block_scene.instantiate()
	add_child(block)
  • preload() loads the scene file once, as a PackedScene. It is a recipe for a block.
  • instantiate() builds a new block from the recipe.
  • add_child() adds the block to the game as a child of this node. Until then, it is not drawn and does not run.
challenge icon

Challenge

Easy

When the game starts, drop one block:

  1. At the top of the script, preload res://block.tscn into a variable called block_scene.
  2. In _ready(), make a block with instantiate() and add it with add_child().

Try it yourself

extends Node2D

func _ready():
	pass
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