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.

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.

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.

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
Mod:Creation Kit/Category:Scripting Mod:Creation Kit/Category:Papyrus Mod:Creation Kit/Category:Papyrus Language Reference