I understand that the best practice is to separate the UI code from the logic.
In my case, I have a class (money) that keeps track of the amount of cash the user has. The class also containts the “AddMoney” method that others methods will call whenever they need to update the amount of money the user has. That amount is then displayed as text in the UI.
public class Money : MonoBehaviour {
public int money;
private int _money
{
get
{
return _money;
}
set
{
_money = value;
}
}
public void AddMoney(int amount)
{
money += amount;
}
}
Now, the logical thing to do - for me, anyway - would be to add a line to the AddMoney method that would update the UI text in question with the current value of the money variable - for example, something like this:
However, wouldn’t doing so mean updating the UI from within what is essentially a logic class and contradict the goal of separating the two?
Of course, I could probably create something like an UpdateLabel component that will essentially rely on the Update function to update the text every frame. While generic, it seems like, intuitively, this is a rather inefficient way of doing things, since I’d be basically refreshing the contents of the label every frame regardless of whether they changed. In contract, relying on the AddMoney function would ensure that the contents are updated only when such an update is necessary (e.g., the underlying value has changed).
Thank you! I looked into it and got it working, and learned something new in the process (up until now, I steered clear of delegates and events).
I do have a question about events, however, and I’m sorry if it’s a philosophical one - is using Events generally a good practice? To me, intuitively, it sounds a little bit like, well, magic - something that’s able to look through the whole solution, find the listener and the trigger, match them up, and do whatever it is supposed to do.
It’s seems to resemble the difference between GetComponentInChildren() and FindObjectOfType - both produce the desired result (well, assuming that T is a singleton), but the latter is “magical” in that it looks through the entire solution and therefore is terribly inefficient as a result. In contrast, GetComponentInChildren looks only within the hierarchy and thus, while more limited, has less of a performance overhead.
Now, I know what they say - don’t optimize around problems you haven’t encountered. However, I am curious whether relying on Events going forward (especially given my intuitive preference for decoupled component-based solutions) isn’t something that won’t come around to bite me later.
Like anything else, it’s a tool, and it’s good practice when it’s the right tool for the right job. Obviously this is a little tautological, but… best I can do. In this scenario, though, yes, it’s a good practice for sure.
Quite the contrary, the listening script seeks out the event and lets the event know where to find it when you do the += part of the event code. So your OnAddedMoney event has a list of these four specific function on these four listener objects - it just runs through and calls those four. If you have 10,000 other GameObjects in the scene [that don’t listen for the money event], invoking OnAddedMoney will incur zero additional cost (unlike findObjectOfType, which will of course get slower and slower as you add objects).
Brilliant, this really clears this up! I guess going forward I’ll rely on Events more whenever communication between components is required.
Up until now, avoiding Events forced me to basically construct huge hierarchies of things just so I could reliably use GetComponentInChildren and to go completely nuts referencing three or four scripts in every other script just so I could do something that an Event could accomplish.
Needless to say, this translated into extensive use of public variables, interdependencies, and, most crucially for me, made a component-based approach impossible, as I could not rely on locating the right component to communicate with other than through FindObjectOfType (which, to me, is a last resort and not something I want to build an entire solution around).