#Understanding Node paths and Unique Names

4 messages · Page 1 of 1 (latest)

gaunt leaf
#

I'm having issues pointing to my Camera2D node using the following code. For all the lines below (only one is uncommented at a time), either the Slimes or the MapBottom are able to successfully find the Camera2D node, but no line below works for both of them.

@onready var camera_2d: Camera2D = $"../../Player/Camera2D" # works for Slimes
@onready var camera_2d: Camera2D = $"../Player/Camera2D" # works for MapBottom
@onready var camera_2d: Camera2D = %Player/Camera2D # works for MapBottom
@onready var camera_2d: Camera2D = %Camera2D # works for MapBottom

My MapBottom node and the two Slime nodes both use my killzone script. Given the relative locations of the MapBottom/Slimes to the Camera2D, I think I understand the behavior of the top two lines: the Slimes and the MapBottom are in different levels of the hierarchy, so if I use a relative path, the script will only work for nodes that are at a single specific level of the hierarchy.

However, I don't understand the bottom 2 lines. Why would the unique name only work for MapBottom?

west timber
lyric gyro
#

If you need a bunch of different instantiated scenes to access your Camera2D, I recommend you add it to a Global Script as a variable. That way you wont need to rely on hardcoded node paths (which wont work anymore if you change the hierarchy of your scene tree)

wind pollen
#

also look into signals for inter node communication