🧠 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:
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:
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:
if has_key:
print("The door opens.")
else:
print("You need a key.")
🧠 What You’ve Learned
- Booleans store either
true
orfalse
. - 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.