How do I check if a node was freed?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By batmanasb
:warning: Old Version Published before Godot 3 was released.

So my issue is that I have a variable called target that is a reference to another node. Right now my camera has a script that looks something like this:

_fixed_process(delta):
    if(target):
        #move to target

This way it moves to any target I set, and if the target is null it ignores it. But I seem to have a problem when I call queue_free() inside of a node that is the target. The error I get is:

Attempt to call function 'get_pos' in base 'previously freed instance' on a null instance.

It seems like target wasn’t reset to null after what it was referencing was deleted.

:bust_in_silhouette: Reply From: PixelWizzard

Maybe you should try checking has_node(target) first, just to make sure the node exists.
If you assign a node to a variable, this assignment is not going to change automatically once you delete the node. Istead, you have to assign null yourself or check if the node exists everytime you want to use it.

Thanks, this works for me. Although one small correction this answer needs is that has_node()takes a NodePath as the parameter, not a Node. So this made it a lot more work to replace the simple “is null” check I used earlier because I had to save the NodePath of the target node as well.

batmanasb | 2016-03-23 19:09

Hi, im really new into this and i had the same problem as you! My question is how you get the nodepath from a node? sorry if my question is simple (also sorry for my english, im not a native speaker)

Thanks!

Avocadosan | 2016-03-23 22:51

node.get_path() gets you the path from the root. Then you have to get the root and call has_node(path) from there in order to check if the node is still there (aka not deleted). So: get_tree().get_root().has_node(targetPath)

batmanasb | 2016-03-23 23:20

Thanks for answering! this worked :D!

Avocadosan | 2016-03-24 00:14

:bust_in_silhouette: Reply From: puppetmaster-

I prefer to work with group.

#to get the target node(s)
get_tree().get_nodes_in_group("target")

#delete target node
remove_from_group("target")
add_to_group("free")

#free all
func delete_free():
	for node2free in get_tree().get_nodes_in_group("free"):
		node2free.free()

yeah, I considered that option but it might get messy

batmanasb | 2016-03-23 23:30