Difference between revisions of "Struct Reference"
imported>Qazaaq (Changed the structure array example) |
imported>Homecom m (Fixed a typo: changed "is other languages" to "in other languages".) |
||
Line 1: | Line 1: | ||
Structs are basically mini-objects that can hold several variables of different types, but cannot contain functions or events, and cannot extend any objects or other structs. However, like objects (and unlike structs | Structs are basically mini-objects that can hold several variables of different types, but cannot contain functions or events, and cannot extend any objects or other structs. However, like objects (and unlike structs in other languages) they are passed and held onto by reference, not by value. Structs may be used in arrays. | ||
== Defining a Struct == | == Defining a Struct == |
Latest revision as of 19:57, 15 June 2018
Structs are basically mini-objects that can hold several variables of different types, but cannot contain functions or events, and cannot extend any objects or other structs. However, like objects (and unlike structs in other languages) they are passed and held onto by reference, not by value. Structs may be used in arrays.
Defining a Struct[edit | edit source]
<struct> ::= 'struct' <identifier> <variable definition>+ 'endstruct'
A struct is defined by having its name on the line with the struct, followed by the list of variables the struct contains, and terminated with 'endStruct'. The variables inside a struct can be anything except another struct, an array, or a var. Struct variables also may not be defined as const. Unlike object variables, struct variables may have documentation strings attached.
Examples[edit | edit source]
; A struct containing two floats
struct Point
float X
float Y
endStruct
; A struct containing a quest and a stage to set
struct QuestStage
Quest QuestToSet
{The quest whose stage is to be set}
int StageToSet
{The stage to set on the quest}
endStruct
; A struct that will FAIL to compile
struct BadStruct
Var MyVariable ; BAD - cannot contain var type variables
int[] MyArray ; BAD - cannot contain arrays
Point MyPoint ; BAD - cannot contain other structs
float MyFloat const ; BAD - cannot have const variables
endStruct
Struct Creation[edit | edit source]
<struct creation> ::= 'new' <struct type>
To create a struct, use the "new" keyword, followed by the type.
The initial value of each variable in the struct will be the default value specified for each member in the struct definition.
If you make a struct property, then the Creation Kit will create and set up the struct for you if the CK user gives it a value.
Note that this cannot appear in the script outside of a function.
Examples[edit | edit source]
; Create a new point structure.
Point myPoint = new Point
; Create an array of five new point structures.
Point[] array = new Point[5]
array[0] = new Point
array[1] = new Point
array[2] = new Point
array[3] = new Point
array[4] = new Point
Accessing Struct Types in Other Scripts[edit | edit source]
<struct type> ::= <script name> ':' <struct name>
You can access a struct type defined in another script by prefixing the name of the struct with the name of the script containing the struct, separating them with a colon. If you don't want to type in the script name all the type, you can use Import, just like with global functions.
Examples[edit | edit source]
; Assume Point struct is defined in MyScript
MyScript:Point myPoint = new MyScript:Point
Struct Members[edit | edit source]
<struct access> ::= <expression> '.' <member name>
To get a specific struct member, put its name after a dot, just like accessing a property on a script.
Examples[edit | edit source]
; get X element of a point
x = myPoint.X
; Set the quest stage contained in the "questStage" struct defined above
myQuestStage.QuestToSet.SetStage(myQuestStage.StageToSet)