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 withif 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.
Select a Node in the Scene Panel (e.g.,
Player).
- 2. Click the Attach Script button (the small scroll icon with a green '+').
-
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
varkeyword.
-
Functions are blocks of code that perform an action. Use the
funckeyword.
-
_ready(): Runs exactly once when the game starts.
-
_process(delta): Runs every single frame (e.g., 60 times a second).
python
6. Logic and Conditions (If Statements)
Games are built on decisions. We useif statements to check conditions.
python
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
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
ifstatement, 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.
Create a
Node2Dscene. Attach a script to it.
- 2. In the script, delete everything and write the following:
python
- 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. What keyword is used to declare a variable in GDScript?
-
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 CharacterBody2Dmeans 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 usingif 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.