- Observer Pattern -
The objective of this assignment was to create a mini-game with Unity and C# that uses an implementation of the observer pattern.
Play the Game
- Event System -
Utilizing the Observer Pattern
Questions and Answers
1. What does the player do in the game that sends data from your Subject class to all of your Observer classes (with the observer design pattern) while the game is running?
The player pushes around Puffles in an attempt to get them into the bucket. If the player misses a Puffle and it goes out of bounds, they will lose a life. If the player successfully pushes a Puffle into the bucket, they will score a point. Each of these actions notifies Observers to the change in lives and score respectively.
Additionally, the GameManager notifies any Observers who are subscribed to the TutorialStart and GameStart events. This helps the UIManager update UI, specifically hiding title screen UI elements. Also, when the game starts, the SpawnManager is informed so it can start spawning Puffles.
When the player runs out of lives, the subject PlayerLives notified observers that the game has been lost. This updates UI and makes sure the player cannot score any more points.
When a Puffle enters the bucket trigger, the subject, BucketTrigger, notified observers that the player has scored a point.
2. What data does the Subject send to the Observers, and what do the Observers do with that data?
Data that is sent to observers includes score and lives. The UIManager uses the score and lives to update the UI with the appropriate score and lives the player has. These are the only two scenarios in which data is sent to observers. Other than that, the Observers heavily rely on changes in game state, such as game start and game win/loss.
3. When and how are Observers registered, subscribed, or added?
Observers subscribe in the Awake method and unsubscribe in the OnDisable method. I have a static EventManager class that helps reduce the number of times I call the GetComponent or FindObjectOfType method. Instead, I could subscribe to an event remotely and invoke events as well all without having to get any components or find any objects.
4. What were the benefits of using the Observer Pattern to make your mini-game?
The Observer Pattern really helped me keep my code decoupled. I noticed how modular I can make my scripts with this design pattern. I only need to subscribe to events! It’s also a nice alternative to Singletons.
5. Did you find any drawbacks to using the Observer Pattern? If so, what were they?I did notice that if I wanted to have something stop after the game over, each Observer needed to have a member variable such as gameOver, which is false on start, then I would subscribe it to the OnGameWin and OnGameLoss events, which would set that variable to true and any method I didn’t want running after the game ended would stop with a simple if statement. I found having to create a gameOver member variable in each of these classes and subscribing to the game end events quite repetitive. I see where a Singleton would come in handy here; I could just check if(GameManager.instance.gameOver) and be done! Instead, I had to rely on member variables and functions dedicated to changing them.
6. What is the player’s goal in your mini-game and what makes it challenging?
The goal is to corral 30 Puffles into the bucket. It is challenging because many Puffles spawn, and they all want to go in their own direction! The player has to make sure to keep focused and push the Puffles into the bucket without letting any escape.
7. How does the game communicate its goal(s) to the player?
There is a brief tutorial at the start of the game that displays the controls and goal of the game. There are also animated UI elements to give feedback to the player.
8. How can the player fail at the game and how does the game detect it?
The player fails if they let 3 Puffles escape (go out of bounds). The game keeps track of how many Puffles have escaped (the ‘lives’ the player has) and lets the player know when one got away.
9. How does the game give players feedback about how well they are doing?
Animated UI elements provide feedback to the player. When the player scores, the current score slides up from inside the bucket. When the player loses a life (a Puffle escapes), a heart with the number of lives remaining inside of it slides up from the lower left of the screen.