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

Struct Example

//Define a new struct

struct Box

{

var float Length;

var float Width;

var float Height

};

//Declare a couple variables of type Box

var Box MyBox, YourBox;

//Assign values to individual components

YourBox.Length = 3.5;

YourBox.Width = 5.43;

YourBox.Height = 2.8;

//Set MyBox equal to YourBox

MyBox = YourBox;

The struct defines a new type of variable, called "Box", which has three sub-variables to it: length, width, and height. Once you define a variable of the new type, you can assign values to its individual components with the syntax "VarName.ComponentName". One very common struct in UnrealScript is the vector, which is made up of X, Y, and Z components. The techniques of working with vectors are somewhat complex, and you can learn more about them in the Vectors tutorial.

C o n d i t i o n a l s

If you've ever done any programming before, you're almost sure to be familiar with the If/Then/Else statement. They exist in UnrealScript as well, although the syntax might be slightly different than what you're used to if you program in a BASIC language. If you've never programmed before, then allow me to explain. A conditional is a way of having Unreal perform certain operations only if a certain condition is met. For instance, do one thing if a bool is true, and do something else if it's false. Conditionals are key to accomplishing all sorts of things in any programming language, and UnrealScript is no exception. The basic syntax for a conditional in UnrealScript is:

Conditional Syntax

if ([expression1] [operator] [expression2])

{

Do some stuff here;

}

else if ([expression1] [operator] [expression2])

{

Do more stuff here;

}

else

{

Hey, look, more stuff;

}

First, Unreal checks to see if the first condition is true by comparing expression1 to expression2 using the operator. If that condition checks out, then the first set of commands are executed, and the conditional is finished. If the first condition isn't true, though, Unreal will check the second condition, and if it's true, it'll execute the second set of commands. If it goes through all the conditions, and none of them are true, it will execute the "else" set of commands. When writing a conditional, you don't have to have else if's and else's. They're just available should you need to be more specific with what you want Unreal to do. All you have to have when writing a conditional is the first "if" statement. There are many different operators that can be used in conditionals, as you can see in this table:

OperatorDescription

==Equal to

!=Not equal to

<Less than

>Greater than

<=Less than or equal to

>=Greater than or equal to

~=Approximately equal to

Not every operator will work with every variable type. For instance, you can't really say that one actor reference is "greater than" another actor reference, so the four greater than/less than operators aren't applicable to actor references. Just use common sense to determine what will work with what, and you should be just fine.

Basic Conditional

var bool bSomeBool, bSomeOtherBool;

var int SomeInt, SomeOtherInt;

if (SomeInt > 3)

{

SomeInt is greater than 3, so do

something;

}

else if (SomeOtherInt <= SomeInt)

{

SomeInt is not greater than 3,

but SomeOtherInt is less than or

equal to SomeInt, so do something

else;

}

else

{

All the conditionals failed, so

do this;

}

if (bSomeBool)

{

bSomeBool is true, so do this;

}

else if (!bSomeOtherBool)

{

The first conditional failed, but

bSomeOtherBool is false, so do this

instead;

}

Note the way I used the bools in the second conditional. Because bools can only be one of two values (true or false), they don't need to be compared using two expressions. Saying "if (bSomeBool)" is the same as saying "if (bSomeBool == true)", and saying "if (!bSomeOtherBool)" is the same as saying "if (bSomeOtherBool == false)". Now, moving on, what if you wanted to do something only if two conditions were true? Or what if you wanted to do something if only one of two different conditions were true? That's where these operators come in:

OperatorDescription

&&And

||Or

These are used to link conditions together in the same statement.

Take a look at this example:

Conditional with && and ||

var bool bSomeBool;

var int SomeInt, SomeOtherInt;

if (SomeInt > 3 && SomeOtherInt < 3)

{

SomeInt is greater than three,

and SomeOtherInt is less than

three, so do something;

}

else if (SomeOtherInt == SomeInt || !bSomeBool)

{

The first condition failed, but

either SomeOtherInt equals SomeInt,

or bSomeBool is false, so do this

instead;

}

In the first one, && links the two statements together, so the condition is only true if both statements are true. In the else if, || links the two expressions together, so the condition will be true if either of the statements is true.

O t h e r F l o w C o n t r o l D e v i c e s

In addition to "if" statements, there are other ways to control how code flows. Things such as loops and switch statements will allow you to fine-tune your code, and get the results you want. To be honest, I've never used a switch statement in UnrealScript, but I'll explain them anyway, since everyone's coding style is different. Loops, however, I use all the time. They can be extremely useful to do certain things. There are three types of loops in UnrealScript, which I will explain below.

F o r L o o p s

For loops are the type I use the most, since I've found that they usually fit my needs just as well or better than the other two types. The basic concept of a for loop is to execute a certain block of code over and over again, until a certain condition is met.

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