Update 1: C# Events and UI
Gold and Stress
For the past few days I've been implementing gold and stress into Dark Miasma. Gold is, as you would expect, how much money you have and is used for buying and crafting cards, training your stats, and relieving stress. You become more stressed by working and fighting, and if you reach maximum stress, you will collapse and be out of commission for a week.Money and stress are two very large factors to consider when deciding how you want to spend your week in Galeport, so we want to display those values openly when you're choosing your activities.
The question now is: How does my display know how much stress I have, and how does my display know when my stress changes?
How do does the UI know how much stress I have?
This one's pretty simple. Since Dark Miasma is a single player game, I use a singleton to track my player. Being developed in Unity, the Player is currently a GameObject that triggers DoNotDestroyOnLoad, and the Player class keeps a static reference to the instance of Player. I've been leaning towards changing Player from a Unity singleton to a pure C# singleton though, since the Player object currently does not need to leverage any features of Unity GameObjects.
But anyway, the gold and stress UI displays know where to find their respective values.
So when do we update these values?
Well, we could check every frame! But since player stress and gold only change occasionally, the number of times the UI would update itself with redundant information could lead to performance issues.
We could have the Player call UI update functions directly. This would mean that the Player needs to know what UI elements are active, which means if we switch scenes we need to update the Player wire any references... Yeah no. I'm not a fan of trying to find objects in separate scene hierarchies.
But having the player let the UI know when stress and gold update, now that's not a bad idea on its own. As long as the UI elements tell the Player that they want updates, not the other way around. After all, as a singleton, everything in the game has access to Player, for better or for worse.
Actions
Coding nearly entirely in C#, I searched for C# solutions and found what I was looking for. System.Actions. I hadn't heard about Actions before, being relatively new to C#, and was excited to learn that they worked similarly to subscription based callback logic I've had experience with in the past. In a nutshell, if I put an Action in the Player object, I can subscribe to that Action by passing it a function handler, or delegate. Then whenever Player calls the Invoke() function on the Action, every subscriber registered with the Action will execute its delegate function.
So now, when the UI elements for displaying Gold and Stress are enabled and loaded into the scene, they subscribe to these Player Actions, and whenever they are disabled they unsubscribe (this is really important in the long run!). Then, whenever the Player updates their gold or stress, they Invoke the appropriate actions, causing the UI to update. No checking every frame, no hassle. The UI simply updates when its relevant information is updated.
C# is slowly growing on me. As I use it, I learn more of its features and tools. I still haven't figured out how to properly use LINQ yet, but that's a feature that excites me quite a bit. When I come across anything else in C# that I find particularly cool, I'll be sure to post it here!
So now, when the UI elements for displaying Gold and Stress are enabled and loaded into the scene, they subscribe to these Player Actions, and whenever they are disabled they unsubscribe (this is really important in the long run!). Then, whenever the Player updates their gold or stress, they Invoke the appropriate actions, causing the UI to update. No checking every frame, no hassle. The UI simply updates when its relevant information is updated.
C# is slowly growing on me. As I use it, I learn more of its features and tools. I still haven't figured out how to properly use LINQ yet, but that's a feature that excites me quite a bit. When I come across anything else in C# that I find particularly cool, I'll be sure to post it here!
Comments
Post a Comment