Skip to main content
Godot Fundamentals – Complete Beginner to Advanced Guide
CHAPTER 05 Beginner

GDScript Programming Basics

Updated: May 16, 2026
30 min read

# CHAPTER 5

GDScript Programming Basics

1. Introduction

To make a game interactive, you need logic. You need rules that say: "If the player's health reaches zero, display the Game Over screen." In Godot, you write these rules using GDScript. GDScript is a programming language built specifically for Godot. It is heavily inspired by Python, meaning it reads almost like plain English. It is incredibly lightweight, fast to write, and designed entirely around the Node architecture. In this chapter, we will learn how to code. We will cover the basic syntax of GDScript, how to store data in variables, how to make decisions with if statements, and how to use Godot's powerful "Signal" communication system.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Attach a script to a Node in the Godot Editor.
  • Understand GDScript syntax (indentation and keywords).
  • Declare Variables and write custom Functions.
  • Use conditional logic (if, elif, else).
  • Connect and emit Signals (Godot's event system).

3. Attaching a Script

Code must live on a Node.
  1. 1. Select a Node in the Scene Panel (e.g., Player).
  1. 2. Click the Attach Script button (the small scroll icon with a green '+').
  1. 3. Click Create. Godot switches to the "Script" workspace, and you see a blank text file ending in .gd.

4. GDScript Syntax (The Rules of the Language)

  • Extends: The first line is usually extends Node2D. This tells the script, "I am attached to a Node2D, give me access to all of its abilities."
  • Indentation is Mandatory: GDScript does not use curly brackets {}. It uses tabs (indentations) to group code. If your indentation is wrong, the code will crash.
  • Comments: Any line starting with a # is ignored by the computer. Use it to write notes to yourself.

5. Variables and Functions

  • Variables store data (like memory boxes). Use the var keyword.
  • Functions are blocks of code that perform an action. Use the func keyword.
Godot provides built-in functions that run automatically:
  • _ready(): Runs exactly once when the game starts.
  • _process(delta): Runs every single frame (e.g., 60 times a second).
python
12345678910111213141516
extends Node2D

# 1. Variables
var player_name = "Hero"  # A string (text)
var health = 100          # An integer (whole number)
var speed = 2.5           # A float (decimal number)

# 2. Built-in Function (Runs at start)
func _ready():
    print("Welcome to the game, " + player_name)
    take_damage(20) # Calling our custom function

# 3. Custom Function
func take_damage(amount):
    health = health - amount
    print("Health is now: ", health)

6. Logic and Conditions (If Statements)

Games are built on decisions. We use if statements to check conditions.
python
1234567
func check_death():
    if health <= 0:
        print("Game Over!")
    elif health < 20:
        print("Warning: Low Health!")
    else:
        print("Player is fine.")

7. Signals (The Communication System)

How does a Button tell a Menu to open? Through Signals. Signals are Godot's version of events. When something happens (a button is clicked, an enemy dies), the node "Emits" a signal. Other nodes can "Connect" to that signal to listen for it and run a function when they hear it.
  • *Visual Connection:* You can select a Button, go to the "Node" dock (next to the Inspector), double-click the pressed() signal, and attach it to your script visually.

8. Visual Learning: The Signal Flow

txt
1234567891011121314
[ Node: StartButton ] 
  |
  +-- (Player clicks button)
  |
  +-- Emits Signal: `pressed`
          |
          v
(Wire connects signal to Level script)
          |
          v
[ Node: LevelManager (Script) ]
  func _on_StartButton_pressed():
      print("Starting Level 1!")
      load_level()

9. Best Practices

  • Use Static Typing (Optional but great): In GDScript, you can force a variable to *only* hold a specific type of data by using a colon. var health: int = 100. This prevents bugs (like accidentally trying to store the word "Apple" inside a health variable) and makes the code run slightly faster.

10. Common Mistakes

  • Indentation Errors: A beginner writes an if statement, but the next line is flush against the left wall instead of indented by one tab. The engine throws an "Expected indented block" error. *Always press Tab after a colon :!*

11. Mini Project: The Health Calculator

Objective: Write your first script to calculate math and use logic.
  1. 1. Create a Node2D scene. Attach a script to it.
  1. 2. In the script, delete everything and write the following:
python
123456789101112131415
extends Node2D

var hp: int = 100

func _ready():
    print("Start Game. HP: ", hp)
    take_hit(40)
    take_hit(70)

func take_hit(damage: int):
    hp -= damage
    print("Took ", damage, " damage. Current HP: ", hp)
    
    if hp <= 0:
        print("Player Died!")
  1. 3. Press Play. Look at the "Output" console window at the bottom of the screen. You should see the math happen and the "Player Died!" message print.

12. Practice Exercises

  1. 1. What keyword is used to declare a variable in GDScript?
  1. 2. Explain the difference between the _ready() function and the _process(delta) function.

13. MCQs with Answers

Question 1

In GDScript, how does the engine know which lines of code belong inside an if statement?

Question 2

Which Godot system allows a Timer node to alert a Spawner node that 5 seconds have passed without the two nodes directly referencing each other in code?

14. Interview Questions

  • Q: GDScript is often compared to Python. What are the key syntactic similarities, and what makes GDScript uniquely suited for the Godot Engine?
  • Q: Explain the Observer Pattern in the context of Godot. How do Signals solve the problem of tightly coupled code between different nodes?
  • Q: Walk me through what extends CharacterBody2D means at the top of a script. What is inheritance in Object-Oriented Programming?

15. FAQs

Q: Should I use C# instead of GDScript? A: If you are a beginner, 100% stick to GDScript. It is faster to write, has better documentation in Godot, and perfectly integrates with the engine. Use C# only if you already have years of experience with it from Unity or enterprise software.

16. Summary

In Chapter 5, we learned how to speak to the computer. We mastered the clean, Python-like syntax of GDScript. We learned how to store game states in Variables, encapsulate logic into Functions, and make branching decisions using if statements. Most importantly, we uncovered Godot's powerful Signal system, allowing disparate nodes in our game to communicate with each other seamlessly. We are no longer just building static scenes; we are programming game logic.

17. Next Chapter Recommendation

We know how to write code. It is time to apply that code to move a character across the screen. Proceed to Chapter 6: Player Movement and Input Systems.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·