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.
Use the buttons to make coins from the coin scene. The order you do things in matters. Follow the mission.
Spawning a scene from code is always the same few steps.
Loads your scene file once, when the game starts. It's a PackedScene: a blueprint, not a coin yet.
Builds a brand-new coin from the blueprint. It exists, but it isn't in the game. Nothing shows up yet.
Adds the new coin to the scene tree. Only now does it appear, run its _ready(), and start _process.
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.”
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.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.
About 12 minutes. Keep working in your project from Basics 6.
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.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._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.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.
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.
The path to coin.tscn is wrong. Delete the text between the quotes and drag coin.tscn from the FileSystem dock into that spot.
Your coin.gd doesn't have signal collected yet. Finish Basics 6 first, or add that line near the top of coin.gd.
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.
The position is being set after add_child. Put coin.position = … above add_child(coin).
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.
That's random placement. It's fine for now, or try smaller ranges in randf_range to keep coins in an open area.
Every 2 seconds a new spinning, bobbing coin appears somewhere random. Collecting any coin, placed or spawned, raises the score.
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.
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.
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.
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.
# 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 sameGroups are handy everywhere: “all enemies,” “all doors,” “everything that can be hit.” You'll see them again.
Three questions, then you're done.
preload, instantiate, position, connect, add_child: the same recipe works for enemies, bullets and anything else you'll ever spawn.