Play Game Design · Godot Basics · 3 of 10

Moving things with code

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.

⏱ About 20 minutes✅ Needs Basics 2 (your coin scene)⌨️ No coding experience needed
0 of 4 complete
1

Feel it: the coin race

Two coins race to the finish line. Their code is almost the same. Race them on computers of different speeds and see what happens.

# Coin A moves 4 pixels every frame position.x += 4 # Coin B moves 240 pixels every second position.x += 240 * delta
Race on a computer running at:

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.

No races yet. Pick a computer speed above.
What happened: Coin A moves a fixed amount per frame, so faster computers move it faster. Coin B multiplies by 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.
2

Name it: reading a script

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.
  • Indentation (the space at the start of a line) shows which lines belong to which function. Godot uses one Tab per level.
3

Prove it: code playground

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 💡.

Computer speed:
Goals

    4

    Build it in Godot: spinning, bobbing coins

    About 10 minutes. Open your project from Basics 2.

    1. Open the coin scene. Click the coin tab above the viewport. If it's closed, double-click coin.tscn in the FileSystem dock (bottom left).
    2. Select the root. In the Scene dock, click Coin, the top node with the blue icon. Not the Sprite2D: the script needs to go on the coin itself.
    3. Attach a script. At the top of the Scene dock, to the right of the Filter Nodes box, click the Attach Script button: a scroll icon with a small green +. A window called Attach Node Script opens.Check it says Language: GDScript and Inherits: Area2D, and that the path ends in coin.gd. Leave everything else as it is and click Create.
    4. Meet the script editor. Godot switches to the Script view at the top of the window. You'll see 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.
    5. Make it spin. Select all the code (Ctrl+A, Cmd+A on Mac) and replace it with this:
      Typing it yourself is the best practice. Godot suggests words as you type; press Enter to accept one. The second line needs one Tab at the start. Copying is fine too.
    6. Save and run. Press Ctrl+S (Cmd+S). Then click 2D, click the main tab above the viewport, and press F6 (Cmd+R).Make sure the main tab is open when you press F6. It runs whichever scene tab is open.
    7. Add bobbing and settings. Go back to the coin script: click Script at the top and make sure coin.gd is highlighted in the script list on the left (click it if not). Select all again and replace it with the full version:
    8. Save and run again (Ctrl+S, then the main tab and F6). Then look at the Output panel at the bottom of the editor: there's one “Coin ready at…” line per coin, printed by _ready().
    9. Give one coin its own personality. In the main tab, click Coin2 in the Scene dock. At the top of the Inspector there's a section for your script with Spin Speed, Bob Height and Bob Speed. Set Coin2's Spin Speed to -6 and press Enter. A ↺ arrow appears next to it, meaning this coin is now different from the others. Save with Ctrl+S before running, or the change may not show up.In Basics 2, changing one instance was a mistake. Here it's on purpose: your @export settings let each coin be a little different.
    🛠 Common problems
    Red error: “Mixed use of tabs and spaces” or “Unexpected indent”

    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.

    Red error: “Identifier … not declared in the current scope”

    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.

    The coins don't move at all

    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).

    Only one coin moves

    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.

    The coins spin around a strange point, like a wheel off its axle

    In coin.tscn, select the Sprite2D and check its Position is 0, 0 and Centered is ticked. Things rotate around their node's position.

    I changed Coin2's Spin Speed, but it still spins the same way

    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.

    I can't see Spin Speed in the Inspector

    Save the script first (Ctrl+S). Then select a coin in main.tscn and look at the very top of the Inspector.

    Checkpoint

    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.

    🎯 Your turn optional · no code given

    Make the coins bob out of sync

    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.

    Done when you run main.tscn and the three coins are clearly at different heights at the same moment.

    You'll need: your coin script from this lesson, plus one new function: randf(), which gives a random number between 0 and 1.

    💡 Hint 1 (a nudge)

    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?

    💡 Hint 2 (more help)

    In _ready(), set time to something random. randf() * 10.0 gives a random number between 0 and 10.

    ✅ Solution (try it yourself first!)
    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.

    5

    Quick check

    Three questions, then you're done.

    🎉 You wrote your first game code.

    You know what _ready and _process do, why games multiply by delta, and how @export turns code into settings anyone can tweak.

    Next · Basics 4: Input with keys & controllers
    Make something move when the player presses a key
    Start Basics 4 →