Like Legos!

Creating Modular Powerup Systems

Christopher West
3 min readApr 20, 2021
Photo by Ryan Quintal on Unsplash

Yesterday we discussed some tuning of our game and how long our powerups should last. Today we’re adding a new powerup! So far we have our triple shot powerup. Now we want to add a speed boost powerup. It’s likely that we will want to make more powerups in the future as well. We don’t want to reinvent (or worse copy and paste) the powerup collectible behavior every time we create a new power up, so let’s modularize our powerup script!

To do this we will want to add an identifier of some type so that we know which type of powerup we are working with. We could use a string or and integer to identify our powerup but strings can easily be mistyped and cause a bug finding easter egg hunt to occur and integers don’t inherently convey what they stand for and make the code harder to read. In C#, and most C-like languages, we have a structure known as an enum. In C# an enum acts as an integer value with a typed name attached, which resolves both our issues above. You may have noticed that I already created an enum in my last article for our powerup typing system. I already knew that we were going to have more than one powerup and had already started to modularize our script. Our enum for Shields and the Triple Shot is depicted below.

You may have also noticed that instead of an if statement in our ActivatePowerup method on the Player class I used a switch statement, in this case with just one clause for the Triple Shot powerup. This was also a bit of foreshadowing because I knew in advance that I was going to have multiple powerups to deal with. Here is our ActivatePowerup method with the new powerup added to it.

being able to modularize our method by passing in an enum type really makes extending our powerup system fairly easy. There is one more benefit to using an enum over an integer or string. Unity’s editor knows that there are a number of readable values in an enum and displays them as a dropdown in the editor with the readable names! That is much nicer and more understandable for designers in the future!

Next Time!

That’s all for today. Tomorrow, we will look at adding a third powerup, Shields! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.