UDN
Search public documentation:

UnrealScriptControlStructuresCH
English Translation
日本語訳
한국어

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

UE3 主页 > 虚幻脚本 >UnrealScript语言参考指南 > UnrealScript 控制结构

UnrealScript 控制结构


概述


很多时候,您都需要根据某些因素在不同条件执行不同的代码,或者需要重复执行一小段代码。UnrealScript通过各种控制结构提供了针对这个功能的支持,比如循环条件语句switch语句

在所有的流程控制语句中,你可以执行一个没有括弧的单独的语句,如下所示:

// Example of simple "if".
if( LightBrightness < 20 )
	'log( "My light is dim" );

或者你可以执行由大括号包围的多个语句,像这样:

// Example of simple "if".
if( LightBrightness < 20 )
{
	'log( "My light is dim" );
	'log( "Brightness ="@LightBrightness );
}

无论语句的数量是多少,通常都推荐您使用大括号。这使得您的代码更易读,并且可以防止由于添加语句但忘记添加大括号而产生的逻辑错误。

选择结构


选择结构允许您根据某个条件(变量的值、表达式的值)决定执行哪个代码流程。

If语句

if 语句 ,在满足某些条件情况下让您执行代码。最简单的形式是 if 语句和一个单独条件。要想判断多个条件,可以在最初的 if 语句后使用 else if 语句,每个 else if 都有它们自己的条件。最后,如果前面的所有条件都不满足,那么可以使用一个捕获所有不满足条件情况的 else 语句。

// 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" );
}

上面的示例都使用判断同一变量的值作为条件,但是if/else if语句中的条件不是必须彼此相关联。一般它们以这种形式出现,因为把相关项分到一组是有意义的,但是这不是必须的。

Switch语句

switch语句 ,可以根据表达式的值执行不同代码。这个结构以 switch 关键字开头,后面圆括号中是变量或表达式。可能出现的不同值通过使用 case 关键字指定,case关键字后跟随着一个值和冒号。每个case语句可以具有多个和其相关的语句,如果switch表达式的值和该case语句的值相匹配那么将会执行这些相关语句。==break== 命令用于中断执行并跳出switch语句。如果没有 break 命令存在,那么代码执行将继续执行下面的其他case语句,直到到达switch语句结束处为止。如果没有case语句和switch表达式相匹配,那么您可以通过使用 default 关键字指定要执行的代码,指定方式和case语句一样。

// 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;
	}
}

以下显示了一直向下执行直到中断位置的switch语句示例。这对于那些需要针对多个值执行同一段代码的情况是有用的,这样就不必重复那段代码了。

// Example of switch-case.
function TestSwitch2()
{
	switch( LightType )
	{
		case LT_None:
			log( "There is no lighting" );
			break;
		case LT_Steady:   // will "fall though" to the LT_Backdrop case
		case LT_Backdrop:
			log( "There is lighting" );
			break;
		default:
			log( "Something else" );
			break;
	}
}

循环结构


循环结构提供了几种不同的方法来重复执行某段代码,直到满足某个条件为止。

For 循环

for循环 ,只要满足某个条件它就会重复执行一个代码块。在for循环中,你必须指定3个表达式且它们之间使用分号分隔。第一个表达式用于初始化一个值作为它的初值; 第二个表达式给出一个条件,用于在每次循环执行前进行条件判断;如果这个表达式为真,则执行循环。如果它为假,则终止循环。一般,这个条件判断在第一个表达式中初始化的变量的值。每次循环后但再次计算该条件表达式之前执行第三个表达式。一般,该表达式以某种方式修改第一个表达式中初始化的值。

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

尽管大多数”for”循环仅仅是更新一个计数器,但你也可以通过使用合适的初始化值、终止条件和增量表达式,来使用”for”循环来执行像遍历链表这样的高级操作。

Do循环

do 循环 ,在某个结束表达式为true时重复执行某个代码块。这意味着该代码块将总是至少会被执行一次,因为执行完代码之后才会计算表达式。

注意: Unreal使用 do-until 语法,这和C/Java (它们使用 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);
}

这个循环的输出是:

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循环

while 循环 ,当某个起始表达式为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);
}

这个循环的输出是:

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

Continue


"continue"命令将会跳回到循环的开始位置,所以将不会执行continue之后的任何代码。这可以用于在某些情况下跳过循环代码。

function ContinueExample()
{
   local int i;
   log( "Demonstrating continue" );
   for( i=0; i<4; i++ )
   {
      if( i == 2 )
         continue;
      log( "The value of i is " $ i );
   }
   log( "Completed with i=" $ i);
}

这个循环的输出是:

Demonstrating continue
The value of i is 0
The value of i is 1
The value of i is 3
Completed with i=4

Break


"break"跳出最近一层循环("For", "Do", 或 "While")。

function BreakExample()
{
   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);
}

这个循环的输出是:

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

注意"break"命令也可以用于跳过执行一个条件语句("switch")的剩余代码。

Goto


Goto 命令可以任意地控制代码的执行流程,可以在当前函数或状态的某处指定一个要 goto(跳转到的) 标签,然后将会从那个位置处继续执行。

// 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" );
}

输出结果是:

Starting GotoExample
At Hither
At Yon
At Elsewhere

这在某些情况下是有用的,尤其是在状态中,但是它也会产生一些问题,因为代码的执行顺序不会那么明显。除了在状态代码中外,您可能很少看到在其他任何地方使用这种方法控制代码流程,因为其他情况可以使用其他的更好的控制结构进行处理。