Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
UScript Tutorial.doc
Скачиваний:
4
Добавлен:
01.09.2019
Размер:
634.37 Кб
Скачать

For Loop Example

var int i;

for ( i=0; i<5; i++ )

{

Do stuff;

}

The first statement in the parenthesis, "i=0", sets the initial value of i as the loop starts. The second statement,

"i<5", is the condition that must be met for the loop to continue executing. As soon as i is greater than or equal to 5, the loop will terminate. The final statement, "i++", is what is done to i each time the loop executes. So, the first time this loop executes, i will equal 0. The next time, i will be incremented by one, making it equal 1. This is still less than 5, so the loop executes again. Next time, i will be 2, then 3, then 4, and finally 5. The loop will terminate once it gets to 5, since 5 is not less than 5.

D o L o o p s

Do loops, unlike for loops, have no built-in expressions for incrementing counters or setting initial values. They simply execute over and over until a condition at the end of the loop is met. Because they have no built-in expression for incrementing a variable, you will have to include a line within the loop that somehow increments or changes your counter variable, so that the ending condition will eventually be met. Otherwise, you get an infinite loop. Not a good thing.

Do Loop Example

var int i;

do

{

Do stuff;

i++;

} until (i == 5);

You'll notice I included the line "i++;" within the loop. This will increment i each time the loop executes, so it will terminate when i gets to 5. The main distinction of the do loop is the fact that it executes until some condition is true. Both for and while loops execute while some condition is true.

W h i l e L o o p s

While loops are basically do loops, except for the fact that they execute while their condition is true, whereas do loops execute until their condition is true. Again, you'll have to include a line in the loop to somehow increment your counter variable, since there is no built-in expression for this in the loop declaration.

While Loop Example

var int i;

while ( i < 5 )

{

Do stuff;

i++;

}

S w i t c h S t a t e m e n t s

A switch statement is basically like a complicated if statement. It allows you to execute different blocks of code depending on the value of a certain variable. Take a look at this example:

Switch Statement Example

var string[32] Developer;

switch ( Developer )

{

case "Tim":

Hey, it's Tim. Do something;

break;

case "Cliff":

Look, there's Cliffy.

Do something else;

break;

case "Myscha":

Where'd Myscha come from?

Better do something else;

break;

default:

No one here;

break;

}

Not too complicated. You just supply the variable you want to use for the switch in the first parameter, then write different "cases" depending on the different values of the variable. The final "default" label is optional, and will be executed if none of the other cases are true. Note the break statements marking the end of each case. Like I said before, switch statements are basically just complicated if statements. I rarely have use for them, since the same effects can be achieved simply by using an if/else if/else.

F u n c t i o n s

I have a little confession to make. You know all the examples I've been giving so far, in which I declare a variable or two, then jump right into some code, such as assigning values to these variables, or writing an if statement or a loop? Well, that was illegal. In actual UnrealScript, you cannot just write code by itself. The only parts of a class that can be completely on their own are the class declaration, variable declarations, and exec commands. Everything else must be part of a function or state. So, what's a function, you ask? A function is just a block of code that performs some action. Once they're defined, they can be called in other parts of the code, to do whatever it is they're supposed to do. They can be given, or passed, variables when they're called, and they can return values. I know this all probably sounds very complicated (assuming you've never done any programming before), but it's really fairly simple once you understand it. Take a look at this example:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]