| Home Page | Recent Changes | Preferences

UnrealScript Language Reference/Program Structure

Program Structure

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

For Loops

"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-While Loops

"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

"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

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

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

"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 if "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 if "if" with brackets.
if( LightType == LT_Steady )
{
    log( "Light is steady" );
}
else
{
    log( "Light is not steady" );
}

Case Statements

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

// Example of switch-case.
function TestSwitch()
{
       // Executed 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: /FunctionsSection 5 of 9 – Next Page: /States

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In