Relative Node Paths
Asked Answered
C

2

0

Cantilena Don't use either. Never try to access parent nodes directly. Instead declare an export property in your child script and then in the Inpector panel assign the node that is somewhere up the tree.

@export var grid_container: GridContainer

But if you really want to do it: Use relative paths like in your first example, even if it looks ugly.

Cantilena @onready var grid = $"root/world/gui/inventory/GridContainer"

Begin the path with "/root/".

Cahra answered 3/12, 2023 at 11:24 Comment(0)
C
0

Cantilena It's to avoid tight coupling between components. Ideally your components further down the tree should have no knowledge of what is above the tree. One solution is to use signals to pass information upwards.

Cahra answered 3/12, 2023 at 13:46 Comment(0)
A
0

Cantilena If you see something like ../../.. in your node paths, it's a solid indicator that your project architecture is bad and you should rethink it, following the hints Robbirobbia laid out above.

Afford answered 3/12, 2023 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.