This page will attempt to explain the basic facts about variables that are needed in order to get started. For a more in depth explanation when you are more confident with UnrealScript you can simply search the Unreal Wiki or UDN for any topics about Variables.
“Variables can appear in two kinds of places in UnrealScript: instance variables, which apply to an entire object, appear immediately after the class declarations. Local variables appear within a function, and are only active while that function executes.” – UDN, UnrealScriptReference
Declaring Variables
Instance variables (also known as global variables) must appear at the top of the class before any function code, but after the class declaration.
Global variables must be declared with the “var” keyword. Global variables are available to any sub/child classes. A few examples of declaring global variables.
class MyVariables extends Actor; //declare new class called "MyVariables" which extends the Actor class. //The global variables must be declared after the class declaration but before any functions. var int a; //declare a global variable of type int (integer), and call it "a". var string myword; //declare a global variable of type string, and call it "myword". var bool myfact; //declare a global variable of type bool, and call it "myfact". var Actor other; //declare a global variable of type Actor, and call it "other". function AnyFunction() { }
Local variables are declared within functions and can only be used within the function it was created in. These variables are not accessable anywhere else. Local variables must be declared with the “local” keyword. Local variables must be declated at the top of the function, before the functions code. A few examples of declaring local variables.
class MyVariables extends Actor; //class declaration. //global variables go here function AnyFunction() { local int b; //declare a local variable of type int and call it "b". local string mylocalword; //declare a local variable of type string and call it "mylocalword". //function code goes after the local variables. }
There are 8 very basic variable types supported by UnrealScript that you will be using a lot through out your coding. It’s important that you understand what they are and what they mean.
Variable Types
- byte – A single-byte value ranging from 0 to 255* (a good example of this is the RGB (Red, Blue, Green) values of a colour which are limited to 255. If bytes are not initialized, they default to 0.
- int – A 32-bit integer value* (a whole number). If integers are not initialized, they default to 0.
- bool – A boolean value: either true or false.*If booleans are not initialized, they default to false.
- float – A 32-bit floating point number* (a decimal number). If floats are not initialized, they default to 0.00.
- string – A string of characters.*If strings are not initialized, they default to an empty space ” “.
- name – The name of an item in Unreal (such as the name of a function, state, class, etc). Names are stored as an index into the global name table. Names correspond to simple strings of up to 64 characters.* Names that belong to something, such as a function or state cannot be changed. If names are not initialized, they default to ‘None’ (meaning nothing exists for that value – Null).
- Object and actor references – A variable that refers to another object or actor in the world. For example, the Pawn class has an “Enemy” actor reference that specifies which actor the pawn should be trying to attack. Object and actor references are very powerful tools, because they enable you to access the variables and functions of another actor. For example, in the Pawn script, you can write Enemy.Damage(123) to call your enemy’s Damage function — resulting in the enemy taking damage. Object references may also contain a special value called None, which is the equivalent of the C NULL pointer: it says “this variable doesn’t refer to any object”.*If actors are not initialized, they default to ‘None'(meaning nothing exists for that value – Null).
- Structs – UnrealScript structs let you create new variable types that contain sub-variables. For example, two commonly-used structs are vector, which consists of an X, Y, and Z component; and rotator, which consists of a pitch, yaw, and roll component.*If vectors are not initialized, they default to 0.00,0.00,0.00. If rotators are not initialized, they default to 0,0,0.
*information from the UDN – UnrealScriptReference.
TIP – Use meaningful names for your variables.
Recent Comments