- Home /
Advice on reducing amount of direct referencing.
I want to cut down the amount of direct referencing I'm using in code and trying to get my head around using UnityEvents instead, so I can upgrade the functionality I've already created...
I've started with my stat system which uses a scriptable object for each stat. (This is so I can easily add and remove stats if I later change my mind). My MonoBehvaior StatManager script collects all the scriptable objects on runtime to generate an array of available stats. When I want to effect a stat I use a separate class called StatToMod. Objects that effect stats (I.E. Items) can have an array of StatToMod. My item object will then go through it's array of stats to mod and passes the values to StatManager which has an UpdateStats method. This checks if any match it's array or available stats, and if it does, then executes the UpdateStat method in the scriptable object.
To make sure I'm understanding this correctly in my head, if I wanted to change this to use a UnityEvent, would I need to create a class in StatManager called something like say, ModStats, that derives from UnityEvent and add the listener to the MonoBehvaiour StatManager script. Then in my item class, instead of directly referencing the StatManager script to access it's UpdateStats method, I 'invoke' the ModStats class, which when invoked, executes the UpdateStats method? Have I got that right? And if so, I don't understand the advantage of using UnityEvent over just turning StatManager into a singleton and using something like StatManager.UpdateStats(StatToMod[])? Any advice? I'm self taught off YouTube tutorials and I'm sure they're teaching me bad habits :/