Difference between revisions of "Operator Reference"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>IsharaMeradin
(Changed all source lang tags from papyrus to text because papyrus is unsupported and results in displaying error boxes which throw off information flow.)
imported>Egocarib
 
(3 intermediate revisions by 2 users not shown)
Line 12: Line 12:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; i will get the value of 9
; i will get the value of 9
i = (1 + 2) * 3
i = (1 + 2) * 3
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; call the function MyFunction with a parameter of 10
; call the function MyFunction with a parameter of 10
MyFunction(10)
MyFunction(10)
Line 27: Line 27:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; i whatever is in element 0 of the array
; i whatever is in element 0 of the array
i = myArray[0]
i = myArray[0]
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; define an array of size 10
; define an array of size 10
int[] myArray = new int[10]
int[] myArray = new int[10]
Line 42: Line 42:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; call the function MyFunction with a parameter of 10 and 20
; call the function MyFunction with a parameter of 10 and 20
MyFunction(10, 20)
MyFunction(10, 20)
Line 49: Line 49:
== Math Operators ==
== Math Operators ==
  + - * / %
  + - * / %
Math operators perform some sort of operation on the two [[Expression Reference|expressions]] it sits between. Papyrus supports assignment, addition, subtraction, multiplication, division, and integer modulus. The minus sign is also used for negation (also known as unary minus). The addition operator can also be used to concatenate ('paste together') strings. If you try to divide by zero or find the remainder of a divide by zero through modulus the result is undefined and the game will spit an error to the log.
Math operators perform some sort of operation on the two [[Expression Reference|expressions]] it sits between. Papyrus supports assignment, addition, subtraction, multiplication, division, and integer modulus. The minus sign is also used for negation (also known as unary minus). The addition operator can also be used to concatenate ('paste together') strings. If you try to divide by zero or find the remainder of a divide by zero through modulus the result is undefined and the game will spit an error to the log. Integer division will return an integer value and discard any remainders.


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; i will get the value of 7
; i will get the value of 7
i = 1 + 2 * 3
i = 1 + 2 * 3
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; i will get the value of 1
; i will get the value of 1
i = 9 % 4
i = 9 % 4
; i will get the value of -3
i = -7 % 4
</source>
<br>
<source lang="papyrus">
; i will get the value of 4
i = 29 / 6
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; i will get the string "Hello World"
; i will get the string "Hello World"
i = "Hello " + "World"
i = "Hello " + "World"
Line 68: Line 76:
<br>
<br>
The next example ''will not compile'' as perhaps expected because the minus sign is also used for negation
The next example ''will not compile'' as perhaps expected because the minus sign is also used for negation
<source lang="text">
<source lang="papyrus">
int x
int x
int y
int y
Line 80: Line 88:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; Call the function on the variable
; Call the function on the variable
myVariable.MyFunction()
myVariable.MyFunction()
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; Set the property to 10
; Set the property to 10
myVariable.MyProperty = 10
myVariable.MyProperty = 10
Line 95: Line 103:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; A basic hello world string
; A basic hello world string
hello = "Hello World!"
hello = "Hello World!"
Line 109: Line 117:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; i gets true if both a and b are true
; i gets true if both a and b are true
i = a && b
i = a && b
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; i gets true if a is false
; i gets true if a is false
i = !a
i = !a
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; short-circuit can make sure a reference exists before doing something with it
; short-circuit can make sure a reference exists before doing something with it
if ref && ref.HasKeyword(VendorItemClutter)
if ref && ref.HasKeyword(VendorItemClutter)
Line 135: Line 143:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; i will get true if a is greater then b
; i will get true if a is greater then b
i = a > b
i = a > b
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; i will get true if a is equal to b
; i will get true if a is equal to b
i = a == b
i = a == b
Line 150: Line 158:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; assign 10 to i
; assign 10 to i
i = 10
i = 10
</source>
</source>
<br>
<br>
<source lang="text">
<source lang="papyrus">
; add 1 to the current value of i, and assign it to i
; add 1 to the current value of i, and assign it to i
i += 1
i += 1
</source>
</source>


Only the = operator can be used with an [[Arrays_(Papyrus)|array]]:
Only the = operator can be used to assign values to an [[Arrays_(Papyrus)|array]] element:
<source lang="text">
<source lang="papyrus">
  ; Will not compile, as the compiler doesn't know how to handle it.
  ; Will not compile, as the compiler doesn't know how to handle it.
  myArray[3] += 5
  myArray[3] += 5
Line 171: Line 179:


Examples:
Examples:
<source lang="text">
<source lang="papyrus">
; cast a to a float and assign it to i
; cast a to a float and assign it to i
i = a as float
i = a as float

Latest revision as of 20:38, 24 April 2014

The following are valid operators in the Papyrus language:

() [] ,  =  +  -
*  /  %  .  "" !
== != >  <  >= <=
|| && += -= *= /=
%= as

Parenthesis[edit | edit source]

()

Parenthesis, when surrounding an expression, will cause that expression to be evaluated before any operators outside of the parenthesis. When following a function name, they denote the parameter list for the function.

Examples:

; i will get the value of 9
i = (1 + 2) * 3


; call the function MyFunction with a parameter of 10
MyFunction(10)

Square Brackets[edit | edit source]

[]

Square brackets are used for arrays. Either for accessing a specific array element, denoting the size of an array for a new one, or for denoting that a type is an array type.

Examples:

; i whatever is in element 0 of the array
i = myArray[0]


; define an array of size 10
int[] myArray = new int[10]

Comma[edit | edit source]

,

The comma operator separates parameters in a function definition, or a function call.

Examples:

; call the function MyFunction with a parameter of 10 and 20
MyFunction(10, 20)

Math Operators[edit | edit source]

+ - * / %

Math operators perform some sort of operation on the two expressions it sits between. Papyrus supports assignment, addition, subtraction, multiplication, division, and integer modulus. The minus sign is also used for negation (also known as unary minus). The addition operator can also be used to concatenate ('paste together') strings. If you try to divide by zero or find the remainder of a divide by zero through modulus the result is undefined and the game will spit an error to the log. Integer division will return an integer value and discard any remainders.

Examples:

; i will get the value of 7
i = 1 + 2 * 3


; i will get the value of 1
i = 9 % 4

; i will get the value of -3
i = -7 % 4


; i will get the value of 4
i = 29 / 6


; i will get the string "Hello World"
i = "Hello " + "World"


The next example will not compile as perhaps expected because the minus sign is also used for negation

int x
int y
x = 22
y = x-1 ; compiler: no viable alternative at input 'x'

Dot[edit | edit source]

.

The dot operator goes after a variable name to let you call functions on it, or to access properties.

Examples:

; Call the function on the variable
myVariable.MyFunction()


; Set the property to 10
myVariable.MyProperty = 10

Double Quotes[edit | edit source]

""

Double quotes surround a string literal.

Examples:

; A basic hello world string
hello = "Hello World!"

Logical Operators[edit | edit source]

! || &&

Logical operators evaluate to true or false values, based on their expressions.

  • The NOT operator (!) will be true if its single expression (to the right of the operator) is false, and false if it is true.
  • The OR operator (||) will be true if one of the expressions to its left and right are true, and will short-circuit if the left expression is true (it will not evaluate the right expression at all).
  • Note that If A == (B || C || D) is a common mistake, it should be If A == B || A == C || A == D, read more here
  • The AND operator (&&) will be true if both of the expressions to its left and right are true, and will short-circuit if the left expression is false (it will not evaluate the right expression at all).

Examples:

; i gets true if both a and b are true
i = a && b


; i gets true if a is false
i = !a


; short-circuit can make sure a reference exists before doing something with it
if ref && ref.HasKeyword(VendorItemClutter)

Comparison Operators[edit | edit source]

== != < > <= >=

The comparison operators compare both expressions to their left and right, and return a boolean value based on the operator used. If floating-point values are being compared, a tiny epsilon value is accounted for to conteract floating-point error.

  • Equality (==) returns true if both expressions have equal values.
  • Inequality (!=) returns true if both expressions have inequal values.
  • Less-than (<) returns true if the left expression's value is smaller then the right expression's value.
  • Greater-than (>) returns true if the left expression's value is larger then the right expression's value.
  • Less-than or equal to (<=) returns true if the left expression's value is smaller than or equal to the right expression's value.
  • Greater-than or equal to (>=) returns true if the left expression's value is larger than or equal to the right expression's value.

Examples:

; i will get true if a is greater then b
i = a > b


; i will get true if a is equal to b
i = a == b

Assignment Operators[edit | edit source]

= += -= *= /= %=

Assignment operators assign the value from the right expression to the variable (or property) from the left expression. If the equal sign starts with one of the math operators, then the right expression's value will be added to the current value of the left expression, and then assigned to the left expression. Note that if the left expression is a property, then both the property's get and set functions will be called.

Examples:

; assign 10 to i
i = 10


; add 1 to the current value of i, and assign it to i
i += 1

Only the = operator can be used to assign values to an array element:

 ; Will not compile, as the compiler doesn't know how to handle it.
 myArray[3] += 5

Cast[edit | edit source]

as

The cast operator attempts to cast the value from the left expression to the type to the right of it, and returns the resulting value.

Examples:

; cast a to a float and assign it to i
i = a as float

Operator Precedence[edit | edit source]

Operator precedence is listed in the following table, from highest to lowest:

() Mathematical parenthesis
. Dot operator (for accessing functions and properties)
as Cast operator
- ! Unary minus and logical NOT
* / % Multiplication, division, and modulus
+ - Addition and subtraction
== != > < >= <= Comparison operators
&& Logical AND
|| Logical OR
= += -= *= /= %= Assignment