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

Simple Function Example

var int SomeInt, Result;

//Take an integer, and return its square

function int Sqr( int Num )

{

return Num * Num;

}

//Test the Sqr() function

function PostBeginPlay()

{

SomeInt = 3;

Result = Sqr(SomeInt);

}

There are two functions here, Sqr(), and PostBeginPlay(). Sqr() takes a number, Num, multiplies it by itself, and returns it. You'll notice that I didn't declare Num up with SomeInt and Result. This is because it is "declared" as a parameter to a function. When I call Sqr() down in the PostBeginPlay() function, I supply SomeInt as the value in parenthesis, or the parameter. The call to Sqr() causes the code contained in the Sqr() function to be executed, with SomeInt plugged in for Num. You'll also notice that I put the call to Sqr() after "Result =". This is because I am assigning the value which is returned by Sqr() to Result. When all this code is done executing, Result will be equal to 9: the square of 3. You may also have noticed the keyword "int" before the function name in Sqr()'s definition. This "int" means that Sqr() returns an integer value. Anyway, you may be wondering by now, Where is PostBeginPlay() being called from? The answer is, the engine. There are a wide variety of functions in UnrealScript which are called by the engine in certain places and under certain circumstances. The PostBeginPlay() function is called when an object is first created, so it makes a good place to put code that you want to be executed before any other code. For a list of common functions which are called by the engine (as well as other useful functions which aren't called by the engine), refer to the Function Reference at the side of this page. So, are you thoroughly confused yet? If not, then you're doing good. I know I was scratching my head quite a bit when I first learned this stuff. Well, keep reading, it gets better (or worse, depending on your viewpoint). You know the way I've been declaring variables all along? At the beginning of a class, using the syntax "var [vartype] [varname]"? Well, that's not the only way you can declare a variable. That type of variable, declared outside of any functions, is called a global variable. Global variables can be accessed anywhere in a class, and even outside of a class (as we'll see a little later). But, there are also local variables. Local variables are declared at the beginning of a function, and can only be accessed within that function. They're useful for doing short-term operations that won't need to be "seen" outside of a particular function. You see, one of the key elements of a function, and of object-oriented program as a whole, is the fact that a class or function can share useful data and important information with other classes and functions, but hide how they got that useful data and information. They show only the result, but not how they found the result. In any case, where was I going with this? Oh, yes. Local variables. Local variables are declared just like global variables, only they use the local keyword instead of the var keyword.

Local Variables

function PostBeginPlay()

{

//Declare a local integer

local int SomeInt;

//Assign a value

SomeInt = 3;

}

So, that's a local variable. Not too complicated, is it? Just like a global variable, except for the fact that it can only be accessed inside a particular function.

I n h e r i t a n c e

Inheritance, you ask? What could inheritance possibly have to do with programming? Well, it has a lot to with programming. At least when you're talking about classes. If you'll remember, I told you earlier that one of the reasons classes are arranged in a hierarchy is that child classes inherit code from their parent classes. Well, I wasn't just saying that to watch myself type. A class will inherit all variables, functions, states, and default properties (I'll talk about states and default properties a bit later) from every class above it in the hierarchy. For example, the Weapon class has in it all the code written in the Inventory class, the Actor class, and the Object class, since these are the classes above it in the hierarchy. Any new code you write in a class is simply added on to the code inherited from parent classes. But what if you wanted to change a certain inherited function? Well, you can. It's called overriding a function. All you have to do is copy the function definition into the new class (the name, parameters, and return value type), and write new code for it. The ability to do this is extremely useful in UScript, since it allows you to add or change functionality in things without having to copy over all the code. For instance, say you wanted to make an ASMD that launched grenades in alt-fire instead of the little blue energy ball thingy. All you would have to do is copy the one function that controls what happens when the player presses alt-fire, and make a few little changes. Nothing to it.

S t a t e s

No, not the United kind. We're talking about UnrealScript here, remember? Anyway, a state is simply a section of code that is executed only the class is in that state. For instance, what are the different states that a weapon could be in? It could be firing, alt-firing, reloading, or just sitting there looking pretty. Each of these conditions could have their own state defined for them, which would contain code that's only used when the weapon is in that condition. For instance, if you wanted a weapon to play an idle animation every 30 seconds, you could put a looping timer in the idle state, so it would only run when the weapon was not doing anything. To give you an idea of how they're defined, here's the actual Idle state from the Weapon class:

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