Time for action – Using switches
Let's take a look at an example of a Switch statement.
var int Int1; function PostBeginPlay() { Int1 = 2; switch(Int1) { case 1: 'log("Int1 == 1"); case 2: 'log("Int1 == 2"); case 3: 'log("Int1 == 3"); default: 'log("Int1 isn't any of those!"); } }
Running the code, the log looks like this:
[0007.97] ScriptLog: Int1 == 2 [0007.97] ScriptLog: Int1 == 3 [0007.97] ScriptLog: Int1 isn't any of those!
What just happened?
Why did the other lines log? Unlike if/else statements, switches will continue executing the next steps after the condition is met. Sometimes we'll want it to do that, but if not we can use the break statement here too.
var int Int1; function PostBeginPlay() { Int1 = 2; switch(Int1) { case 1: 'log("Int1 == 1"); break; case 2: 'log("Int1 == 2"); break; case 3: ...