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 aPackedScene. 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
EasyWhen the game starts, drop one block:
- At the top of the script, preload
res://block.tscninto a variable calledblock_scene. - In
_ready(), make a block withinstantiate()and add it withadd_child().
Try it yourself
extends Node2D
func _ready():
pass
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