I know, there is get_node("../Player") but that's such an ugly way to program, not flexible at all. In unity they have: player = gameObject.FindPlayerWithTag("Player") and no matter where in the hierarchy, the script always finds the player... is there no similar method in Godot?
Get player position?
- Edited
You can use find_node()
, but keep in mind this is a potentially fragile method that is slower than get_node()
. In particular, avoid using it every frame – cache its result into a variable in _ready()
instead (or use onready
).
It's likely a better idea to keep using get_node
or its $
shorthand ($"../Player"
), but cache the result into a variable at the top of your script using onready var player = $"../Player"
.
I'm just learning Godot still, but they have a saying get nodes down, but signal up. So you put a connection to a signal in the Player script if the thing using it is on the same level of the tree or higher. http://kidscancode.org/godot_recipes/basics/node_communication/
a year later
Megalomaniak added the Godot Help tag .