Menu
CoddyTech

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 = null

Use 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 yet

A 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) # Sword

Note: 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 icon

Challenge

Beginner

The player has not picked a weapon yet.

  1. Under the first comment, create a variable named weapon with the value null.
  2. Under the second comment, store the String "Sword" in weapon.

Do not change the print() lines. The program prints:

weapon = <null>
weapon = Sword

Try it yourself

extends Node

func _ready():
	# Create the variable weapon with the value null
	
	print("weapon = ", weapon)
	# Store "Sword" in weapon
	
	print("weapon = ", weapon)
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