My journey becoming a Unity game developer: Creating Modular Powerup Systems. Powerups given unique ID numbers.
Objective: Using a modular programming technique to assign unique ID numbers for each powerup. This technique will allow the ID number activated to have unique code assigned to each powerup allowing for each powerup to have unique code written for them.
Modular programming is a software design technique that emphasizes dividing the functionality of a program into smaller sub-tasks, such that each contains everything necessary to execute only one aspect of the desired functionality. This breakdown enables us to view a complex problem in an easier, more understandable, and easily accessible program. This allows us to program smaller, simpler programs rather than one large complex program.
We began by setting up the Speed Boost powerup game object with the same settings applied to the Triple Shot prefab, including the PowerUps script. https://rhetthaynes66.medium.com/my-journey-becoming-a-unity-game-developer-introduction-to-physics-in-unity-47e39b47b154
Inside the PowerUps script, we created a new variable with a SerializeField that is Private with a Type INT named _powerupID. This variable will represent the modular part of our program as all powerups added to this game will be assigned ID numbers. These ID numbers will differentiate each powerup allowing us to assign code behavior for each powerup making them unique to one another if needed.
Inside the OnTriggerEnter2D under the IF player is not equal to NULL condition, we checked IF a certain powerupID number was activated. If powerupID number equal zero, we have the player’s TripleShotActive() method called to activate the Triple Shot. If powerupID number equal 1, we have the player’s Speed Boost activated. Since we haven’t setup the Speed Boost effects yet, we used a Debug.Log message signaling the Speed Boost PowerUp was collected. If powerupID number equal 2, we did the same thing for the Shield as we did for the Speed Boost.
Under the Project folder inside the Prefabs folder, we selected the Triple Shot PowerUp prefab and made sure the Powerup ID number assigned was zero in the PowerUps script component. Then, we selected the Speed Boost PowerUp prefab, and changed the Powerup ID number to 1. These powerups were assigned their ID numbers, therefore whatever ID number is activated will run the code inside their IF condition.
When we play the game, we notice that when the Triple Shot PowerUp is collected, the Triple Shot is activated for the player to use. On the other hand, when we collected the Speed Boost PowerUp, the Debug.Log message appeared at the bottom of the editor and inside the Console tab notifying us that we collected the Speed Boost PowerUp.
The variables we’re using in the PowerUps script are both modular as we can use different speeds for the powerups to move down the screen, as well as giving unique ID numbers for each powerup created in this game. Modular programming is probably used the most in functions or methods we use. These functions have code inside that we can use for a variety of things on different game objects without having to make any unique adjustments to the code for that object. Whenever we can optimize one set of code to be used by other objects to get the same results without having to write it out for each object, do it.