Godot: NodePath cant be compared with null

Created on 19 Sep 2017  路  17Comments  路  Source: godotengine/godot

Operating system or device, Godot version, GPU Model and driver (if graphics related):
Linux, 3d06957f12ba5c04702f3db980af9c07142e7886

Issue description:
It's impossible to compare NodePath with null. It will give Invalids operands 'NodePath' and 'Nil' in operator '=='. error

Steps to reproduce:

  1. Create new scene.
  2. Add Node type to the scene root
  3. Attach this script to the Node:
extends Node
var someNodePath = NodePath();
func _ready():
    if(someNodePath == null):  #Invalids operands 'NodePath' and 'Nil' (...)
        pass
    else:
        print("it's ok");

  1. Run the scene

Update 1: From my bisection it seems that 137f8a5 is the first bad commit

bug gdscript

Most helpful comment

Oh, but the comparison should return false instead of erroring, right.

All 17 comments

That variable is not null, it contains an empty NodePath. I think you must compare it to NodePath() or "".

Oh, but the comparison should return false instead of erroring, right.

After #11388 you will be able to do:

if not someNodePath:
  print("noooooo")

@neikeq we're making GDScript more strongly typed. Comparing unrelated types to each other is being removed (not that it was ever very consistent)

From my bisection it seems that 137f8a58a8f2a6c356ef00e5371ff144c8a89fb0 is the first bad commit

@hpvb not sure if this was on purpose, but comparing with null makes sense for sure (and should stay with us in my opinion)

It was done on purpose, however not adding the option back to booleanize the types was an oversight.

x != null should always work, in practice it's a shorthand for the unwieldy typeof(x) != TYPE_NIL needed with dynamic typing

@eska014 thanks for the hint, guess will use that one

@eska014 can you make an issue about this? If we want null comparisons to always work this can be easily added after a discussion.

@kubecz3k Is there any reason why if not path: would not work for you here?

It really only matters if you have to actually know if something is null. This doesn't seem to be that common. Previously you couldn't find out if something was null or not without a type check either.

Previously:

c = call()
if c == null:
  if typeof(c) == TYPE_NIL:
    print("call is null")
  print("call was empty string")

Now:

c = call()
if not c:
  if typeof(c) == TYPE_NIL:
    print("was null")
  print("x was empty string")

Point being, if you need to know it's no more code. If you don't need to know it's less code.

@hpvb not sure, I heard that empty path will return false? But even if not, putting object inside if (without anything else) is to much 'magic'/'ambigous' for my taste. But don't worry about me, I just wanted to understand what is new way of doing stuff, since I need to port project.

Also thank you a lot for your work :)

@kubecz3k yeah, an empty nodepath will be false. Is this a problem for you? If you actually have a usecase that is broken or inconvenient now please do tell us. We definitely want to fix those things before 3.0

Note that for actual Objects (e.g. Nodes), you can still use == null IINM. But NodePath is a core type like Vector2 and int, and you'd agree that Vector2(3, 1) == null or 5 == null don't make much sense. But get_node(my_node_path) == null would of course still work.

@akien-mga oh I get it now! Was not aware of the fact that NodePath is a simple type in Godot, that explains a lot. Thanks!

Was this page helpful?
0 / 5 - 0 ratings