#Call function on parent from deep in the node tree

7 messages · Page 1 of 1 (latest)

faint oasis
#

I have resource structure: Galaxy - Planets - Buildings
For these things I have UI containers system like: Galaxy UI containers which generate Planets UI containers which generate Buildings UI panels for each building. This whole galaxy structure is dynamic so one galaxy may have different planets, and different planets may have different buildings.
Player can interact with buildings (build new, upgrade current e.t.c). What is the best way to set communication between building node with galaxy and planet node? Like if player choose to construct new building, some variables should be also changed in Planet and Galaxy accordingly.
The way I see to implement this is to create and set variables like of_galaxy and of_planet in Planet and Building script, store self in it whenever parent creates that child and then use that variable to call that parent functions cascade way when something happens deep inside.
But I have doubts that this is a good way.

shut prism
#

in general, i recommend using signals to have deep children affect parent. whenever a building or whatever is created by the player, you could connect a signal from the building to a function in the parent. then, you can just emit the signal and not worry how deep in the scene tree anything is. thoughts?

faint oasis
#

oh, indeed, forgot about signals... gonna try this way

faint oasis
#

ok, I found a problem using signals for this
since to connect a signal I need to give proper path to parent of what that signal should activate, I still need to give child path to that parent, and connecting signal makes no sense because I can just call function right by path_to_parent.function()
I just gonna use global signaling routing via singletone, think this is the good way to do something like this

shut prism
#

you might also be able to use owner to reference the root of the tree directly?

faint oasis
#

whole tree may become more complex so owners may be different and not work properly

true jetty