|

All of the relational operators can be applied to any two numbers or any two strings. All other values can only use the == operator to see if they are equal.


Relational operators return Boolean values (true or false). For example:


10 > 20; -- resolves to false


a = 10;

a > 300; -- false


(3 * 200) > 500; -- true

"Brett" ~= "Lorne" -- true


One important point to mention is that the == and ~= operators test for complete equality, which means that any string comparisons done with those operators are case sensitive. For example:


"Jojoba" == "Jojoba"; -- true "Wildcat" == "wildcat"; -- false

"I like it a lot" == "I like it a LOT"; -- false "happy" ~= "HaPPy"; -- true

Logi ca l Op era t o r s

Logical operators are used to perform Boolean operations on Boolean values. The following logical operators are supported:


and (only true if both values are true) or (true if either value is true)

not (returns the opposite of the value)


For example:


a = true; b = false;

c = a and b; -- false d = a and nil; -- false e = not b; -- true


Note that only nil and false are considered to be false, and all other values are true. For example:

iaminvisible = nil; if iaminvisible then

-- any lines in here won't happen

-- because iaminvisible is considered false Dialog.Message("You can't see me!", "I am invisible!!!!");

end


if "Brett" then

-- any lines in here WILL happen, because only nil and false

-- are considered false...anything else, including strings,

-- is considered true

Dialog.Message("What about strings?", "Strings are true.");

end


Th e L e ng t h Op era t o r

The length unary operator # can be used to get the length of a string, for example: nLength = #"Hey"; -- This results in nLength being 3.

C on ca t e n a t ion

In AutoPlay scripting, the concatenation operator is two periods (..). It is used to combine two or more strings together. You don't have to put spaces before and after the periods, but you can if you want to.


For example:


name = "Joe".." Blow"; -- assigns "Joe Blow" to name

b = name .. " is number " .. 1; -- assigns "Joe Blow is number 1" to b


Op era t o r P rece d e n ce

Operators are said to have precedence, which is a way of describing the rules that determine which operations in a series of expressions get performed first. A simple example would be the expression 1 + 2 * 3. The multiply (*) operator has higher precedence than the add (+) operator, so this expression is equivalent to 1 + (2 * 3). In other words, the expression 2 * 3 is performed first, and then 1 + 6 is performed, resulting in the final value 7.


You can override the natural order of precedence by using parentheses. For instance, the expression (1 + 2) * 3 resolves to 9. The parentheses make the whole sub-expression "1 + 2" the


left value of the multiply (*) operator. Essentially, the sub-expression 1 + 2 is evaluated first, and the result is then used in the expression 3 * 3.


Operator precedence follows the following order, from lowest to highest priority:


or

and

<

>

<=

>=

~=

==

..


%

- (unary)

+

-

*

/

not

#

^


Operators are also said to have associativity, which is a way of describing which expressions are performed first when the operators have equal precedence. In the script engine, all binary operators are left associative, which means that whenever two operators have the same precedence, the operation on the left is performed first. The exception is the exponentiation operator (^), which is right-associative.


When in doubt, you can always use explicit parentheses to control precedence. For example:


a + 1 < b/2 + 1


...is the same as:


(a + 1) < ((b/2) + 1)


...and you can use parentheses to change the order of the calculations, too:


a + 1 < b/(2 + 1)


In this last example, instead of 1 being added to half of b, b is divided by 3.


C o n t r o l S t r u c t u r es


Control Structures

The scripting engine supports the following control structures: if, while, repeat and for.


I f

An if statement evaluates its condition and then only executes the "then" part if the condition is true. An if statement is terminated by the "end" keyword. The basic syntax is:


if condition then

do something here

end


For example:


x = 50;

if x > 10 then

Dialog.Message("result", "x is greater than 10");

end


y = 3;

if ((35 * y) < 100) then

Dialog.Message("", "y times 35 is less than 100");

end


In the above script, only the first dialog message would be shown, because the second if condition isn't true...35 times 3 is 105, and 105 is not less than 100.


You can also use else and elseif to add more "branches" to the if statement:


x = 5;

if x > 10 then

Dialog.Message("", "x is greater than 10");

else


end


Dialog.Message("", "x is less than or equal to 10");


In the preceding example, the second dialog message would be shown, because 5 is not greater than 10.


x = 5;

if x == 10 then

Dialog.Message("", "x is exactly 10"); elseif x == 11 then

Dialog.Message("", "x is exactly 11"); elseif x == 12 then

Dialog.Message("", "x is exactly 12");

else


end


Dialog.Message("", "x is not 10, 11 or 12");


In that example, the last dialog message would be shown, because x is not equal to 10, or 11, or 12.


Whil e

The while statement is used to execute the same "block" of script over and over until a condition is met. Like if statements, while statements are terminated with the "end" keyword. The basic syntax is:


while condition do

do something here

end


The condition must be true in order for the actions inside the while statement (the "do something here" part above) to be performed. The while statement will continue to loop as long as this condition is true. Here's how it works:


If the condition is true, all of the actions between the "while" and the corresponding "end" will be performed. When the "end" is reached, the condition will be reevaluated, and if it's still true, the actions between the "while" and the "end" will be performed again. The actions will continue to loop like this until the condition evaluates to false.


For example:


a = 1;

while a < 10 do a = a + 1;

end


In the preceding example, the "a = a + 1;" line would be performed 9 times.


You can break out of a while loop at any time using the "break" keyword. For example:


count = 1;

while count < 100 do count = count + 1; if count == 50 then

break;


end

end


Although the while statement is willing to count from 1 to 99, the if statement would cause this loop to terminate as soon as count reached 50.


Re p ea t

The repeat statement is similar to the while statement, except that the condition is checked at the

end of the structure instead of at the beginning. The basic syntax is:


repeat

do something here

until condition


|