UDN
Search public documentation:
UnrealScriptControlStructuresJP
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
と単一の条件からなるものです。複数の条件をチェックするには、 else if
文を、 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 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" ); }
switch 文
switch 文 を使用することによって、式の値に応じて異なるコードを実行することができます。この構造は、まず、switch
キーワードを置いて、次に丸括弧内に変数または式を入れます。この変数または式が取りうるさまざまな値を指定するには、 case
キーワードを置いて、その後に値とコロンを置きます。各 case には、その値に関連させて、任意の数の文を置くことができます。この文は、switch 文の式の値が case の値と合致した場合に実行されます。 break
コマンドを使用すると、実行が停止されて switch 構造から抜け出ます。 break
コマンドが存在しない場合は、switch 構造の最後に到達するまで、それより下に続く他の case も実行され続けていくことになります。switch の式と合致する case がない場合に実行されるコードを指定するには、オプションのキーワードである default
を使用します。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 つの式をセミコロンで区切って指定しなければなりません。1 番目の式は、変数を初期値に初期化するためのものです。2 番目の式は、ループを実行する際、イタレーションそれぞれの前にチェックされる条件です。この式が true と判定されると、ループが実行されます。false の場合は、ループが終了します。通常の場合は、1 番目の式で初期化された変数の値についてチェックすることになります。3 番目の式は、各ループが行われた後で、さらに、2 番目の条件式が再び評価される前に、実行される式です。通常は、1 番目の式で初期化された変数の値を、何らかの仕方で変更します。 例 :
// 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
構文を使用します。 do-while
構文を使用する C/Java とは異なります。
// 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