Switch Statements to the Rescue- Game Dev Series 19

Objective: Upgrade the modular from “if” to “switch”.

Previous: Creating Modular Powerups System

Although we have created a modular, the code is so nasty. It almost check every line when we collide with a powerup. What if we have tons of powerup, it would be completely nightmare in an if statement. Since we have powerup ID for our powerups, there is an efficient way to identify objects directly.

We can use switch statement instead of an if statement.

In switch statement, we can send in the value we want our switch to verify. Sort these value in separate cases and always end it with a break.
And there can also have a default to deal with the value is not listing in the cases, just like else in if statement.

As our modular, it can be changed like this,

In this statement, we simply told our Powerup script that if the ID is 0, switch to triple shot; if it is 1, give it a speed boost, and so on.
Switch statement will check the value we have imported and switch to the right one.

Then we should have the same result when we play it.

--

--

--

A passionate gamer whose goal is to work in video game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How businesses can utilize Client Dynamic to manage their social media presence

Python Data Validation

BigQuery Hack: How to Quickly Populate a Table Without INSERT()

Black wireframe glasses looking at code.

The most hated code comments types

Move over Python Julia is the AI Engineer’s new language!

district0x Network Roadmap 2020

Explore the Top Pros & Cons of Laravel for an Astounding Business Growth

Bridgeswap

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Adding a thruster speed boost on keypress

Learn to code by making a 2D space shooter game.

Create A White Noise Shader With Shader Graph In Unity

Draggable UI In Unity