How do a child check if his parent exist?

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

To do this:

onready var WorldNode = Node

    If get_parent().exists(): #How to do dis?
        WorldNode = get_parent().get_node("WORLD") 
        #This path will be a standard for all my maps.

Because I would be able to test scenes separated, without having to load the main maps. Using export path vars does work, but it requires you to manually load in the inspector, also tried get_owner but didn’t achieved much in that sense of finding if a parent exist.

The map structure: Imgur: The magic of the Internet

I would be able to test scenes separated

What do you mean by this? Have you made a series of levels that lead on from each other?
The question doesn’t really make sense because to use get_parent() you need a child, if the parent doesn’t exist the child won’t exist either.

Magso | 2019-12-09 14:09

Oh I see what you mean, the real name of the question should have been, how a node check if he has a parent?

Well, I have a UI Node which is saved as a separate scene, it needs to know what node is the WORLD, to drop items you selected in the inventory.

This works if I load my main map and use direct path to this node, my map scene is structured like so :

Imgur: The magic of the Internet

The node in question is UI_Master and the node which I drop items is WORLD/ITEM

The_Black_Chess_King | 2019-12-09 14:14

Hmm, I think by design my logic is wrong, what I need is to pass a signal to the World, and he will create the items, will work much better and the UI_Master doesn’t need to know who World is.

Still, is it possible to check if a node has a parent?

The_Black_Chess_King | 2019-12-09 14:40

The nodes will always have a parent, Godot basically works like this, get_tree().get_root().Top node in the scene aka get_owner(). You just need to reference the correct node to then add the items to it.

Magso | 2019-12-09 15:54

:bust_in_silhouette: Reply From: selamba

If a node has a parent, then it HAS to exist. If the parent node doesn’t exist, then all of that node’s children also don’t exist.
The only scenario where a node doesn’t have a parent is when we’re talking about the viewport, which isn’t really a node to begin with. Try launching your scene and then in the “scene” tab of Godot switch to “Remote” from “Local”. You’ll see what I mean then.

Indeed it helped me looking at Remote tab, I can now understand how my question doesn’t make sense.

The_Black_Chess_King | 2019-12-10 12:51

:bust_in_silhouette: Reply From: The_Black_Chess_King

Thanks for the @Magso and @selamba, I got the code I was wanting, but maybe I didn’t explained properly to begin with, my bad:

if get_tree().get_root().has_node("MAP"):

With this line I can check if a node is playing in the map, or I am just running the scene alone, thanks guys!