Topic was automatically imported from the old Question2Answer platform.
Asked By
cardoso
Old Version
Published before Godot 3 was released.
Is there a way to get a node (from the current scene), if not knowing the exact node path?
Scene structure:
level
-- player
-- item
In the item script I want to access the player node
I tryed using find_node(), but aparently it’s not working.
If I use get_tree().get_root().get_node("level").find_node("player") it works,
but if I use get_tree().get_root().find_node("player") it does not.
If you want to find something on children, set owner to false in the parameters
find_node ( mask, true, false)
Keep in mind that find_node is slow, could be better to add the node to the “player” group and get it by searching groups (or even call_group if it is for something simple).
Also I’m pretty sure there is a better way to make item and player interact rather than creating globals, hardcoding paths or searching nodes.
Usually, collision detection and signals are a good fit for this, but it depends on your game.
Zylann | 2017-04-25 18:23
eons, thanks, it works!
cardoso | 2017-04-25 19:04
Zylann, actually, it’s because of using a signal that I needed to find the player.
I want to make the player and the item interact - when the player (body) enters the item (area) - so I use a signal. (And for my logic I need to know the item position in the signal method, which I pass a extra parameter.)
So I created a script for the item, where I connect() it with the player.
And I don’t want to have the player path harcoded there.
If you know a better way to organize this, I am open to know
cardoso | 2017-04-25 19:11
If you do this through collision, you can either send the collider to the collided using a function, and this way you won’t need to use get_node, find_node or signals at all ^^