Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Editor and Scenes

Instancing Scenes in Godot: Reuse, Spawn and Edit

Learn how scene instancing works in Godot 4: PackedScene, instantiate(), editable children, inherited scenes, spawning bullets and enemies from code, and avoiding common mistakes.

Instancing is the feature that makes Godot's scene system practical. You build a thing once, such as a coin, an enemy or a button, save it as a scene, and then place as many copies as you like, in the editor or at runtime. Change the original and every copy updates. This lesson shows both ways to instance and what happens behind the scenes.

Godot code here is read-only; it needs the real editor. The Python stand-in models the idea of a blueprint that creates independent copies.

Scene as blueprint

When you save a scene, you are saving a blueprint. In code, loading a .tscn file gives you a PackedScene resource. Calling instantiate() on it builds a fresh copy of the whole node tree, with scripts attached and properties set. Each copy is independent: changing one coin's position does not move the others.

flowchart LR
    F["coin.tscn (file)"] --> P["PackedScene (loaded)"]
    P -->|"instantiate()"| C1["Coin copy 1"]
    P -->|"instantiate()"| C2["Coin copy 2"]
    P -->|"instantiate()"| C3["Coin copy 3"]

One blueprint, many independent instances.

Instancing in the editor

  1. Create a scene coin.tscn: root Area2D named Coin, with a Sprite2D and a CollisionShape2D. Save it.
  2. Open level.tscn. In the FileSystem dock, drag coin.tscn into the viewport, or press the chain-link Instantiate Child Scene button in the Scene dock.
  3. Repeat: you now have Coin, Coin2, Coin3, each with a clapper icon in the Scene dock.

In level.tscn, a coin instance stores only what is different from the original: its name and position. Edit coin.tscn (change the sprite) and every level that uses it changes automatically.

To tweak an instance's inner nodes in one place only, right-click it and choose Editable Children. Use it sparingly: such overrides are stored in the parent scene and can surprise you later. Prefer exported variables on the coin's script so each instance can be customized from the Inspector, e.g. @export var value := 1.

To work inside the instance's original, right-click and choose Open in Editor.

Instancing from code

GDSCRIPT
extends Node2D

const COIN_SCENE: PackedScene = preload("res://coin/coin.tscn")

func spawn_coin(at: Vector2) -> void:
    var coin := COIN_SCENE.instantiate()
    coin.position = at
    add_child(coin)

Points to remember:

  • preload("path") loads at compile time and must be a constant string. Use it for scenes you always need. For paths decided at runtime use load(path).
  • instantiate() creates the copy but does not put it in the tree. Nothing (no _ready, no drawing, no physics) happens until add_child(coin).
  • Set properties before add_child when possible, so _ready already sees them. Careful: @onready variables are not assigned yet. Do not touch child nodes before the copy is in the tree.
  • You can export the scene so the designer picks it: @export var enemy_scene: PackedScene, then drag a .tscn onto the slot in the Inspector. That avoids hard-coded paths.

A typical spawner

GDSCRIPT
extends Node2D

@export var bullet_scene: PackedScene
@export var cooldown := 0.25
var _timer := 0.0

func _process(delta: float) -> void:
    _timer -= delta
    if Input.is_action_pressed("shoot") and _timer <= 0.0:
        _timer = cooldown
        var bullet := bullet_scene.instantiate()
        bullet.global_position = global_position
        get_tree().current_scene.add_child(bullet)

Why get_tree().current_scene.add_child? If you add the bullet as a child of the player, it follows the player when the player moves, which is wrong for a bullet. Add it to the level instead.

Inherited scenes

Sometimes you want a variation, not a copy: a FastEnemy that is an Enemy with a different speed and sprite. In the editor choose Scene > New Inherited Scene and pick enemy.tscn. The new scene is linked to the original: structure changes in enemy.tscn flow into it, and you override only what differs. Inherited scenes are great for families of similar things; for anything simpler, use exported variables on one shared scene.

Instances in the tree

Each instance's root is a normal node. In the Scene dock the root of an instance can be renamed, moved, deleted, and given its own script on top of the one inside it. In a running game there is no difference between a node placed in the editor and one created with instantiate().

Stand-in: blueprint and copies

This is not Godot. It uses a Python dictionary as the "blueprint" and deepcopy to build independent instances, with a counter to show names like Coin2.

Python Stand-in (Python): instantiate() makes independent copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second coin has value 5 while the other two stay at 1, and the blueprint is unchanged.

Common mistakes

  • Forgetting add_child, then wondering why nothing appears.
  • Calling instantiate() on a path string. You must load() or preload() the PackedScene first.
  • Using preload with a variable path. It needs a literal string.
  • Spawning bullets as children of a moving node.
  • Using Editable Children for many instances, creating hidden overrides.
  • Circular preloads: two scenes that preload each other fail to load. Use load() at runtime for one of them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does instantiate() return?

    instantiate() builds a new copy of the saved scene. It becomes active only after add_child.
  2. 2. What is the best way to let each coin have a different value from the Inspector?

    @export variables can be set per instance in the Inspector.
  3. 3. Why add a bullet to the level and not to the player node?

    Children inherit the parent's transform, so the bullet would follow the player.
  4. 4. When can preload() be used?

    preload runs at compile time, so the path must be a constant string. Use load() for dynamic paths.

Summary

  • A saved scene is a blueprint; PackedScene.instantiate() creates independent copies.
  • In the editor drag a scene into another scene; changes to the original propagate.
  • In code: preload/load, instantiate(), set properties, add_child().
  • Use @export var scene: PackedScene and exported variables for flexible spawners.
  • Inherited scenes create variants; keep Editable Children rare.

Next: Resources and Loading