Mostly Harmless

Structs

From Unreal Wiki, The Unreal Engine Documentation Site
Revision as of 06:05, 5 May 2008 by Azrael (Talk | contribs) (Native struct code)

Jump to: navigation, search

A struct (or structure) is a composite data type in UnrealScript, that can contain multiple member variables of (almost) any type, including another struct type. Structs are not reference types, that means a variable of a struct type actually contains the struct data itself, not a reference to the struct data. This imposes an important restriction on struct types: a struct may not directly or indirectly contain a variable of that same struct type.

Note: The compiler will probably not complain about a struct violating this restriction. However, if the struct type is used to declare any class or local variables or function parameters or return types, the compiler will likely crash as it attempts to allocate an infinite amount of memory!

Declaration syntax

The general syntax of a struct declaration is:

struct modifiers structname [extends parentstruct] {
  ...
}

Like enums, struct declarations may appear either as standalone declaration, in which case they must be followed by a semicolon, or as inline declaration instead of the type specification in variable declarations. Struct declarations may be contained in other struct declarations, both standalone or in variable declarations. There is no difference between declaring a struct at the class level and within another struct or between a standalone and inline declaration, in all cased the struct is a direct member of the class.

Remember that all members of an UnrealScript class share the same namespace. A struct declaration may hide any member of a parent class if it has the same name.

Modifiers

Struct modifiers are not available in Unreal Engine 1. Most struct modifiers are related to native code. Modifers with other uses are:

long 
Specifies that the struct contains many members and should not be examined when generating a value string for struct variables in UnrealEd's Actor properties window. Usually structs like Vector are displayed as "(X=1.2,Y=3.4,Z=5.6)", but with the long modifier only "(...)" is displayed.

Other struct modifiers you may encounter but shouldn't use for your own structs are:

export 
This struct is supposed to be exported to native headers.
immutable 
?
init 
?
native 
This struct includes native logic.
transient 
?
{native type name
The type to use in native code instead of this struct type. This is mainly a hack in Unreal Engine 3 to declare variables in UnrealScript with types the UnrealScript language does not provide. There are only three instances of this syntax in UT3. The structs Object.qword and Object.double represent the native types QWORD and DOUBLE respectively, and the struct UIEditorOptions.ViewportDimension represents the native type WxRect.

Member declarations

Struct members are declared like class variables. A variable group name or certain variable modifiers don't make sense for struct members, though.

Default member values

By default all struct member variables are initialized with the null value of the corresponding type.

Starting with Unreal Engine 3, struct members can also have a default value other than the corresponding null value. This can be defined in a structdefaultproperties block inside the struct declaration. A good example is the LinearColor struct from UT3:

// A linear color.
struct immutable LinearColor
{
	var() float R, G, B, A;
 
	structdefaultproperties
	{
		A=1.f
	}
};

LinearColor declared four members of type float. The default value of member A is defined to be 1.0, while all other members keep 0.0 as their default value. This means, whenever a variable of type LinearColor is declared, its A component already contains the value 1.0, while the R, G, and B components have the value 0.0.

Native struct code

Like native classes, some structs in Unreal Engine 2 and 3 games may contain native code snippets in UnrealScript. These declarations do not affect UnrealScript directly, but they are exported when auto-generating native headers while compiling UnrealScript classes. In UT2004 there are only two struct like this, GUI.GUITreeNode and GUIMultiColumnList.MultiColumnSortData. In Unreal Engine 3 games this is a much more common sight, especially in UI-related classes.

In Unreal Engine 2 native struct code is enclosed in a cppstruct block. Unreal Engine 3 uses a structcpptext block instead.