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[edit | edit source]
In script:
<variable definition> ::= <type> <identifier> ['=' <constant>] (<flags>)*
In struct:
<variable definition> ::= <type> <identifier> ['=' <constant>] (<flags>)* (<terminator> <docstring>)?
In function:
<variable definition> ::= <type> <identifier> ['=' <expression>]
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[edit | edit source]
; Defines a variable named "foo" that is an integer
int foo
; Defines a variable named "bar" that starts with a value of 10.5 times x (must be in a function)
float bar = 10.5 * x
; Defines to variables, both named foo, that are distinct
if x
int foo = 10
else
float foo = 10.5
endIf
; Can't access foo out here!
foo = 10 ; fails to compile!
Value Variables[edit | edit source]
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[edit | edit source]
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[edit | edit source]
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[edit | edit source]
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[edit | edit source]
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[edit | edit source]
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