A way to check and see if a node exists or not

I think you should try the has_node() not really sure if get_node_or_null() will work

Yeah this won’t work in this case you’d need the reference not the path.

I was talking about has_node() or get_node_or_null() where you’d need the path as a parameter like
has_node(target1path)
Sorry for confusion :pensive:

1 Like

So not like this?

Depends what you are gonna use.
Are you gonna use has_node() ?

This line is not necessary

But isn’t has_node() for checking child nodes? (sorry i’m a bit lost)
So i could check the parent’s node and see if is has the node

Ok let me give you a proper example

Var targetPath 
func _ready():
 targetPath = TargetNode.get_path()

func hurt():
 if has_node(targetPath):
    # rest of the code
1 Like

Not necessarily

1 Like

It worked thanks so much, i was trying to fix this bug in my game for 2 days! thank you, and sorry if i wasted your time by being dumb :sweat_smile:

I’m sorry too, took me way to long to properly explain

1 Like

I had this. Don’t check for null as it won’t work with freed nodes: those exist in limbo for a moment. use is_instance_valid(node) instead

See also this

2 Likes

@TheOneAvailable Remember the SceneTree and how callbacks and notifications are sent is important to keep in mind.(see quoted blocks below)

A few others that I have used in various ways for your use case (depending):

Spend a day trying things out on these 2 MOST IMPORTANT pages, and you’ll save years of headache:

  1. The entire single page on Class → Node
  2. All of the sections in → Best Practices

Scene tree: The SceneTree contains the active tree of nodes. When a node is added to the scene tree, it receives the NOTIFICATION_ENTER_TREE notification and its _enter_tree callback is triggered. Child nodes are always added after their parent node, i.e. the _enter_tree callback of a parent node will be triggered before its child’s.

Once all nodes have been added in the scene tree, they receive the NOTIFICATION_READY notification and their respective _ready callbacks are triggered. For groups of nodes, the _ready callback is called in reverse order, starting with the children and moving up to the parent nodes.

This means that when adding a node to the scene tree, the following order will be used for the callbacks: _enter_tree of the parent, _enter_tree of the children, _ready of the children and finally _ready of the parent (recursively for the entire scene tree).

1 Like

This is a way better solution, thank you

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.