Passing/Accessing parent variable in child node?

Godot Version

v4.5.stable.steam [876b29033]

Question

Simple base question: Is there a way for a child node to easily access a parent node’s variable for reference purposes? Should I be exporting or using signals or something similar?

Within the game I’m attempting to make, I have a screen/scene that creates a paged list of what a player owns (6 things per page, with no direct limit on how many a player can own). To begin, the parent node takes the player’s list and gets its size/length to calculate number of pages needed in total.

After this, various child nodes use the parent’s variables to determine which item should be shown/accessed, and while I have it working at current I was wondering if there was a better way than I managed to come up with, as shown below:

Parent Node:

Child Node:


(Mentioned nodes of CatSprite and NameLabel proceed to use the ‘target’ variable to determine their displays. Additionally, the child is a Button that allows a player to select and further investigate ‘target’.)

Your way if doing it is fine, but if you use the parent more often its better to save it in a variable with @export for example

1 Like

Another alternative like you said would be using signal, where the child will emit it and the parent will listen. But that means the parent would need to handle the update. I guess, it just a matter of preferences.

Also gdscript style guide is to prefix a variable with _ i.e. var _total_pages: int to note it as private. So that the variable without underscore prefix is public which noted that it can be read or mutate by other object. This will make things easier to manage/debug.

1 Like