Lesson 4 - UnrealScript: Language

Lessons and tutorials about mods and maps
Post Reply
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Lesson 4 - UnrealScript: Language

Post by Feralidragon »

First, here's the possible code for the homework from the previous lesson:
Spoiler
Image
________________________________________________________________________________________________________________________

The game uses a language which is called UnrealScript, and is rather simple as it's a compiled strong-typed imperative language.
I am pretty sure those terms may sound "awesome" to you, but what they mean is:
- compiled: the code must be compiled/transformed to be able to run;
- strong-typed: variables need to be declared beforehand and they always require the type of data they use to be specified;
- imperative: most of your code will be about building your own algorithms using low level conditions (like "if", "switch case"), loops and whatnot rather than just use function "Z" to get "X", since UnrealScript is a Java-C++ weird hybrid, and both of them are imperative as well.


1 - First thing you have to know is that UnrealScript is case insensitive, which means that keywords and names such as "if", "iF", "If" and "IF" are one and the same thing for the UnrealEngine. However, it's a good practise to keep one single style of doing things in your code, like always using "if" or "IF", but never doing both at the same time.


2 - The second thing is that UnrealScript is fully object-oriented, which means that you never run code outside an object scope, as all the code is always inside and bound by some object or class, no exceptions, therefore you have to create classes and extend them from another class in order to run your code, just like we did in the lessons 1 and 2 when creating a new mutator and new trigger respectively.

However, if you are familiar with object-oriented programming, one thing you must note is that UnrealScript doesn't have things like constructors for instance.
The closest thing you can do to a constructor is to call a function in the object after you instantiate it.
The standard practise in UnrealScript is to create further subclasses with their own default properties set if the same settings are used often in many instantiations.
In the case of Actor subclasses, this instantiation is done through the Spawn function, therefore "instantiating an Actor class" is the same as spawning that Actor into the world.


3 - The struture within an object (or class) is always as following:
Image

Basically, first comes the class declaration, in the form of:

Code: Select all

class A extends B;
Then the exec directives (generally used to import custom resources, such as models, skins, sounds, etc), like:

Code: Select all

#exec Texture Import File=Textures\S_Actor.pcx Name=S_Actor Mips=Off Flags=2
Then the variables or properties of the class itself:

Code: Select all

var int SomeIntProperty, AnotherIntProperty;
Then the replication block:

Code: Select all

replication
{
    reliable if (Role == ROLE_Authority)
        SomeIntProperty;
}
Then the actual functions with the code in them:

Code: Select all

function doStuffA()
{
    //Some code here
}

function doStuffB()
{
    //Some code here
}
And finally the default properties block to define the default values of the variables or properties declared above:

Code: Select all

defaultproperties
{
    SomeIntProperty=10
    AnotherIntProperty=22
}
Keep in mind however that you don't need to have all this in your classes, as aside from the class declaration (that must be done every single time), you should only add to the class what you really need.
For instance, most of the times you won't need to use exec directives nor replication blocks, but in case you do, the picture above shows where they should be located and declared as.


4 - UnrealScript statements always end with semi-colon (;) and are grouped by using curly braces around them ({ and }).
Example:

Code: Select all

function doStuff()
{
local int a, b, c;

    if (a > 0)
    {
        a++;
        b = c + a;
    }
    else
    {
        a--;
        c = b - a;
    }
}

5 - In UnrealScript, function and variable names can only have letters (a-z and A-Z), numbers (0-9) or underscores (_), and none of them should start with a number nor have any spaces at all.


6 - UnrealScript is compiled into byte-code, just like Java. Therefore, when compiled, UnrealScript runs in a virtual machine called UVM (Unreal Virtual Machine), hence UnrealScript is by far slower than C++ for example, but faster than interpreted languages like Javascript.
UnrealScript was designed this way so, just like Java, it could run in any operating system that the engine supports, but still be as fast as possible.
This makes possible to have the same mod and map packages running either in Windows or Linux machines for instance, instead of having to create one package for Windows and another for Linux, like the C++ counterpart demands.

The UVM is part of the Unreal Engine of course, and the whole engine is written in C++, which is commonly called as the "native language" of the engine.
C++ can also be used to do mods for UT, however is highly discouraged due to its compatibility and complexity compared to UnrealScript, and therefore it should only be used if proven inevitable for the mod or addon you want to do for the game (cases where UnrealScript is just too slow, or when it's only absolutely possible in C++ and no in UnrealScript).


The next few lessons will cover more of the syntax of UnrealScript, concerning variables, functions, statements, states and more.

________________________________________________________________________________________________________________________

Post Reply