Hi, I just recently figured out how delegates and events work and I decided to give them a go. However I got confused again.
public delegate float Scored();
public static event Scored OnScore;
I’m having several issues regarding the implementation.
Should the event be in an EventManager script or should I stick this code in the Items that yield the score? How does the data get passed between the scripts? I want when an item is collected, the score tracking script to get the score float.
But as far as I understand the float return type doesn’t do anything it’s just the method signature.
public delegate void Scored(float score);
public static event Scored OnScored;
This way it seems that I will have to pass the score float, but since firing the event has to occur from this script, that would mean that if I subscribe with my items I will be sending the score from the EventManager to the Item, but it has to go the other way around.
Your delegate should accept arguments that will modify the score.
In the simplest case, that would be a single float or int: how much should the score increase by?
In more complex cases, that might include other information. Maybe an enumerator indicates why the score is changing. Maybe multipliers can apply. Maybe you need to know who’s score should increase, or who increased a score. Maybe you need to pass effect information for the UI to consume. Who knows?
Point is, I’d recommend something closer to this:
public delegate void IncreaseScoreDelegate(float amount);
public static event IncreaseScoreDelegate IncreaseScore;
All of this accomplishes separation of concerns. Your scoring objects shouldn’t need to know how the score system is implemented; they’re just sending messages that represent desired score changes.