Mod:Creation Kit/Structs (Papyrus)

=Description=

Structs are special kinds of variables that can hold several named values of differing types. You access the members of a struct just like you'd access the properties of a script.

=Declaring Structs=

Point is a struct that contains two float variables, X and Y, both of which default to 1.0. We then make a MyPoint variable that is of Point type, but it will start with the value of None, because it is uninitialized. We then give it a value inside MyFunction using the new command.

QuestStage is a struct that contains a Quest and an integer. Like MyPoint, we create a MyQuestStage variable, and give it a new value inside MyFunction.

OtherStruct is a struct containing an integer and a float, but only the float is visible in the editor UI.

Note that structs cannot contain other structs, arrays, Var variables, or const variables.

You can add documentation strings to struct variables, which will show up in the editor in the tooltips (just like documentation strings on properties).

Example:

You can, of course, make a property that is an struct. The editor will show a special UI for it that allows you to edit it.

Example:

You can also make arrays of structs. Note that when first created, the array will be nothing but NONE - in order to use an array element, you have to assign it to a struct or create a new struct.

Example:

The new call must be done inside a function. You cannot use it on an object variable outside of a function. If you want a variable to start with an value, then you can make the struct inside the OnInit event.

Function Parameters
To accept a struct as a parameter to a function, use the same syntax as for passing a variable. Structs are passed by reference, just like arrays and scripts, so changes made to them are made to the original.

Example:

Returning from Function
To return a struct from a function, again, use the same syntax.

Example:

=Creating Structs=

To create a struct, use the "new" keyword, followed by the struct's type. Each variable in the array will get the appropriate default value, according to the struct definition.

Example:

Usually you'll be assigning these directly to new struct variables, but if a function wants an struct, you can make the new struct in the function call, like so:

Example:

=Getting/Setting Members=

To get a variable from an array, or to set it, use a dot with the name of the struct member after the variable name, just like accessing a property or function on a script.

Example:

If the struct members are other scripts, you can access properties and functions in the same way.

Example:

Note that, since structs are passed and assigned by reference, that any modifications to a struct's elements will be reflected in any other variables looking at that struct.

=Assigning/Passing Structs= Assigning one struct to another struct, or passing a struct to a function, is done just like any other variable. However, note that structs are passed/assigned by reference, just like objects (and unlike some other languages). In other words, if you assign one struct to another, both are looking at the same struct - and modifications made to one, will be reflected in the other.

Example:

=Casting Structs= Structs can be cast to strings, bools, or vars. If you cast a struct to a string, it will put each member in the struct inside brackets, separated by commas, prefixed by the member name, in the order they were specified in the script. If the struct is especially large, it may trim the string a little early and put an ellipsis on the end. If you cast an struct to a bool, it will be true if the variable is pointing at any non-none value, and false if it is none.

Example: Mod:Creation Kit/Category: Papyrus Mod:Creation Kit/Category: Papyrus Tutorials

= Accessing Struct Types in Other Scripts = 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.