Just as mentioned earlier, variables are used to store data, and they can hold data permanently or temporarily.
Every single variable needs to be "declared", or in another words, you have to tell that the variable exists and what it's supposed to store in it.
The syntax is very simple:
var (variable type) (variable name)
or
local (variable type) (variable name)
Examples:
Code: Select all
var int SomeNumber;
local int SomeLocalNumber;
On the other hand, variables declared as local are only used within functions, and are temporary and unaccessible from outside that function, hence once a function call finishes they simply disappear.
Variables are always declared before any code further is written. Unlike some other languages, you cannot declare variables in the middle of the code in UnrealScript.
Examples:
Code: Select all
class SomeClass extends Actor;
var int Var1;
var string Var2;
var bool Var3;
//Rest of the class code goes here
Code: Select all
function SomeFunction()
{
local int Var4;
local string Var5;
local bool Var6;
//Rest of the function code goes here
}
In UnrealScript every variable can hold a specific limited amount of data of a specific type, and they are divided into 4 main sets of types:
- primary
- enumeration
- reference
- structure
Primary types:
The primary types are the ones which store the most basic kind of data: numbers, text, etc, and in UnrealScript there are only 6 of them:
- bool: a boolean type, and its value can only be True or False.
- byte: an unsigned byte type, and as such its value can only range between 0 and 255.
- int: a signed 32bits integer type, as as such possible values can only range from -2147483648 to 2147483647.
- float: a float types, or in other words, any kind of real number with decimal places, like 1.25, 502.1235, etc. Keep in mind however due to the very nature of the float type ("float" stands for "floating point"), you may get precision errors the more decimal places you use or the higher the number itself becomes.
- name: a name is like a keyword, since it obbeys the same rules as the naming of variables and functions themselves (only letters, numbers and underscore, cannot start with numbers and cannot have spaces). They're represented within single quotes, as: 'Name_1', 'Name_2', 'Name_3'.
Don't use this type for normal text though, the name type is to be used as an identifier alone and cannot be concatenated.
- string: a normal text type, here you can use any characters you wish and it uses double quotes to enclose them, as: "This is some text".
Enumeration types:
The enumeration types are the ones which store "meaning" behind a set of numeric values. For example:
Code: Select all
enum EBtnPress
{
BT_None,
BT_PressUp,
BT_PressDown
};
var EBtnPress ButtonPress;
Enumerations are nothing more than normal primary byte types, but with the catch that each value of the byte (0, 1, 2, ...) can be named to your liking and represent that value.
They are also good to restrict the range of that byte to the size of the enumeration.
Let's analyze the example above:
- BT_None represents the byte value 0;
- BT_PressUp represents the byte value 1;
- and so on, up to BT_PressDown in this case.
Therefore we can make friendly code like:
Code: Select all
if (ButtonPress == BT_PressUp)
Code: Select all
if (ButtonPress == 1)
And is also possible to make these:
Code: Select all
if (ButtonPress > BT_None)
They are also used to be able to build list boxes when seeing an actor property in Unreal Editor for example.
The greatest and highest usage you will find on enumerations is when dealing with replication, but that will be explained later on in the replication lessons.
Reference types:
The reference types are the ones which store object or class references. They're generally also called "pointers".
Example:
Code: Select all
var Actor A;
var class<Effects> FXClass;
Code: Select all
var Actor A;
var float glow;
function PostBeginPlay()
{
A.DrawScale = 2.0;
A.PlayAnim('SomeAnim');
glow = A.ScaleGlow;
}
At the end, our object glow variable assumes the value of the ScaleGlow property of the actor A.
Being that A is just pointing at the actor, this means that if the actor changes in some way, A and all the variables pointing to this actor will see those changes as well.
Structure types:
The structure types are the ones which store other variables within them instead of actual data, therefore it's somewhat like creating our own variable types (in a way). For example:
Code: Select all
struct SomeStruct
{
var int Var1;
var string Var2;
var Actor Var3;
};
var SomeStruct MyStructVar;
Then you can access them as:
Code: Select all
local int local_var1;
local string local_var2;
local Actor local_var3;
local_var1 = MyStructVar.Var1;
local_var2 = MyStructVar.Var2;
local_var3 = MyStructVar.Var3;
Code: Select all
local SomeStruct local_struct;
local_struct = MyStructVar;
This is useful when you want to represent a bunch of different data encapsulated into a single variable, and you can even create new structures with other structures within them, like:
Code: Select all
struct MegaStruct
{
var string VarTxt;
var SomeStruct VarStruct;
};
________________________________________________________________________________________________________________________
< Previous lesson | Next lesson > (soon)