Logical Operators Part 1
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 21 of 92.
Logical operators combine bool values. GDScript has three of them: and, or and not.
and gives true only when both sides are true:
var has_key := true
var door_found := true
var can_open := has_key and door_found # trueIf one side is false, the result is false:
var has_key := true
var door_found := false
var can_open := has_key and door_found # falseEach side can be a comparison, because a comparison gives a bool:
var hp := 30
var is_hurt := hp > 0 and hp < 50 # trueGDScript also accepts symbols for these operators: && for and, || for or and ! for not. The Godot style guide recommends the words, and this course uses them.
Challenge
EasyA player can drive when they are at least 18 years old and have a driving license. Change the values in the code:
- Set
ageto20 - Set
has_licensetotrue - In the line of
can_drive, replace the0with the minimum age,18
The output must be:
Age: 20
Eligible to drive: trueTry it yourself
extends Node
func _ready():
# Change the values below
var age: int = 0
var has_license: bool = false
var can_drive: bool = age >= 0 and has_license
# Don't change the lines below
print("Age: ", age)
print("Eligible to drive: ", can_drive)
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