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
日本語訳
한국어
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
UnrealScript 控制结构
概述
// 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" ); }
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; } }
// 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);
}
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
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
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
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
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