Event System

Late in the project components were becoming tightly coupled and needing to communicate quite a bit, so, I decided to write an event system.

I wanted to try out the built in UnityEvent and UnityAction api and take a new (for me) approach, so I decided to do this in a way that I never had before.

The event systems messages are all type-safe, with the return value of the event defined by that Type. When the player scores points, a call is made to EventManager.Raise(arg) where arg is typeof(PointsScored). The PointsScored objects has a reference to the sending Monobehaviour and an int of the number of points that were scored.

public PointsScored(GameObject sender , int value){...}
public PlayerDamaged(GameObject sender , int value, Vector2 worldLocation){...}
public PlayerDeath(GameObject, Vector2 worldLocation){...}

This model lead to a lot of empty class declarations once base types were established, but its very clear what is going on when looking at the code.

Further under the hood, The EventManager is a singleton that maintains a Dictionary<Type,UnityAction<GameEvent>> of listeners where GameEvent is the base class from which all our custom events inherit.

Its a simple and typesafe approach to an event system, but I will likely use a different approach necxt time.