Play Game Design · Godot Basics · 7 of 10

Spawning things

So far you've placed every coin by hand. Real games create things while they run: enemies, bullets, pickups. You'll learn the three steps to spawn a scene from code and make coins appear every few seconds.

⏱ About 25 minutes✅ Needs Basics 6
0 of 4 complete
1

Feel it: the coin factory

Use the buttons to make coins from the coin scene. The order you do things in matters. Follow the mission.

The template

coin.tscn
a scene file (PackedScene)

Made, but not in the game yet

The game (scene tree)
Mission
    2

    Name it

    Spawning a scene from code is always the same few steps.

    preload("res://coin.tscn")

    Load the template

    Loads your scene file once, when the game starts. It's a PackedScene: a blueprint, not a coin yet.

    instantiate()

    Make a copy

    Builds a brand-new coin from the blueprint. It exists, but it isn't in the game. Nothing shows up yet.

    add_child(coin)

    Put it in the game

    Adds the new coin to the scene tree. Only now does it appear, run its _ready(), and start _process.

    Timer · timeout

    Do it on a schedule

    A Timer node counts down Wait Time seconds, emits timeout, and starts again. Connect it to your spawn function for “a new coin every 2 seconds.”

    The order that matters: set the coin's position before add_child. Your coin's _ready() remembers its starting height for bobbing, and _ready() runs the moment it's added. Set the position too late, and it bobs around the wrong spot, which is exactly what you saw in the mission.
    3

    Prove it: build the spawner

    Turn lines of code on and off, choose their order and set the Timer, then watch the level. Click coins to collect them.

    👇 Tick the lines your spawner needs, and use the menu and slider under the code. The level updates live, so watch it after each change.

    Only the first line is on to start. Work through the four goals under the level. Each has a 💡 Hint, then a 👉 Show me exactly button if you're still stuck.

    Coins: 0
    Goals
      4

      Build it in Godot: a coin every 2 seconds

      About 12 minutes. Keep working in your project from Basics 6.

      1. Add a Timer. Open the main tab (click 2D first if you're in the Script view). Select the root Node2D → + → search Timer → Create. Rename it SpawnTimer. 💾 Save: Ctrl+S
      2. Set it up. In the Inspector, set Wait Time to 2 and tick Autostart. Leave One Shot unticked, so it keeps repeating. 💾 Save: Ctrl+S
      3. Connect its signal. With SpawnTimer selected, open the Signals tab (next to Inspector) and double-click timeout(). In the window, click the root Node2D. The Receiver Method should say _on_spawn_timer_timeout. Click Connect.Same move as connecting the coins in Basics 6, just a different signal. 💾 Save: Ctrl+S
      4. Update main.gd. Godot opens the script with a new empty function. Select all and replace with:
        Check the path in preload("res://coin.tscn"). If your coin scene is saved somewhere else, drag coin.tscn from the FileSystem dock into the script and Godot types the right path for you. The var coin = line has no := on purpose, because Godot can't tell ahead of time that it's your coin with a collected signal.
      5. Save and run. Ctrl+S, Scene → Save All Scenes, make sure the main tab is open, then F6. Wait a couple of seconds, then go collect.
      6. Feel the order (an experiment). You'll break the spawner on purpose to see why the order of lines matters, then fix it. ① Click Script at the top of Godot and make sure main.gd is highlighted in the script list on the left. (Not coin.gd!)
        ② Scroll to the bottom, to func _on_spawn_timer_timeout(). Right now its last four lines look like this:
            var coin = COIN_SCENE.instantiate()            # 1. make a coin (not in the game yet)
            coin.position = Vector2(randf_range(100, 1050), randf_range(100, 550))   # 2. pick a spot
            coin.collected.connect(_on_coin_collected)     # 3. listen to it
            add_child(coin)                                # 4. add it to the game
        ③ Click anywhere on the add_child(coin) line. Press Alt+↑ twice. Each press moves the whole line up by one, so it ends up right under the var coin line:
            var coin = COIN_SCENE.instantiate()            # 1. make a coin (not in the game yet)
            add_child(coin)                                # 4. add it to the game
            coin.position = Vector2(randf_range(100, 1050), randf_range(100, 550))   # 2. pick a spot
            coin.collected.connect(_on_coin_collected)     # 3. listen to it
        ④ Save (Ctrl+S), open the main scene tab at the top and press F6.
        You should see: new coins appear at random spots from left to right, but they all hug the very top edge of the window, half cut off. The coin ran _ready() the moment it was added, while it was still at (0, 0), so it remembered “my starting height is 0” and bobs up there.
        ⑤ Close the game window. Click the add_child(coin) line again and press Alt+↓ twice to put it back at the bottom, matching the first box above. Save and run again: coins appear all over the screen.
        Alt+↑ doesn't move the line? Select the whole add_child(coin) line, cut it (Ctrl+X), click at the very start of the coin.position line and paste (Ctrl+V). Make sure it still starts with one Tab, like the lines around it. 💾 Save: Ctrl+S

      💾 Save before you run. A (*) on a tab means unsaved changes, which may not show up in the game.

      🛠 Common problems
      No coins ever appear

      Check SpawnTimer has Autostart ticked, and that its timeout signal is connected to _on_spawn_timer_timeout (a green icon appears beside that function in main.gd). Also check the function name in the code matches the connection exactly.

      Error mentioning preload or “Couldn't load”

      The path to coin.tscn is wrong. Delete the text between the quotes and drag coin.tscn from the FileSystem dock into that spot.

      Error: “Invalid access to property or key 'collected'”

      Your coin.gd doesn't have signal collected yet. Finish Basics 6 first, or add that line near the top of coin.gd.

      Error about the wrong number of arguments for _on_coin_collected

      You did the “worth 5 points” challenge in Basics 6, so your coins send an amount. Keep your version of _on_coin_collected(amount) instead of the one in the code above, and the spawner will work with it.

      New coins bob at the top of the screen

      The position is being set after add_child. Put coin.position = … above add_child(coin).

      Collecting spawned coins doesn't raise the score

      The coin.collected.connect(_on_coin_collected) line is missing or misspelled. Coins made in code have to be connected in code, since you can't use the Signals tab on something that doesn't exist yet.

      Some coins appear inside walls

      That's random placement. It's fine for now, or try smaller ranges in randf_range to keep coins in an open area.

      Checkpoint

      Every 2 seconds a new spinning, bobbing coin appears somewhere random. Collecting any coin, placed or spawned, raises the score.

      🎯 Your turn optional · no code given

      Stop at 10 coins

      Your spawner keeps adding coins forever. Make it stop adding new coins while there are already 10 or more on screen, and start again once you collect some.

      Done when if you stand still, the coin count never goes above 10. Collect a few and new ones start appearing again.

      You'll need: a way to count coins. Godot can put nodes into named groups: add_to_group("coins") joins one, and get_tree().get_nodes_in_group("coins") lists everything in it.

      💡 Hint 1 (a nudge)

      Before making a new coin, the spawner should ask: how many coins exist right now? If it's enough, skip this time. return ends a function early.

      💡 Hint 2 (more help)

      In coin.gd's _ready(), add add_to_group("coins") so every coin, placed or spawned, joins the group. In main.gd, at the very top of _on_spawn_timer_timeout(), check get_tree().get_nodes_in_group("coins").size(). If it's 10 or more, return.

      ✅ Solution (try it yourself first!)
      # coin.gd: inside _ready()
      add_to_group("coins")
      # main.gd: the start of the spawn function
      func _on_spawn_timer_timeout() -> void:
      	if get_tree().get_nodes_in_group("coins").size() >= 10:
      		return   # enough coins already, skip this time
      	var coin = COIN_SCENE.instantiate()
      	# ...the rest stays the same

      Groups are handy everywhere: “all enemies,” “all doors,” “everything that can be hit.” You'll see them again.

      5

      Quick check

      Three questions, then you're done.

      🎉 Your game makes its own things now.

      preload, instantiate, position, connect, add_child: the same recipe works for enemies, bullets and anything else you'll ever spawn.

      Next · Basics 8: Score & UI
      Labels, buttons and a pause menu · coming soon
      Back to all lessons