Back to "Introduction to Godot 4: From Zero to Platformer" Course

Part 3: GDScript Scripting Fundamentals in Godot 4

Part 3: GDScript Scripting Fundamentals in Godot 4
Valentino Phiri
Instructor Valentino Phiri
Published
Duration 4 min read
Series Status in progress

Welcome back! In Part 2, we built our first scene and added a Sprite2D. We even made it move with a tiny script. But if you looked at that code and thought, “I have absolutely no idea why any of that worked”—this part is for you.

Advertisement Space

Reserved for AdSense Revenue

We’re going to slow down, zoom in, and really understand the language that powers Godot: GDScript.

Why GDScript?

This is a fair question. Godot supports multiple languages, including C# and C++. So why learn a custom one?

Here’s the honest answer: GDScript was designed to be the fastest path from an idea in your head to something moving on screen. It reads almost like plain English. There’s no need to wait for long “compilation” times, and no scary technical walls to climb.

Once you understand the logic here, picking up other languages later becomes a walk in the park.

Pro-Tip: GDScript is “dynamically typed” by default, meaning you don’t have to tell Godot that a number is a number. However, as you get better, you can add “type hints” like var speed: int = 400 to help the editor catch your mistakes before you even run the game!

Variables: Giving Names to Data

A variable is just a named box that holds a value. In Godot, we declare them using the var keyword.

1
2
3
4
5
6
extends Sprite2D

var player_name = "Valentino"
var health = 100
var speed = 250.5
var is_alive = true
  • player_name holds text (a String).
  • health holds a whole number (an Integer).
  • speed holds a decimal (a Float).
  • is_alive holds a true/false (a Boolean).

Functions: Packaging Actions

A function is a named block of code that you can run whenever you need it. Godot has special “Lifecycle” functions that run automatically.

_ready() — The Constructor

This runs once, the moment your node “wakes up” in the game. It’s perfect for setting things up.

_process(delta) — The Heartbeat

As we saw in Part 2, this runs every single frame. If your game is running at 60fps, this code runs 60 times a second.

Note: Remember that delta makes your game run at the same speed regardless of how fast the player’s computer is. Always multiply your movement by delta!

Signals: The Godot Way of Communicating

This is the concept that trips up most beginners, but it’s the most powerful idea in Godot.

Imagine you have a TV Remote (a Button) and a TV (your Character).

  • The Wrong Way: The Button reaches out and physically grabs the TV to turn it on. If you replace the TV with a Radio, the Button breaks because it’s still looking for a TV.
  • The Signal Way: The Button just shouts “I WAS PRESSED!” into the room. It doesn’t care who is listening. The TV “listens” for that shout and turns itself on.

This is called Signals. It allows different parts of your game to talk to each other without being “glued” together.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# In your Player script
extends Sprite2D

signal health_depleted # Declare the "shout"

var health = 100

func take_damage(amount):
    health -= amount
    if health <= 0:
        health_depleted.emit() # Shout the signal!

Putting It All Together: A Mini Game Loop

Let’s build a simple “Click to Heal” system. Create a scene with a Label (named HealthLabel) and a Button (named HealButton).

Attach this script to your root node:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
extends Node2D

const MAX_HEALTH = 100
var current_health = 30

func _ready():
    # Connect the Button's "shout" to our function
    $HealButton.pressed.connect(_on_heal_pressed)
    _update_ui()

func _on_heal_pressed():
    current_health = min(current_health + 25, MAX_HEALTH)
    _update_ui()

func _update_ui():
    $HealthLabel.text = "HP: %d / %d" % [current_health, MAX_HEALTH]
    
    # Disable the button if we are full!
    $HealButton.disabled = (current_health >= MAX_HEALTH)

Key Takeaways

  • Variables store data; Functions perform actions.
  • _ready runs once; _process runs every frame.
  • Signals allow nodes to talk without being “glued” together.
  • Indentation (Tabs) is how Godot knows which code belongs to which function.

In Part 4: Player Movement, we’ll apply all of this to build a real, jumping, gravity-defying player controller!

Advertisement Space

Reserved for AdSense Revenue

Share this article:
Stay in the loop

Join the Veigatec Dev Loop

Get the latest Godot, Flutter, and Web Development engineering insights delivered straight to your inbox. No fluff, just code.

We respect your privacy. Unsubscribe at any time.

Sponsored

Special Developer Offer: 20% off at TechGadgets

Upgrade your workspace with premium hardware and software tools today.