Topic was automatically imported from the old Question2Answer platform.
Asked By
MOSN
if target != null && target.is_in_group(“player”)
I’m using this to avoid “target.is_in_group()” crashing the game when there is no target. But a “previously freed instance” apprently passes “!= null”. I fixed this earlier by instead using “target is Node2D”, but lo and behold in godot 3.2 that now also causes a crash:
“Left operand of “is” was already freed”
When ever player is freed use remove_child(player) instead. The error is happening because even though you’re using and in the if statement target.is_in_group("player") is still being checked so it would be better to make it two if statements.
if target:
if target.is_in_group("player"):
...
Based on the testing I’ve done conditions with && between get checked from left to right and it stops if it runs into a false condition, so this won’t make a difference. The problem is that a previously freed instance passes the first check.
Not sure how it relates to the rest of the comment, but I guess I’ll try using get_parent().remove_child(self) instead of queue_free(), to see if that makes a difference.
MOSN | 2020-01-30 13:01
Seems like that might actually have worked, so thanks
Still would like to know a failsafe way to check that a variable is a currently existing node though. Seems weird to me that a “previously freed instance” returns true from “if target:”, but with that being the case is there another condition you could use to make sure target is neither null or a previously freed instance?
MOSN | 2020-01-30 13:57
Not really, queue_free() should cause the reference to be lost so the variable should return null. In fact I’m sure that’s why it’s better to use queue_free() rather than free().
Magso | 2020-01-30 15:09
Well… But that just doesn’t seem to be the case.
But at least it’s working with the new method.