Fixing A Game Breaking Bug or How I Learned to Love The Observer Pattern

Fixing A Game Breaking Bug or How I Learned to Love The Observer Pattern

By Pedro Colon 

Wooooo boy, this took a bit. Technically-as the time of this writing-I am not completely done with fixing the game breaking bug BUT for the most part the system is working as intended (for the most part), just need to start placing it around the current tracks we have for the game. So what was-

The Problem!

The problem is honestly quite simple. When the player starts, they are able to turn back and hit the lap three times to end the race in seconds. This is obviously not great for a kart racing game as this would just be game breaking as seen below:




This is bad, like just bad. Absolutely unfun and unsatisfying for everyone involved. 
So now the question is, how did I manage to get the bug fixed? The answer is quite simple: A design pattern known as the Observer Pattern.

Observer Pattern is The Best!

I am not kidding when I say that the observer pattern is awesome. I remember in one of my college courses we went over design patterns. In the back of my head while I was trying to figure out a solution, I was thinking of what kind of design could fix this issue. And then it hit me: Observer Pattern! Since this will mainly be event driven, it was the perfect opportunity to apply the Observer pattern to keep track of major gameplay events throughout the life-time of the game. And thankfully, it fit like a glove! Creating a observer class to observe events and handle them has helped a ton. Instead of running every update, it only needs to run when it has to. And with these events, allowed a greater control of the code flow, where one thing would flow onto another. And it is because of this pattern, it allowed me to turn what was flawed into something more secure, as shown here:



While it may be a bit hard to notice, now with checks in place with events and adding a bit of code to our kart collision class, we are able to do checks where if the player hasn't hit every checkpoint prior they cannot hit the lap AND that they can't hit the checkpoint repeatedly. A much nicer way and more fulfilling way to play the game. Though as you can see, this is only setup at the sandbox at the moment and that is mainly because I am currently testing it and from the test, there are some things to work out (such as making sure this can work between other players/AIs). But in regards to it being game-breaking, it is at a much better state now than ever before! 

Screenshots to show things being tracked with the added changes:





Some of this might be a bit redundant, but this will be refactored once all the pieces fit.



So moral of the story? Observer Patterns work and so does design patterns. However, do not try to start the project with thinking of a design pattern in mind, I would say. Or rather, don't get hung up on design patterns so early in development as it would make you suffer from analysis paralysis. And even then, you will probably make use of more than one design pattern. As of currently, I am making use of two: Singleton and Observer. Will they be more added? Hard to say at the moment but best to keep in mind just in case, a new problem occurs on which the best solution requires a different design pattern from the one we are currently using. And honestly that is key here: Design Patterns are here to help solve problems. Do not be dictated by the tool, instead use the tool when needed.




Side note: Here is a link from unity that helped me refreshed on how to implement the observer pattern. Remember that design patterns are language and engine agnostic so you can use them for any game engine and any coding language. https://unity.com/how-to/create-modular-and-maintainable-code-observer-pattern#simple-subject-code


Comments

Popular posts from this blog

The Pitfalls of Crafting a Position Placement and Lap Counter for Kart Racing Games

Troubleshooting Unity GameManager Input Handling

Only one line of code