-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistency in boolean validation of [Freed Object] #59816
Comments
cc @vnen |
I also wanted to report this. I hope this will be helpful.
Detailsv3.4.2.stable.official [45eaa2d] tool
extends EditorScript
func test(value):
print("-----")
print("value ", value)
print("not value ", not value)
print('"+" if value else "-" ', "+" if value else "-")
print("is_instance_valid(value) ", is_instance_valid(value))
print("value == null ", value == null)
print("typeof(value) == TYPE_NIL ", typeof(value) == TYPE_NIL)
print("typeof(value) == TYPE_OBJECT ", typeof(value) == TYPE_OBJECT)
print("value is Object ", value is Object)
print("value is Node ", value is Node)
func _run():
var object = ClassDB.instance("Object")
object.free()
test(object)
var node = Node.new()
node.free()
test(node)
test(null)
v4.0.alpha5.official [d7d528c] @tool
extends EditorScript
func test(value):
print("-----")
print("value ", value)
print("not value ", not value)
print('"+" if value else "-" ', "+" if value else "-")
print("is_instance_valid(value) ", is_instance_valid(value))
print("value == null ", value == null)
print("typeof(value) == TYPE_NIL ", typeof(value) == TYPE_NIL)
print("typeof(value) == TYPE_OBJECT ", typeof(value) == TYPE_OBJECT)
print("value is Object ", value is Object)
print("value is Node ", value is Node)
func _run():
var object = ClassDB.instantiate("Object")
object.free()
test(object)
var node = Node.new()
node.free()
test(node)
test(null)
I don't know how the is operator should behave in this case. Should inheritance be checked for freed objects?
It's probably not possible, but it's strange that
I don't think this is the right solution, |
|
The error is still reproduced in 4.0 beta 1. |
Related? # 3.x
print(1 if "" else 2) # 2
print(1 if !"" else 2) # 1
# 4.0 beta 3
print(1 if "" else 2) # 2
print(1 if !"" else 2) # 2 |
I agree this is a weird situation (I came here from google, searching whether this behavior changed in 4). But also it can be useful to distingish a no-longer-existent object from a variable intentionally set (or initialized) to null. Say, we're tracking a target node, keeping this as a variable on the player. We'd want to know when it disappears. So I'm not sure it's a good idea to get rid of this distinction completely. |
The main question is whether I do like the idea of having this to be OTOH it might be a bug in the game logic if you are dealing with freed objects, so you may want to catch that and fix the source of the bug. If this is |
Actually, it was pointed out to me that |
|
Well, that would be actually correct as it is, but I should have been clearer that I mean this as a conversion to bool, in particular when using as a condition to The To be extra clear, I'm not proposing to remove I believe it is established that Same applies to Now, we could make so In the end, I think I'll go with |
And then we just need to make sure in the documentation that if an object evaluates to true, but further down the line the code throws errors, then they need to use |
I don't mind if Freed Object is booleaned to Moreover, this can be used not only with Comparing a Freed Object to It's sad that the user can shoot themselves in the foot by adding |
Reopening as #73896 is being reverted for now for 4.3. |
Godot version
v4.0.alpha5.official [d7d528c]
System information
Ubuntu
Issue description
I'm aware we ought to use
is_instance_valid()
to validate an Object.However in Godot 4 things may be a little different since comparison with
null
is now possible.While
[Freed Object] == null
andnot [Freed Object]
was false in Godot 3, it is now true in 4.However,
[Freed Object]
still validates to true in an if, whilenull
validates to false.This now results in very strange behavior:
or:
Many new users try comparison with null first before learning about
is_instance_valid()
(why this doesn't work in Godot 3 is a very common question). Since this yields the expected result now, they'll likely stick to it and never learn aboutis_instance_valid()
. Treating[Freed Object]
likenull
will however end up in users assumingif [Freed Object]:
will validate to false, which it does not.So here's the two solutions I can see (either or):
[Freed Object] == null
andnot [Freed Object]
should both be false againif [Freed Object]:
should validate to false as wellis_instance_valid()
Steps to reproduce
Run example code in a
_ready
function.Minimal reproduction project
No response
The text was updated successfully, but these errors were encountered: