Hi,
Could you please re-explain why the event Unit.OnAnyActionPointsChanged
is static?
(I have watched both videos several times and I still don’t understand)
Thanks!
Hi,
Could you please re-explain why the event Unit.OnAnyActionPointsChanged
is static?
(I have watched both videos several times and I still don’t understand)
Thanks!
By being static that event belongs to the class itself an not any specific instance (Unit)
So for a listener to listen to that event, they don’t need a reference to any specific Unit, they can access the event through the class which then fires that same event on every Unit.
Basically it’s just an easy way to listen to when something happens to every single unit, instead of having to cycle through the entire Unit list and listen to each individual non-static event.
As a follow up and handy tip:
When using an EventHandler, we’re always passing the calling object… in this case a Unit…
This means that if you want to know exactly which Unit had a change on the OnAnyActionPointsChanged, you simply have to cast the sender as a Unit.
For example:
void Unit_OnAnyActionPointsChanged(object sender, EventArgs args)
{
Unit unit = sender as Unit;
Debug.Log($"{unit}'s action points have changed.");
}
Thank you both!