0 votes

In the game I am developing, I am trying to understand and decide the best way for certain objects to share information with each other. For example, the player script which handles and calculates player health needs to share its data with the player's GUI to update the value property of their health bar (TextureProgress).

For the given scenario, would it be better to...

A: On the player script, assign a variable for the GUI script so that the player script can directly modify the GUI by doing "gui.healthbar.value = newvalue"

OR

B: Connect a signal from the player script to the gui script so that the player script does not need to assign a permanent variable for the GUI and can instead simply do "emitsignal("updatehealth", new_health)"

Is there a major difference in efficiency between these two methods, or are they essentially the same? This issue is generalizable throughout much of the game so I figured it might be important to know which way is better.

Thank you!

Godot version 3.2.3.stable.mono.official
in Engine by (12 points)

2 Answers

0 votes

For all intensive purposes these should be essentially the same. To my knowledge, signals are reasonably efficient; but more than that, they're convenient and fun.

The benefit of a signal is it is made to only be called when it is needed.

For example, in option A, the GUI script must be constantly reading the variable to be ready for changes. (Albeit this should have no real performance cost in this case given the insignificance of the size of the data).

Option B, on the other hand, would not require the GUI script to constantly read that data. Instead the data would be updated only when a function is activated in the player script that could change the health of the player (the function connected to the signal).

Another interesting option to consider is a global script, or singleton. These set up data that can be simultaneously accesses by any node's script. You can read more about that very simple process here.

by (155 points)
0 votes

The signal should be more efficient because your publisher code does not have to pause to update a UI and especially if you have multiple subscribers/listeners.

Using signals has other advantages.

If you make changes to the subscriber, e.g. the GUI. you don't have to change the game code (the publisher).

Also your code is cleaner overall because game logic is separate from the UI.

by (719 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.