Empty Variables
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 8 of 92.
null is a special value that means "no value". A variable that holds null exists, but nothing is stored in it yet:
var weapon = nullUse null for something that is not known or not chosen yet:
var player_name = null # No name entered yet
var target = null # No enemy selected yetA variable created without a value also starts as null, so var shield on its own holds null.
Printing null shows <null>. When the value is known, store it like any other value:
var weapon = null
print(weapon) # <null>
weapon = "Sword"
print(weapon) # SwordNote: null is not the same as 0, an empty string "" or false. Those are real values: zero, empty text and false. null means there is no value at all.
Challenge
BeginnerThe player has not picked a weapon yet.
- Under the first comment, create a variable named
weaponwith the valuenull. - Under the second comment, store the
String"Sword"inweapon.
Do not change the print() lines. The program prints:
weapon = <null>
weapon = SwordTry it yourself
extends Node
func _ready():
# Create the variable weapon with the value null
print("weapon = ", weapon)
# Store "Sword" in weapon
print("weapon = ", weapon)
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