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
EasyDrop one block when the game starts.
- At the top of the script, preload
res://block.tscnintovar block_scene. - In
_ready(), make a block withinstantiate()and add it withadd_child().
Press Play: a block falls from the top left. Then Check.
Try it yourself
extends Node2D
func _ready():
pass
All lessons in Game Rules
Practice on your own: Godot playground