Why does my game look for a node that has been deleted, even though I check for its existence?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MAxSaL

Hello,

For a while I have been struggling with my game crashing randomly when I try to check if a node is in a scene. I have tried to explain my issue, but as I am new to Godot, I seldom understand what people mean and I am unsure how to apply their answer to my problem.

I asked this question: Avoid getting "Invalid get index (previously freed instance)" error? - Archive - Godot Forum

I have uploaded my project, so you can see what I mean when I describe my issue: https://ufile.io/aa7ic

My issue
Junk spawns randomly and the crabs can pick it up. When they drop it in the bins, the junk is deleted. Sometimes the game crashes, because it tries to run the code, where the crabs carry the junk after they dropped it. I test if the crab is carrying the junk by saying if carried_object: before manipulating the node. However, sometimes (and I have not found a pattern) it ignores the fact, that I check if the node exists and still tired to update its position. I tried to print out the node and at the moment it crashes, carried_object is called [Deleted Node]. This is driving me crazy as I cannot find anything online about it. If you have a suggestion, I would be very happy to receive it.

Thanks a lot! :slight_smile:

:bust_in_silhouette: Reply From: MysteryGM

Can you link the code, it is hard to tell what you are doing. There are multiple reasons this could be happening.

Are you using queue_free() to delete the object?

Also instead of if carried_object: try if carried_object != null and if carried_object != [Deleted Node] :

Here are the scripts that manipulate the nodes:

https://pastebin.com/RESR938R - bin.gd - deletes the nodes
https://pastebin.com/wv6T2b0A - helper.gd - uses the nodes and drops them in the bin

I use queue_free() when deleting the nodes and I have tried carried_object != null which did not work and carried_object == [Deleted Object] is not of valid syntax.
(it was [Deleted Object], not node, I remembered wrong)

I have read about the has_node("node") function and would try to use that. But how would I get the path string fromcarried_object?

Thanks for your help!

MAxSaL | 2018-10-10 09:21