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 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. Every variable in Papyrus that is not given an initial value starts with a default value, based on its type. The default values for each type are listed below:
Type | Default Value |
---|---|
Bool | False |
Int | 0 |
Float | 0.0 |
String | "" |
Objects | None |
Arrays | None |
If a variable is declared outside of a function or event, it can only be initialized as a literal. Inside a function or event, expressions can be used in variable initialisations.
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.
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
Object Variables[edit | edit source]
Variables that are objects or arrays are referred to as "object 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!