Menu
CoddyTech

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 # true

If one side is false, the result is false:

var has_key := true
var door_found := false
var can_open := has_key and door_found # false

Each side can be a comparison, because a comparison gives a bool:

var hp := 30
var is_hurt := hp > 0 and hp < 50 # true

GDScript 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 icon

Challenge

Easy

A player can drive when they are at least 18 years old and have a driving license. Change the values in the code:

  1. Set age to 20
  2. Set has_license to true
  3. In the line of can_drive, replace the 0 with the minimum age, 18

The output must be:

Age: 20
Eligible to drive: true

Try 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online GDScript compiler