World 1-1 The Fundamentals : GDScript intro

Part 15: Booleans in GDScript

Learn how Booleans work in GDScript. Understand true/false values, what they mean in code, and how they power logic and decisions in your Godot projects.

🧠 Booleans in GDScript – True or False, That’s It!

So far, we’ve explored strings, integers, and floats—each representing text or numbers. But now it’s time to meet one of the simplest and most powerful types in all of programming: the Boolean.

A Boolean value is either true or false. That’s it. No decimals, no text—just two states. It’s used to make decisions in code, like whether a player is alive, a door is locked, or an item has been picked up.


✅ Creating a Boolean

In GDScript, you create a Boolean just like other variables:

gdscript
1
2
3
4

var is_alive = true
var has_key = false

You can name them however you like, but it’s best practice to use descriptive names that start with is_, has_, or can_ to make your logic easier to read.


🔍 Checking the Type

Just like with other types, we can check if a value is a Boolean:

gdscript
1
2
3
4
5
6

print(typeof(is_alive))  # This returns 1 (the type ID for Boolean)

# Or, using our custom Variant helper class:
print(Variant.type_name(typeof(is_alive)))  # Outputs "bool"

💡 Why Booleans Matter

Booleans are the foundation for decision-making in games. You’ll use them in:

  • If statements
  • Loop conditions
  • Event triggers
  • State machines

Example:

gdscript
1
2
3
4
5
6

if has_key:
    print("The door opens.")
else:
    print("You need a key.")

🧠 What You’ve Learned

  • Booleans store either true or false.
  • They’re essential for controlling game flow and making decisions.
  • You can check their type just like any other variable.
  • Good naming helps keep your logic readable.

SightlessDev