Flow Syntax
Basics
There are two basic flow control functions in UnrealScript, GoTo and If...Else.
Any other functions and loops described here (except ForEach) could be replaced by combinations of these two functions.
If a loop containes only one line of code the { } are optional. A single loop, If or Switch statement is treated as one command in this context.
Example:
If ( bExecute ) For (i = 0; i < 5; i++) { a += i; b *= i; }
Special Flow Commands
Return expression
The return keyword terminates execution of the function in which it appears and returns control (and the value of expression if given) to the calling function or state.
If the function returns a certain type of value the return value has to be specified in the return command. If a function with a return value ends without return it will return a null value for its data type. (e.g. 0
, empty string, None
, etc.)
Break
The break keyword terminates the smallest enclosing do, for, foreach, switch or while statement in which it appears. Code execution continues with the next line of code after the loop.
Continue
The continue keyword passes control to the next iteration of the smallest enclosing do, for, foreach or while statement in which it appears.
Example:
While (i < 10) { if ( i % 2 == 0 ) Continue; log(i); }
Stop
Imediately stops execution of State code. This is like the return command for states.
Assert (expression)
If the expression passed to Assert evaluates to False, the game shuts down with an error message telling you the file (class) and the line the Assert call is in.
Example:
Assert(numPlayers > 0);
GoTo (label)
Continues code execution at the specified label. GoTo is mainly used in state code, but is also allowed within functions, although it is rarely used there. For state flow there's also GotoState( Statename, label): see state for more on this.
Syntax:
GoTo('label');
Example:
State Idle { Begin: PlayIdleAnim(); FinishAnim(); Sleep(0.5); GoTo('Begin'); }
If Statements
If statements execute some code if a certain condition is true. The optional Else part executes some other code if the condition is false. Multiple If...Else statements can be combined to create If...ElseIf...Else statements.
Syntax:
// single line if: if ( condition ) // statement // simple block if: if ( condition ) { ... } // if...else: if ( condition ) { ... } else { ... }
Example:
(The second If is the command executed if the first condition is false.)
If ( Pawn(Owner) != None && Pawn(Owner).bIsPlayer ) { log(Pawn(Owner).PlayerReplicationInfo.PlayerName); Pawn(Owner).PlayerReplicationInfo.Score += 2; } Else If ( Pawn(Owner) != None ) Owner.Destroy(); Else log("No Pawn.");
Switch Statements
Switch statements can be used to check if a certain expression is set to one of several values.
Syntax:
Switch ( expression ) { Case value1: ... break; Case value2: Case value3: ... break; Default: ... break; }
In this example a variable i is checked. If its value is 1 the DoSomething function is called and code execution continues below the Switch statement. If its value is 2 or 3 both DoSomethingElse and DoSomethingMore are called and the current function is canceled (due to the return command). If the value of i is 4 only the DoSomethingMore function is called and the current function is canceled. If i has a different value the DoNothingSpecial function is called and code execution continues below the Switch statement.
Note that without a break; or return; at the end of a case, execution will simply fall through to the next case's statements.
Example:
Switch (i) { Case 1: DoSomething(); break; Case 2: Case 3: DoSomethingElse(); Case 4: DoSomethingMore(); return; Default: DoNothingSpecial(); break; }
While Loops
While loops are executed as long as the condition stays true. If the condition is false before the loop starts, the whole While loop is not executed.
Syntax:
While ( condition ) { ... }
For Loops
For loops can be used similar to While loops. The InitialCommand is executed before the first loop cycle starts, LoopCondition is checked before each (including the first) loop cycle and UpdateCommand is executed at the end of every loop cycle. The For loop is executed as long as the condition stays true, but it will not be executed at all if the condition initially is false.
Syntax:
For ( InitialCommand; LoopCondition; UpdateCommand ) { ... }
For loops can be used to make simple loops with a counter or to iterate through a linked list.
Examples:
For (i = 0; i < 10; i++) { ... }
The above example will execute for i = 0, 1, ... 9. The endgame scenario is:
- execute loop for 9
- increment i to 10
- start from the top: check i and it fails the condition; so move on.
The following example loops through all Pawns in the level: (works only server-side)
For (P = Level.PawnList; P != None; P = P.NextPawn) { ... }
Do...Until Loops
Do...Until loops are always executed at least once. The loop stops if the condition becomes true.
Syntax:
Do { ... } Until ( condition );
Example:
Do { log(i); } Until (i++ > 10);
ForEach Loops
ForEach uses an iterator function to loop through a list of actors.
Syntax:
ForEach IteratorFunction(BaseActorClass, IteratorVariable, IteratorParameters) { ... }
The first example iterates through all HUD actors in the map.
The second one iterates through all projectiles, which are in the same zone like the actor calling the ForEach loop.
Examples:
ForEach AllActors(class'HUD', H) { ... }
ForEach Region.Zone.ZoneActors(class'Projectile', P) { ... }