• Godot Help
  • How to check if variable is null/empty in Godot 4?

In Godot 3 I could write:

if not variable:
    do stuff

But the "not" keyword seems reserved for booleans only now.

How do we check if a variable is empty?

  • Sorry, it's null now.

    if test == null:

This is my workaround for now:

static func is_not(variable):
	if variable is String and variable == '':
		return true
	elif variable is Dictionary and variable == {}:
		return true
	elif variable is Array and variable == []:
		return true
	elif variable is bool and not variable:
		return true
	elif variable is Object and not is_instance_valid(variable):
		return true
	elif variable is int and variable == 0:
		return true
	elif variable is float and variable == 0.0:
		return true
	return false

Since it's hard to search Godot 4 docs+info right now...

I believe the keyword is None. So.

if my_var == None:
   # it's null 

    You could also make use of != in this scenario and just have something happen if you don't get the desired result in an if statement, don't see why it wouldn't work as that's the same as not I think? Lots of syntax changes happening in Godot 4, I'm still working it out myself.

    cybereality

    Hmm I get "Identifier None not declared in current scope"

    Sorry, it's null now.

    if test == null:

    That seems to be it, thanks cybereality