Variable Syntax
Variable Declaration
Class variables
The var keyword is used after the class declaration to declare class variables. Using parentheses after the keyword var makes the variable appear in the Actor properties window for objects of that class. Using group makes them appear in a certain group like Display. If group is not specified the class name is used as group. See Displaying variables in UnrealEd.
var(group) modifiers type name, name, ...; var() modifiers type name, name, ...; var modifiers type name, name, ...;
Default values of variables are set in the Default Properties block. For the possible values of the type, see Variable Type.
Constants
Constants are just names for certain values and can be changed neither from UnrealScript nor from native code. Constants can be declared anywhere in a class, even inside functions, but they are always available in the whole class.
const name = value;
Local Variables
Local variables are only available in the function they were declared in.
local type name, name, ...;
Arrays
Static Arrays
To create an array just specify its length in brackets behind the variable's name:
var/local type name[length], ...;
Type can be any of the built-in types (except bool) or a struct or and object. (Enums?) The length needs to be a constant integer value greater than 0, i.e. you can also use a constant instead of a number here. The array's size cannot be changed once the array has bee declared. All elements are initialized to their null values. (see Built-In Types below)
Array values are accessed via name[0] to name[length - 1]. If you need to get the length of the array use ArrayCount(name).
Note: UnrealScript doesn't support multi-dimentional arrays of any kind. To fake multi-dimensional arrays you either have to use a larger array and write accessor functions for it that transform multiple indices to a the actual array index or use a declaration like this:
struct TMultiArray { var int Y[100]; }; var TMultiArray X[100];
Later, in a function you can access this fake multi-dimensional array with this syntax:
X[50].Y[50] = 31337;
Dynamic Arrays
Dynamic Arrays are partly built into UT's UnrealScript. Only the declaration works. However they are fully supported by UT2003's UnrealScript (in addition to the regular static array which were already in UT UnrealScript). Dynamic arrays can't be replicated.
var/local array<type> ArrayName, ...;
The type can be any of the built-in or self-defined types (structs, enums, classes). Note that you can declare a variable of type array<bool>
and your code will compile fine, but the engine simply ignores assignments to elements of that array, so it's basically worthless. You'll have to resort to array<byte>
for that. After declaration the dynamic array is empty, i.e. the array's length is zero.
To access a dynamic array use this syntax:
ArrayName[element number]
Dynamic arrays have various properties and methods to modify them:
- ArrayName.Length
- Returns the number of elements in the array. (Similar to ArrayCount(ArrayName) for static arrays.) You can change this property to add or remove elements from the array. Only use the
=
operator for doing this, any other (like e.g.+=
) will not work and might even crash the game.
Increasing the length adds empty elements at the end of the array while preserving the existing elements. Decreasing the length removes only the last elements without changing the other ones. - ArrayName.Remove(position, number)
- Removes number elements from the array, starting at position. All elements before position and from position+number on are not changed, but the element indices change, i.e. the element formerly accessed through ArrayName[position+number] is accessed through ArrayName[position] after the removing.
- ArrayName.Insert(position, number)
- Inserts number elements into the array at position. The indices of the following elements are increased by number in order to make room for the new elements.
If you write to an array element that doesn't exist (yet) because the array is too short, the array will automatically increase its length. All new elements inserted between the former end of the array and the element you are writing to are initialized with their respective null element (zero, empty string or None, depending on the array's type).
If you read an array element that doesn't exist because it's beyond the array's length, you'll get a null value (see below) and an "Accessed array out of bounds" warning is logged.
Note: You can't use a combination of dynamic and static arrays.
The declaration var array<string> StringArray[10];
will actually be compiled and used as a static array declared as var string StringArray[10];
Variable Modifiers
Modifier keywords come between the var keyword and the type keyword. Any number of modifiers can be used, including zero.
//Example var transient int MyVar;
- config
- The value of this variable can be saved to a *.ini file and is class specific. (see Config Vars And .Ini Files)
- const
- The variable cannot be changed from UnrealScript, but only through native code.
- deprecated
- (Only in v600+ builds of the Unreal Engine.) The compiler prints the warning "Reference to deprecated property variable name." when a variable marked as "deprecated" is used in the code.
- edfindable
- (Only in v600+ builds of the Unreal Engine.) Can be read as 'Editor Findable.' If this property is selected in the actor properties window within UnrealEd, there will be a "find" button beside the property in question. If you click the "find" button, the cursor will change to the "finder" cursor. If an actor is clicked while the "finder" cursor is displayed, the currently selected property (in the actor properties window) will be set to the actor clicked upon.
- editconst
- The variable cannot be changed in UnrealEd, even though it might be displayed there.
- editinline
- (Only in v600+ builds of the Unreal Engine.) Allowes UnrealEd users to edit the object referenced by this variable within the property window of the object this variable belongs to. See editinline for details.
- export
- ???
- globalconfig
- The value of this variable can be saved to a *.ini file and is also used for all subclasses. (see Config Vars And .Ini Files)
- input
- Mapped to Unreal's input system (keyboard, mouse, joysticks).
- localized
- The default value of this variable can be located in a localization file (*.int, etc.).
- native
- This variable is set by native code.
- private
- The variable is only visible from the class it was declared in, but not from any of its subclasses.
- protected
- The variable is only visible from the class it was declared in and all subclasses.
- transient
- This variable will not be included when saving the current game. (in Unreal)
- travel
- The value of this variable does not change when traveling between levels.
Related Topics
Discussion
Foxpaw: I have some variables that I could make private, but I'm curious - is there any advantage to having them private or should I leave them public on the off chance that a subclass might want to use them somewhere down the road? I don't see any reason why they would, but on the other hand there are some functions declared as final that Epic apparently didn't think anyone would want to override, whilst I very much do - so it's possible that sometime someone might want to use this variable for something. Is there a performance gain or anything like that to be had for using private variables?
Mychaeel: That's more a code design question than a purely technical one. While I'm very glad Epic followed a policy of leaving virtually everything public and non-final, my own code makes very frequent use of private or protected variables and functions.
At least during development my intent is to force others working with my code (and myself) to interact with my code only in a clearly defined, controllable fashion instead of messing with the internal state of my objects at will. That allows me to change the internal implementation of my classes without having to worry about what those changes do to others' code as long as I keep the interface true to its specification.
If you want to keep people from accessing variables or functions from outside a class but would still like to let them use those variables and use/overwrite the methods in subclasses, make them protected, not private.
Tarquin: I don't quite understand what "private" does. If I declare variable "Foo" to be private in MyClass, and I have objects A and B of that class, and C of a child class, can B read A's foo but C can't read A's foo? C has a foo variable of its own, with the same restrictions to its child classes?
Mychaeel: First part: Yes. Second part: C can have its own private (or public) variable called "foo," but that variable wouldn't be connected in any way to the "foo" variable from the base class. (Of course C has a piece of memory space allocated that the code in the base class would address as "foo," but the code in the child class doesn't know about the base class's "foo" at all.)