Tes3Mod:If

The UESPWiki – Your source for The Elder Scrolls since 1995
Jump to: navigation, search


if
elseif
else
endif

                if ( LeftValue CompareOp RightValue )
                        < statements >
                elseif ( LeftValue CompareOp RightValue )
                        < statements >
                else
                        < statements >
                endif

       Where:   LeftValue  = Local/global variable or function call
                CompareOp  = ==, <=, <, >, >=, !=
                RightValue = Local/global variable or numeric literal value
                Statements = Zero or more statements to execute

        Type:   System

     Returns:   none

     Example:   if ( GlobalVar >= 10 )
                        ; do stuff
                elseif ( LocalVar < GlobalVar )
                        ; do stuff
                elseif ( player->GetStrength == 100 )
                        ; do stuff
                else
                        ; do stuff
                endif

     Scripts:   

The IF statement block is used to test and execute conditional statements. The elseif/else blocks are both optional. It should be noted that if statements can be used in dialogue results. You should not include any operations (add, subtract, multiply, divide) in either the LeftValue or RightValue fields. Use a temporary variable and a set statement before the if block if you need to use operations.

Update: It is actually possible to use operators and functions on both sides of the comparison operator and references on the left side. In order for this to work correctly you must enclose both sides in brackets as well as have a space on either side of the comparison operator. If you omit either the comparison may not work as expected or even result the script crashing (the compiled output is messed up). Some example of correct and incorrect statements:

     Wrong:     if (  Var1 * Var2 + 10  >=  Var2/109  )     ;Missing brackets
     Correct:   if ( (Var1 * Var2 + 10) >= (Var2/109) )
  
     Wrong:     if ( (Var1 * Var2 + 10)>=(Var2/109)  )     ;Missing spaces around comparison
     Correct:   if ( (Var1 * Var2 + 10) >= (Var2/109) )
  
     Wrong:     if ( (player->GetStrength)>=(player->GetIntelligence)  )     ;Cannot have reference call on right side
     Correct:   if ( (player->GetStrength) >= (TempInt) )

If in doubt it is always safer to assign the expression to a temporary variable and then use that variable in the if() statement. The Construction Set will not warn you with the two wrong examples above (it does give a strange error on the last one).

It is important to remember to use spaces surrounding the '(', compare operator, and ')'. There are known cases where omitting the spaces will cause compiler or runtime errors in the script.

Note: On the official forums Dave Humphrey notes, "An if block is limited to 255 compiled commands which are difficult to convert in the number of lines as each line may end up as one or several commands when compiled. The 255 line limit is just for each block, i.e. if you had an if block with 200 commands followed an elseif with 100 commands and an else with 50 commands, you'd be fine." See Another script character limitation? thread on The Elder Scrolls Forums.