Difference between revisions of "Set"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>JOG
m (clarifications about using "-" as operator or sign)
imported>UDUN
 
(21 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Update}}
Sets a local or global variable to a specified value. This value can be a number or an expression.
Sets a local or global variable to a specified value. This value can be a number or an expression.


'''''Arithmetic Operators:'''''
{|border="1" cellpadding="5" cellspacing="0"
|-
! style="background:#efefef;" | Operator
! style="background:#efefef;" | Description
|-
|    <nowiki>+</nowiki>
| Addition
|-
|    <nowiki>  -</nowiki>
| Subtraction
|-
|    <nowiki>  *</nowiki>
| Multiplication
|-
|    <nowiki>  /</nowiki>
| Division
|-
|    <nowiki>  %</nowiki>
| Modulus (do integer division and return remainder)
|}
==Notes==
*Improper syntax can cause scripts to stall myseriously, such as in the example below (the problem being the extra '134' at the end).
set MyRefVar to SomeOtherRef 134
*The modulos operator "%" is evaluated after multiplication/division but before addition/subtraction
4 * 3 % 2 = 0
4 * (3%2) = 4
1 + 2 % 3 = 3
(1+2) % 3 = 0
*A minus right in front of a number or variable acts as "negative"-sign. When you want to do a subtraction, you need at least one space before and behind the minus. This is the only place where you really need a space between arithmetic operators. (For instance: "a-b" will not compile; it needs to be "a - b")
*When using only numbers in a division, you need to use at least one decimal point to tell the game that you want to use floating point division, without a decimal point the remainder is truncated at the end of the division:
float a
set a to 9/5    ; will set "a" to 1.000
set a to 9.0/5  ; will set "a" to 1.800
*When you want to store the correctly rounded result of a division in an integer-variable, you need to make sure, that the calculation uses floating point (so that the decimal-fraction isn't truncated) and add 0.5:
short a
set a to 9/5          ; will set "a" to 1
set a to 9/5  + 0.5  ; will set "a" to 1
set a to 9.0/5        ; will set "a" to 1
set a to 9.0/5 + 0.5  ; will set "a" to '''2'''
set a to 7.0/5 + 0.5  ; will set "a" to '''1'''
short a
set a to 9
set a to a/5.0 + 0.5  ; will set "a" to '''2'''
short a
float b
set b to 9
set a to b/5  + 0.5  ; will set "a" to '''2'''
*You can also use a comparison as the "value". The variable will then be set either to 1 or 0 depending on whether the condition is true or not.
set goodluck to player.Getav luck > 60
Does the same as
if player.Getav luck > 60
  set goodluck to 1
else
  set goodluck to 0
endif
You can use arithmetic operations on a comparison value, but when you do so you need to put the comparison in parentheses:
set luckbonus to 50*(player.Getav luck > 60)
Does the same as
if player.Getav luck > 60
  set luckbonus to 50
else
  set luckbonus to 0
endif
'''''Other Examples:'''''
  set a to 2
  set a to 2
  set b to a*a
  set b to a*a
Line 8: Line 97:
   
   
  set stage to getstage quest1 + 10
  set stage to getstage quest1 + 10
set weapondrawn to player.isweaponout


As you see, when you put a minus right in front of a variable it will be negated. To acomplish this, the parser needs you to put spaces arount all minuses that are actually operators instead of signs. If you use "a-b" in the above example, the script doesn't compile; it needs to be "a - b".
==Bug==
 
The set function has a bug that will cause the CS to crash if the size of its compiled line is greater than 73 bytes. It's rare to have a '''Set''' line that long, so most people won't run into the bug.  The main place where it appears is when using [[:Category: TSFC| TSFC]] or [[:Category: Pluggy| Pluggy]] to create long strings.


[[Category:Commands]]
[[Category:Commands]]

Latest revision as of 16:47, 10 October 2008

Sets a local or global variable to a specified value. This value can be a number or an expression.


Arithmetic Operators:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
 % Modulus (do integer division and return remainder)


Notes[edit | edit source]

  • Improper syntax can cause scripts to stall myseriously, such as in the example below (the problem being the extra '134' at the end).
set MyRefVar to SomeOtherRef 134
  • The modulos operator "%" is evaluated after multiplication/division but before addition/subtraction
4 * 3 % 2 = 0
4 * (3%2) = 4
1 + 2 % 3 = 3
(1+2) % 3 = 0
  • A minus right in front of a number or variable acts as "negative"-sign. When you want to do a subtraction, you need at least one space before and behind the minus. This is the only place where you really need a space between arithmetic operators. (For instance: "a-b" will not compile; it needs to be "a - b")


  • When using only numbers in a division, you need to use at least one decimal point to tell the game that you want to use floating point division, without a decimal point the remainder is truncated at the end of the division:
float a
set a to 9/5    ; will set "a" to 1.000
set a to 9.0/5  ; will set "a" to 1.800
  • When you want to store the correctly rounded result of a division in an integer-variable, you need to make sure, that the calculation uses floating point (so that the decimal-fraction isn't truncated) and add 0.5:
short a
set a to 9/5          ; will set "a" to 1
set a to 9/5   + 0.5  ; will set "a" to 1
set a to 9.0/5        ; will set "a" to 1
set a to 9.0/5 + 0.5  ; will set "a" to 2
set a to 7.0/5 + 0.5  ; will set "a" to 1
short a
set a to 9
set a to a/5.0 + 0.5  ; will set "a" to 2
short a
float b
set b to 9
set a to b/5   + 0.5  ; will set "a" to 2


  • You can also use a comparison as the "value". The variable will then be set either to 1 or 0 depending on whether the condition is true or not.
set goodluck to player.Getav luck > 60

Does the same as

if player.Getav luck > 60
  set goodluck to 1
else
  set goodluck to 0
endif

You can use arithmetic operations on a comparison value, but when you do so you need to put the comparison in parentheses:

set luckbonus to 50*(player.Getav luck > 60)

Does the same as

if player.Getav luck > 60
  set luckbonus to 50
else
  set luckbonus to 0
endif



Other Examples:

set a to 2
set b to a*a
set c to (b - a)*b - a
set d to ((3* -b+a) - c)/ -2
message "a=%.0f, b=%.0f, c=%.0f, d=%.0f" a b c d     ; ("a=2, b=4, c=6, d=8")

set stage to getstage quest1 + 10

set weapondrawn to player.isweaponout

Bug[edit | edit source]

The set function has a bug that will cause the CS to crash if the size of its compiled line is greater than 73 bytes. It's rare to have a Set line that long, so most people won't run into the bug. The main place where it appears is when using TSFC or Pluggy to create long strings.