I love the smell of UnrealEd crashing in the morning. – tarquin

Legacy:UnrealScript Language Reference/Program Structure

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

UnrealScript Language Reference[edit]

This subpage is part of a document by Tim Sweeney. The Unreal Wiki has been granted permission to host it. Please don't make any edits to these pages other than basic formatting of the text. If you have more to say on a topic here, please start a new Wiki page on it, for example from UnrealScript or Unreal Engine, and then add a "related topics" section to the very end of a page here.

Tim Sweeney
Epic MegaGames, Inc.
tim@epicgames.com
http://www.epicgames.com

Program Structure[edit]

UnrealScript supports all the standard flow-control statements of C/C++/Java:

For Loops[edit]

For loops let you cycle through a loop as long as some condition is met. For example:

// Example of "for" loop.
function ForExample()
{
       local int i;
       log( "Demonstrating the for loop" );
       for( i=0; i<4; i++ )
       {
               log( "The value of i is " $ i );
       }
       log( "Completed with i=" $ i);
}

The output of this loop is:

 Demonstrating the for loop
 The value of i is 0
 The value of i is 1
 The value of i is 2
 The value of i is 3
 Completed with i=4

In a for loop, you must specify three expressions separated by semicolons. The first expression is for initializing a variable to its starting value. The second expression gives a condition which is checked before each iteration of the loop executes; if this expression is true, the loop executes. If it’s false, the loop terminates. The third condition gives an expression which increments the loop counter.

Though most for loop expressions just update a counter, you can also use for loops for more advanced things like traversing linked lists, by using the appropriate initialization, termination, and increment expressions.

In all of the flow control statements, you can either execute a single statement, without brackets, as follows:

for( i=0; i<4; i++ )
    log( "The value of i is " $ i );

Or you can execute multiple statements, surrounded by brackets, like this:

for( i=0; i<4; i++ )
{
       log( "The value of i is" );
       log( i );
}

Do-Until Loops[edit]

Do-Until loops let you cycle through a loop while some ending expression is true.  Note that Unreal's do-until syntax differs from C/Java (which use do-while).

// Example of "do" loop.
function DoExample()
{
       local int i;
       log( "Demonstrating the do loop" );
       do
       {
               log( "The value of i is " $ i );
               i = i + 1;
       } until( i == 4 );
       log( "Completed with i=" $ i);
}

The output of this loop is:

 Demonstrating the do loop
 The value of i is 0
 The value of i is 1
 The value of i is 2
 The value of i is 3
 Completed with i=4

While Loops[edit]

While loops let you cycle through a loop while some starting expression is true.

// Example of "while" loop.
function WhileExample()
{
       local int i;
       log( "Demonstrating the while loop" );
       while( i < 4 )
       {
               log( "The value of i is " $ i );
               i = i + 1;
       }
       log( "Completed with i=" $ i);
}

The output of this loop is:

 Demonstrating the while loop
 The value of i is 0
 The value of i is 1
 The value of i is 2
 The value of i is 3
 Completed with i=4

Break[edit]

The break command exits out of the nearest loop (For, Do, or While).

// Example of "while" loop.
function WhileExample()
{
    local int i;
    log( "Demonstrating break" );
    for( i=0; i<10; i++ )
    {
        if( i == 3 )
            break;
        log( "The value of i is " $ i );
    }
    log( "Completed with i=" $ i );
}

The output of this loop is:

 Demonstrating break
 The value of i is 0
 The value of i is 1
 The value of i is 2
 Completed with i=3

Goto[edit]

The Goto command goes to a label somewhere in the current function or state.

// Example of "goto".
function GotoExample()
{
       log( "Starting GotoExample" );
       goto Hither;
Yon:
       log( "At Yon" );
       goto Elsewhere;
Hither:
       log( "At Hither" );
       goto Yon;
Elsewhere:
       log( "At Elsewhere" );
}

The output is:

 Starting GotoExample
 At Hither
 At Yon
 At Elsewhere

Conditional Statements[edit]

If, Else If, and Else let you execute code if certain conditions are

met.

// Example of simple "if".
if( LightBrightness < 20 )
    log( "My light is dim" );
 
// Example of "if-else".
if( LightBrightness < 20 )
    log( "My light is dim" );
else
    log( "My light is bright" );
 
// Example of "if-else if-else".
if( LightBrightness < 20 )
    log( "My light is dim" );
else if( LightBrightness < 40 )
    log( "My light is medium" );
else if( LightBrightness < 60 )
    log( "My light is kinda bright" );
else
    log( "My light is very bright" );
 
// Example of "if" with brackets.
if( LightType == LT_Steady )
{
    log( "Light is steady" );
}
else
{
    log( "Light is not steady" );
}
Note: due to a bug in the unrealscript parser, you cannot use a loop construct (while, for, etc) in a simple else clause. You must use the form with brackets.

Case Statements[edit]

Switch, Case, Default, and Break let you handle lists of conditions easily.

// Example of switch-case.
function TestSwitch()
{
       // Execute one of the case statements below, based on
       // the value in LightType.
       switch( LightType )
       {
               case LT_None:
                       log( "There is no lighting" );
                       break;
               case LT_Steady:
                       log( "There is steady lighting" );
                       break;
               case LT_Backdrop:
                       log( "There is backdrop lighting" );
                       break;
               default:
                       log( "There is dynamic" );
                       break;
       }
}

A switch statement consists of one or more case statements, and an optional default statement. After a switch statement, execution goes to the matching case statement if there is one; otherwise execution goes to the default statement; otherwise execution continues past the end of the select statement.

After you write code following a case label, you must use a break statement to cause execution to go past the end of the switch statement. If you don’t use a break, execution "falls through" to the next case handler.

see also Flow Syntax on the Wiki

Prev Page: Legacy:UnrealScript Language Reference/FunctionsSection 5 of 9 – Next Page: Legacy:UnrealScript Language Reference/States