Difference between revisions of "If"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>Max
m (minor correction)
imported>JOG
(gave it a more professional look)
Line 1: Line 1:
The <tt>if</tt> statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify. Oblivion's <tt>if</tt> command is very powerful and comparable to "real" programming languages.
The <tt>if</tt> statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify. Oblivion's <tt>if</tt> command is very powerful and comparable to "real" programming languages.


== Overview ==
== Overview ==
Line 16: Line 17:


The <tt>else</tt> and <tt>elseif</tt> statements are optional.
The <tt>else</tt> and <tt>elseif</tt> statements are optional.


== Comparison Operators ==
== Comparison Operators ==
Line 46: Line 48:


It is important to note that there are no bitwise comparisons available in Oblivion's scripting language.
It is important to note that there are no bitwise comparisons available in Oblivion's scripting language.


== Combining Comparisons ==
== Combining Comparisons ==
Line 78: Line 81:


This is true when either myVar2 is 5 OR both, myVar1 and myVar2 are 1
This is true when either myVar2 is 5 OR both, myVar1 and myVar2 are 1


== Comparisons and Expressions ==
== Comparisons and Expressions ==
Line 108: Line 112:
  IF IsActor != 0 && Flag != 0
  IF IsActor != 0 && Flag != 0


 
Additional Notes:
== Notes ==
Script parser seems to have problems with one-lined else statements. I personally observed how a line
Keep in mind that the script parser doesn't accept one-lined statments. While
  else set varname to somevalue ;(exactly: else set MTseason to 1)
 
caused the script to ignore it completely (MTseason always stayed at 0). Changing it to  
  if condition==1 set varname to 1
  else  
else set varname to 2
   set varname to somevalue
 
immediately solved the problem. I guess you can also use parentheses to enclose the action part of the statement.
works in other programming languages, you need to split this up for Oblivion.
I'm writing this because many programming and scripting languages let you use the original version of the statement without similar problems. You should also know is that there won't be any error messages to warn you, so (since the line appears to be correct) you may spend a lot of time locating the source of the problem.  
 
if condition==1
  set varname to 1
  else
   set varname to 2
endif
 
(it's better readable too...)
 
 
After the condition or the "else" statement the rest of the line is ignored, and there aren't any error messages to warn you, so (since the line appears to be correct) you might spend a lot of time locating the source of the problem.  
   
   


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

Revision as of 11:41, 15 June 2006

The if statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify. Oblivion's if command is very powerful and comparable to "real" programming languages.


Overview

An if statement uses the following syntax:

if expressionA [comparison] expressionB
; test "expressionA [comparison] expressionB" passed
elseif expressionB [comparison] expressionC
; test "expressionB [comparison] expressionC" passed
else
; none of the above tests passed
endif

The else and elseif statements are optional.


Comparison Operators

An if statement may contain one or more comparison operators. Below is a table of valid comparison operators:

Operator Description
== Exactly equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

It is important to note that there are no bitwise comparisons available in Oblivion's scripting language.


Combining Comparisons

Comparisons can be linked together using the following logical operators:

Operator Description Example
&& Logical AND if x == 1 && y == 1 ; considered true only if both x and y equal 1.
|| Logical OR if x == 1 || y == 1 ; considered true unless both x and y equal 0.


Note that "||" is evaluated before "&&", just like "*" is evaluated before "+" in normal algebra.

If you want the && to be evaluated first, you have to include that part in parentheses. For example:

if myVar1 == 1 && myVar2 == 1 || myVar2 == 5

This is true when MyVar1 = 1 AND myVar2 is either 1 or 5.

if (myVar1 == 1 && myVar2 == 1) || myVar2 == 5

This is true when either myVar2 is 5 OR both, myVar1 and myVar2 are 1


Comparisons and Expressions

The comparison operators can be used with any expression that can be evaluated into a number. Assuming "a = 17", "b = 20" and "c = a - b", all of the following expressions work as expected. Parentheses are only needed when they're necessary for mathematical reasons.

IF c == -3 && b == 20
IF c == -3 && b == 20 && a == 17
IF c - 1 == -4 && b == 20 && a == 17
IF a - 20 == 17 - b
IF a - 20 == 17 - b && c + 3 == 0
IF a + 3 == b
IF a - b == c
IF a *4 - b * 4 == c * 4
IF a * ( 5 + c ) - 14 == b
IF 2*(a*(5+c)-14)==b - -b

If a variable or the result of a function returns 1 or 0, or you're just interested in whether the result is 0 or not you don't need to test on "== 1" or "!=0"

IF Done
IF Getisid MyNPC
IF Getitemcount Lockpick
IF IsActor && Flag

Do the same as

IF Done != 0
IF Getisid MyNPC != 0
IF Getitemcount Lockpick != 0
IF IsActor != 0 && Flag != 0


Notes

Keep in mind that the script parser doesn't accept one-lined statments. While

if condition==1 set varname to 1
else set varname to 2

works in other programming languages, you need to split this up for Oblivion.

if condition==1
  set varname to 1
else
  set varname to 2
endif

(it's better readable too...)


After the condition or the "else" statement the rest of the line is ignored, and there aren't any error messages to warn you, so (since the line appears to be correct) you might spend a lot of time locating the source of the problem.