0 votes

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: https://imgur.com/a/ChzGvAc

in Engine by (376 points)
edited by

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.

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 :


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

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 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.

2 Answers

0 votes
Best answer

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!

by (376 points)
+1 vote

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.

by (141 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.