Previously, I worked on a script to create modular power-ups. The code uses conditional statements to control which type of power-up the player collects. If we decide to add more and more types of player upgrades, this code will get long and messy.
The switch statement is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.
To illustrate this definition better, take a look at the following syntax.
- This variable can be any non-null expression. I’m using an int for the example.
- The syntax for this statement begins with the keyword:
- The expression to be evaluated by the switch statement goes inside the parenthesis.
- Using the
casekeyword, we add potential candidates followed by a Colon (:). The candidates have to match the Type of expression that the switch statement is evaluating. When the evaluation finds a match, it executes the code block that follows it. Every case must end with a
defaultkeyword executes code if it does not find other matches.
Armed with this knowledge, I’ll make some changes to the power-up code I created previously. I will also add a third power-up called Shields.
Currently, my power-up behaves like this:
Now that I want to add a third option to my list, I could add another
else if() statement checking for if
PType == 2 . But when evaluating a single condition, the
switch() method is often a better choice.
To change our code, all we have to do is the following.
Using the same syntax I described earlier, we now check for the integer named PType and apply the correct behavior based on the result. I have included a default case that should help me debug any problems I might introduce to the code.
switch() methods are not great at nesting, unlike
if-else() statements. I, therefore, use a combination of both in this Script.
Hopefully, this gives you an idea of how this method can help you in optimizing your code. I should mention that you can add any number of lines inside of a
case : which will execute normally. In my Script, I only need to call the appropriate method for each power-up. However, I can add any number of other lines to my code I may require.
About the Shield Power-Up:
To complete the tutorial, I want to explain how I implemented the new Shield power-up.
In my Player Script, I have a
TakeDamage() method that subtracts points from my player’s lives.
I want my Shield power-up to provided immunity from one Collision. The first thing I do is declare a
bool named ShieldActive.
Then I create the
public ActivateShield method. This method is responsible for setting the value of ShieldActive to true.
Finally, in my
TakeDamage() method, I make the following changes.
First, I check to see if ActiveShield is true. If this is the case, I set ShieldActive back to false and use the
return keyword to exit the method without executing any other code. What this means is that the Player ignores the first Collision that is detected. Once ActiveShield is set to false, the Player will take damage as expected.
I can now create any number of Power-Ups that I can integrate easily into my code. I have three so far and have plans to add a few more down the line. Preparing your code for potential expansion is key to good coding practices and will help you speed up your development overall.