Observer pattern — How to use this crucial design pattern in game development with Unity

GMGStudio
3 min readOct 17, 2021

--

The observer pattern is used by many developers because it solves so many problems. The observer pattern is used by so many people that C# put it directly in the language with the event keyword. We can solve many problems and one of the best examples are probably achievements. Achievements have to watch for all kind of things happening in your game.

We have also made a YouTube video about the observer pattern: YouTube Video and make every week videos about game development, game design or keep you up to date about our journey.

Observe 👀

When I started with game development I had no idea about design patterns and why or how to use them. I just wanted to solve problems but I realized that I can’t be the only person having this problem. And this is why I use started to use them: To solve problems! And one problem I had many times is that something happens and many classes care about this event. I will use the source code from Catch Every Fruit as example. If you are interested and want to know how to develop, monetize and release a whole mobile game for iOS and Android you can watch our Playlist on YouTube. 🙂

Here we have a LevelManager which when a Fruit was caught

But actually our LevelManager doesn’t care about the Score or AudioManager. I just caught a Fruit and made a point. The problem is when even more things happen after scoring a point or catching the wrong fruit. The LevelManager shouldn’t call the other Managers. They should react to scored points and the solution for this is the observer pattern. Let’s refactor.

First let’s make two events. One when we scored and one if we die. These will be our two events that other classes can watch. And we need to invoke the events if needed. This is how our refactored code would look like

This means that everyone who subscribed to the event Scored or Died will get notified when we call Scored.Invoke() or Died.Invoke() and now the LevelManager can do what it is supposed to do: Manage the level, not calling other Managers to do something. But we still need to handle the events. Here is how:

Usually you subscribe and unsubscribe to events in the methods OnEnable and OnDisable. And this is what we are doing here. We are saying the AudioManager that whenever the Scored event is fired on the LevelManager it has to call the private void PlayScoredSound().

On the ScoreManager and the StateMachine it looks similar.

And this is a simple example on how to use the observer pattern.

If you liked this article feel free to give it a clap 👏🏻 It helps me and other people who don’t know if they should read this article.

We are GMGStudio and develop mobile games, make game development tutorials on YouTube — Feel free to subscribe to our YouTube Channel and checkout our Website

Sources
[1]: Reddit Post from u/munificent
[2]: Microsoft — Observer Design Pattern
[3]: Observer Pattern in Unity from ACDev

--

--