Switch Statements to the Rescue

Multiple If Else Statements

James Lafritz
4 min readApr 8, 2021

Finding your self writing If Else If … Else to program the logic of your game?

You can use a Switch Statement to select one of many code blocks.

The way this works is

  • The Switch expression is Evaluated once
  • The values of the switch are compared to the values of each case, meaning are equal to.
  • If their is a match the block of code is executed
  • When executing the block of code once the break keyword is reached it exits the switch block
  • The break key word is how to have multiple cases exicute the same block of code.
  • The default keyword is what to do if their is no match

In C# 6 and earlier the expression must be of

In C# 7.0 and above the expression can be any non null type

I tend to stick to ENUM type. Depending on the integrated development environment (IDE) you use it may have a way to auto create cases for ENUM type.

The other advantages of using ENUM is

  • the variable has a more meaningful description then int.
  • strings are easy to miss type and have to match exact. there is a difference between “top down”, “TOp Down”, Top_Down”, and “TD”

To learn more about the switch statement and pattern matching see the Microsoft Docs.

Updating the Get Position to Switch

I use Rider as my IDE so all I have to do is select the context menu and select convert to switch. The context menu even tells me that I am missing a Case for InputSystem when I selected generate switch labels from the context menu. I deleted it as I will use the default case. I also changed the default case to return the bounds center.

Using a switch statement to determine the Move Direction for Movable Behavior

Get Direction

I need a Method to return the move direction to use. So in the Position Helper Class I made a public static Method that returns a Vector3 and takes in the Game Move Direction Enum. I started Typing Switch and used the features of my IDE to complete the switch statement.

Now I have to decide which direction to return

Now I like to use Expressions when I can in my code. You can use a switch expression instead of a switch statement. The main difference is you do not need the keyword case match: you use match => , and to get a default case you use _ =>.

Update Moveable to Use the new Game Move Direction instead of Vector3 move direction

I added a Game Move Direction Reference variable, change the tool tip for the move direction and set the move direction in the awake method.

Then I updated all my prefabs that had a moveable component attached to them to reflect this change.

The Game In Action

Changing the Game Move Direction now completely changes the way the game behaves. Of course if you wanted to change the game direction you might want to make other adjustments like the game assets look, the player screen bounds, and the laser offsets to match.

--

--

James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.