Editing
Variable Reference
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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 File Structure|script]], or inside a specific [[Function Reference|function]] or block. == Defining Variables == 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 Reference|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. {{:Default Value Reference}} If a variable is declared outside of a function or event, it can only be initialized as a [[Literals_Reference|literal]]. Inside a function or event, [[Expression_Reference|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 [[Flag Reference|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 === <syntaxhighlight lang="papyrus"> ; Defines a variable named "foo" that is an integer int foo </syntaxhighlight> <br> <syntaxhighlight lang="papyrus"> ; 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 </syntaxhighlight> <br> <syntaxhighlight lang="papyrus"> ; Defines two 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! </syntaxhighlight> == 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 === <syntaxhighlight lang="papyrus"> 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 </syntaxhighlight> == Object Variables == 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 === <syntaxhighlight lang="papyrus"> 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! </syntaxhighlight> [[Category:Scripting]] [[Category:Papyrus]] [[Category:Papyrus Language Reference]]
Summary:
Please note that all contributions to the CreationKit Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
CreationKit:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Default Value Reference
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Landing page
Tutorials
Game Systems
Editor Reference
Scripting Reference
Recent changes
Help
Tools
What links here
Related changes
Special pages
Page information
Page values