Signal architecture between UI and interactable objects

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maxic

Hi everyone.

I have a question about working with signals. In my main scene, I have a ‘Control’ (Node2D) node, which is a parent to all object who have interaction (coins, the player, etc). And a UI node, which is the parent of all UI elements.

It seems reasonable to decouple these two as much as possible, and only let them interact with signals. So I made the following setup:

The starry things is where a script on that node emits a signal. In this way, the ‘Control’ node needs to know the structure of it’s children, but the UI node shouldn’t care. I like this setup, since control and UI are decoupled quite nicely.

However… In this case, an event from the ‘Coin’ node needs to be emitted 3 times, before the data ends up in the UI node. That seems like a lot…

My question is:

Is this an OK way to do this? Is it lacking in performance, or am I maybe overthinking things?

Thanks in advance for your help.

:bust_in_silhouette: Reply From: Lopy

You can have 50 000 functions calls in a tic without much issues (if they are fast), so 3 when a player collects a coin is not an issue. It might make stacktraces and the debugger slightly harder to read however.

You are overthinking things but it is not necessarily a bad thing. The biggest danger to your game is that you loose motivation, so having fun in coding is a consideration, and you seem to enjoy your layout.

Having your Node2D named Control know each of it’s children seems cumbersome. They could operate the connections of Control signals to their own, and modify variables in “Control” that emit signals on set (see setget).

Also consider that you an create signals on the fly with add_user_signal, and override _set and _get to create fake properties (as well as launching signals on set).

PS: Please, do not name a Node2D “Control”, this will confuse people. Consider using “Controller”, or “Model”.

You’re right, I should really change the name of that node, it’s very confusing atm. The setget suggestion would really help in this example, that’s kind of what I was looking for. Thanks!

maxic | 2021-04-27 09:02