Boolean Results
Lesson 5 of 48 in Coddy's GDScript for Lua Developers course.
Only false and nil are false in Lua. Zero and an empty string are both truthy. Lua and and or select operands, which makes a fallback expression useful but unsuitable for distinguishing false from absence. GDScript logical operators return Boolean results rather than one of their original operands. For text selection, use a conditional expression and test emptiness explicitly. Both languages short-circuit evaluation. Do not translate a Lua numeric condition mechanically: state the intended comparison. An empty text result can be valid data and should not be confused with an absent key.
Complete Lua 5.1 comparison. Run main.lua from the directory containing all shown files:
Lua file main.lua:
print(0 and "zero is true" or "false")
print("" and "empty is true" or "false")
Lua output:
zero is true
empty is trueGDScript file main.gd:
extends Node
func _ready() -> void:
print(not "name".is_empty() or not "fallback".is_empty())
print(not false)
GDScript output:
true
trueChallenge
EasyReturn yes when temperature is between 10 and 20 inclusive and enabled is true; otherwise return no.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Your code goes in exercise.gd. The supplied main.gd cannot be edited. Keep each class_name type in its named file.
Try it yourself
extends Node
func contract_ok(data: Array) -> bool:
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for Lua Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command CountPractice on your own: Online GDScript compiler