← PathwayLesson 2 · GDScript FundamentalsNext lesson →

Lesson 02 · 55 minutes · Logic layer

Teach nodes what to do.

Turn your first room into a programmable system. You will store game data, make decisions, organize behavior into functions, and safely connect a script to other nodes.

VARIABLESFUNCTIONSIF / ELSELOOPSNODE REFERENCES
Big ideaCode gives a node memory, decisions, and repeatable actions.
PredictChanging one variable should change the visible result without rewriting every step.
TestTrace one event from input to state change to UI feedback.
Why it mattersClear ownership makes a game easier to debug and expand.
01 / DATA

State

Variables remember what can change.

A variable gives a name to data. Use clear names and add a type when the kind of data should stay consistent.

player.gd
extends CharacterBody3D

@export var speed: float = 5.0
var health: int = 3
var player_name: String = "Nova"
var can_move: bool = true

@export

Shows the variable in the Inspector so a designer can tune it without editing code.

Type annotation

: float promises that speed will hold a decimal number and catches mistakes early.

02 / FLOW

Decisions

Conditions choose what happens next.

damage and decisions
func take_damage(amount: int) -> void:
    health -= amount
    if health <= 0:
        respawn()
    else:
        print("Health: ", health)

func respawn() -> void:
    health = 3
    global_position = Vector3(0, 2, 0)

Functions package one meaningful behavior. Parameters bring data in; -> void says the function does not return a value.

Read it aloud: “When the player takes damage, subtract the amount. If health reaches zero, respawn. Otherwise, print the remaining health.”
03 / NODES

References

Use the scene tree as an address book.

Add a Label named StatusLabel under a CanvasLayer. Drag it from the Scene dock into the script editor while holding Ctrl/⌘ to generate a node reference.

Godot's Scene dock showing a real nested hierarchy: a Ball root node with Sprite2D and CollisionShape2D as indented children.
The tree is the address. Each indented child is one step in a node path. A Sprite2D nested under Ball is reached as $Sprite2D from Ball's own script — the same way $UI/StatusLabel walks down through a CanvasLayer to reach a Label.

Screenshot: Godot Engine documentation, © Juan Linietsky, Ariel Manzur and the Godot community, CC BY 3.0.

safe node reference
@onready var status_label: Label = $UI/StatusLabel

func _ready() -> void:
    status_label.text = "Ready, " + player_name

func _process(_delta: float) -> void:
    status_label.text = "%s | HP %d | Speed %.1f" % [player_name, health, speed]

func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("test_damage"):
        take_damage(1)

func show_inventory(items: Array[String]) -> void:
    for item in items:
        print(item)

@onready waits until the node and its children are ready. A for loop repeats once for every item in a collection.

04 / TRANSLATE

Blocks → Code

What's a rule, and what's yours to imagine?

05 / PREDICT

Read before running

Trace the state change.

Health starts at 3. What is its value after take_damage(1) runs three times?

06 / BUILD

Practice

Create a debug dashboard.

  1. Add a CanvasLayer and Label.Name the label StatusLabel and place it in the top-left corner.
  2. Add exported variables.Create speed, health, and player_name; change them in the Inspector.
  3. Update the label.Display the player name, current health, and speed from _process().
  4. Add a test action.Map test_damage to H and call take_damage(1) when it is pressed.
  5. Prove the logic.Press H three times and confirm health resets and the player returns to the spawn point.

Remix challenge

Add an exported max_health variable. Make respawn restore that value instead of a hard-coded 3.

Duplicate the project as SignalRun_02_GDScript.Your restore point must show live HUD values and a working damage/respawn test.
07 / CHECK

Definition of done

Your logic layer works when…

0/4 checked

Evidence: Demonstrate three H presses and explain why health returns to its maximum on the third.

Lesson 2 complete

You can now express game rules in GDScript.