Your first real script. You'll learn the two functions almost every Godot script uses, why game code multiplies by delta, and you'll make the coins from Basics 2 spin and bob.
Two coins race to the finish line. Their code is almost the same. Race them on computers of different speeds and see what happens.
Each button starts a fresh race from the start line. Results for every speed you try stay in the table, so you can compare. FPS means frames per second: how many times a second the game updates.
delta, the time in seconds since the last frame, so it moves at the same speed everywhere. Games use the Coin B approach so the game plays the same on every computer.Here's the script you'll write later, with every part explained. You don't need to memorize this; just get the gist.
extends Area2D: “this script controls an Area2D node.” Godot writes this line for you.#: a comment. Notes for humans; Godot ignores them.var: a variable, a named box that holds a value, like time.@export: shows the variable in the Inspector, so you can tweak it without touching code.func _ready(): runs once, when the node first appears.func _process(delta): runs every frame, about 60 times a second. delta is the seconds since the last frame.Change the code and watch the coin react. Complete all four goals.
👇 The yellow dashed boxes are menus. Click one to change that part of the code.
What each menu controls: ① which property changes every frame ② by how much ③ whether that's per frame or per second (× delta) ④ how the coin moves up and down. Stuck on a goal? Click its 💡.
About 10 minutes. Open your project from Basics 2.
extends Area2D and possibly a couple of functions that just say pass (meaning “do nothing”).To get back to your scene later, click 2D at the top._ready().Godot wants one Tab per indentation level, not spaces. Select all the code and use Edit → Indentation → Convert Indent to Tabs in the script editor's menu. Or delete the spaces at the start of the line and press Tab once.
Usually a typo, or a var line in the wrong place. The var and @export lines must be at the top with no indentation, outside any func. The error bar at the bottom of the script editor tells you the line number.
Three things to check. 1) You pressed F6 with the main tab open. 2) The script is on Coin in coin.tscn: there should be a scroll icon next to it in the Scene dock. 3) You saved the script (no (*) next to its name in the Script view).
You attached the script to one coin inside main.tscn. Right-click that coin and choose Detach Script (or click the script icon with a red ×), then attach it inside coin.tscn instead.
In coin.tscn, select the Sprite2D and check its Position is 0, 0 and Centered is ticked. Things rotate around their node's position.
Save first. Press Ctrl+S (Cmd+S) in the main tab, then run again. Unsaved changes don't always make it into the game. A (*) next to a tab's name means it has unsaved changes. Still not working? Check the rotation line in your script says rotation += spin_speed * delta, not 2.0.
Save the script first (Ctrl+S). Then select a coin in main.tscn and look at the very top of the Inspector.
When you run main.tscn, all three coins spin and gently bob up and down. Coin2 spins the other way, fast. The Output panel shows three “Coin ready at…” lines.
Right now all your coins bob up and down at exactly the same moment, which looks robotic. Make each coin start at a different point in its bob, without changing anyone's speed.
You'll need: your coin script from this lesson, plus one new function: randf(), which gives a random number between 0 and 1.
Look at what time does in your script. Every coin starts it at 0, so they're all in step. What if each coin started it at a different number?
In _ready(), set time to something random. randf() * 10.0 gives a random number between 0 and 10.
func _ready() -> void:
start_y = position.y
time = randf() * 10.0 # each coin starts at a random point in its bob
print("Coin ready at ", position)Only one new line. That's the point: small changes to the right variable go a long way.
Three questions, then you're done.
You know what _ready and _process do, why games multiply by delta, and how @export turns code into settings anyone can tweak.