Mod:Creation Kit/Variable Reference

A variable is storage for a single value in Papyrus. Depending on its type, it may be a value or a reference to a value. Variables may be defined in the script, or inside a specific function or block.

Defining Variables
In script: ::= ['=' ] * In struct: ::= ['=' ] * ? In function: ::= ['=' ]

Variables are defined simply by writing the type, followed by the identifier to use for the variable.

You may optionally follow the variable with an equals sign and an expression (in a function) or value (in a script) to initialize the variable with that value on the same line. If you do not give it an initial value, then it will start with a default value.

Variables in scripts do not have to be defined before use, but variables inside functions do. Variables defined in scripts may have flags.

The identifier must be unique within the block it is defined. In other words, if your script has a variable named "foo", then none of the functions may have a local variable named "foo" defined in them. However, if one function has a variable named "foo", then a different function in the same script can also have a "foo" variable.

Variables defined inside a block (like an "if" or "while") are local to that block.

Non-const variables may not be defined in native or const scripts (though they can be defined inside functions in these scripts)

Only variable definitions in structs may have documentation strings.

Examples
int foo float bar = 10.5 * x if x int foo = 10 else float foo = 10.5 endIf foo = 10 ; fails to compile!
 * Defines a variable named "foo" that is an integer
 * Defines a variable named "bar" that starts with a value of 10.5 times x (must be in a function)
 * Defines to variables, both named foo, that are distinct
 * Can't access foo out here!

Value Variables
Variables that are of type int, float, bool, or string are referred to as "value variables". These variables simply store a value, and when you assign them to each other, or pass them off to a function, they copy their value.

Examples
x = 5 y = x ; Copies the value of x and puts it into y y = 6 ; Changes the value of y to 6, but doesn't touch x, which is still 5

Reference Values
Variables that are objects, arrays, or structs are referred to as "reference variables". These variables point at a value instead of storing it, so if you assign one variable to another, both point at the same value and changes made to one will reflect in the other.

Examples
x = MyObject x.SetCoolValue(5) ; Sets the cool value of the object pointed to by x to 5 y = x ; Y now also points at MyObject y.SetCoolValue(10) ; Sets the cool value of the object pointed to by both x and y to 10 a = x.GetCoolValue ; Returns 10! b = y.GetCoolValue ; Also returns 10!

Var Variables
Variables of type "var" can hold any value, and remember the type of the object they are holding. You can't do anything with a var variable except ask it what type it is or cast it to a type.

Examples
var x = 1 ; x is now an int and contains 1 x = 1.5 ; x is now a float and contains 1.5 x = "Hello World" ; x is now a string and contains "Hello World" x = akActivator ; x is now whatever akActivator is and contains it

if x is int Debug.Trace("x is an integer: " + x as Int) elseIf x is float Debug.Trace("x is a float: " + x as Float) endIf