Boolean
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 7 of 92.
A Boolean has only two possible values: true or false. Its type in GDScript is bool.
The values are written in lowercase and without quotes:
var is_alive = true
var has_key = falseThis creates two variables: is_alive holds true and has_key holds false. print(is_alive) shows true.
Capitalization matters. True and TRUE are not Booleans in GDScript: they are read as names, no variable has those names, and Godot reports an error before the program runs.
With quotes, "true" is a String that contains the word true. It prints the same, but it is text, not a Boolean.
Booleans store yes-or-no facts, such as whether a door is open or the game is over. The chapters on operators and decisions use them to make choices.
Challenge
BeginnerCreate two Boolean variables under the comment:
is_alivewith the valuetruehas_keywith the valuefalse
Do not change the print() lines. The program prints:
is_alive = true
has_key = falseTry it yourself
extends Node
func _ready():
# Create the variables is_alive and has_key below
# Don't change the lines below
print("is_alive = ", is_alive)
print("has_key = ", has_key)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler