Switch Statements in Unreal Engine 4

Switch statements are a useful and universal programming feature that also is included in Unreal Engine 4 blueprints.

The use of switch nodes can replace large chains of branches in your code, making your blueprints cleaner and more organized.

In this guide we will be going over what Switch Statements are in Unreal Engine 4 and how they are used in engine.

Switch and Case

The switch statement takes in an input runs different code from that value.

The default value will run if the value doesn’t match.

In this example we will setup the simplest switch node; Switch on Int.

Setting up a Switch Statement

In Unreal Engine 4, new execution pins can be added by clicking on the node and looking in the details panel (on the right by default).

Adding Functionality

Once you have added the new pins we can attach blueprint nodes.

Switch on Int node

This blueprint code below will print different numbers on the screen based on the random integer.

If the random integer is 6 the code will output “Default”.

Demonstration

Examples of Other Switch Nodes

Switch on String

This node allows you to set the output pins with a string. The output pin will fire if the selection string matches the pin name.

Switch on String with your string as an output.

This can also be case sensitive.

Switch on Enum

This node fires different output pins based on the custom enum value of selection.

In this example, I created a new enum named “Example Enum”.

A common use for this node would be for switching between states in your game e.g. Game, Main Menu and Credits shown below.

We are working on a guide on Enums in Unreal Engine 4. It will be linked here on completion.

Download the Project Files

Conclusion

Now you can organize and optimize your blueprints with switch nodes!

Even though this is a simple topic, it is extremely important for improving your overall blueprint code quality.

Extra Reading:

Click here for more information on switch statements in Unreal Engine 4.

Click here for more information on flow control in Unreal Engine 4.

Leave a Reply

Your email address will not be published. Required fields are marked *