NWNWiki
Advertisement
NWNWiki
3,718
pages

A logical operator manipulates boolean values (values that can be truth or falsehood). Since NWScript does not have a boolean data type, the values are actually integers, interpreted as boolean values (zero is considered falsehood, while non-zero is considered truth), with the special constants TRUE and FALSE representing canonical values for truth and falsehood. The logical operators in NWScript are &&, ||, and !. These are most often seen in conditionals but are also used when assigning or returning a (logically) boolean value.

The logical-and operator (&&) compares the values of two integers and produces TRUE if neither is zero, FALSE otherwise. That is, this operator produces a true value if the expression on its left and the one on its right are true.

The logical-or operator (||) compares the values of two integers and produces FALSE if neither is non-zero, TRUE otherwise. That is, this operator produces a true value if the expression on its left or the one on its right is true.

The logical-not operator (!) toggles the interpretation of a single integer, turning truth to FALSE and falsehood to TRUE. That is, this operator produces a boolean value that is not the boolean value of the expression on its right.

The doubled character in the "and" and "or" operators is important, as the single-character version is a bitwise operator. If a bitwise operator is used in place of a logical-and or logical-or, then scripts tend to function as intended only when the just the values TRUE and FALSE are involved. Even in that case, using a bitwise operator makes the script less robust, as someone might change it later, introducing the possibility of a value other than TRUE or FALSE being involved.

Evaluating[]

Logical operators use left-to-right "short-circuit" evaluation in NWScript. This means that the computer will stop evaluating expressions once the end result is known. For example, the expression

TRUE  ||  GetName(OBJECT_SELF) == "No one"

will never cause GetName() to be invoked. The computer evaluates the first operand (TRUE) then realizes that whatever the second operand is, the overall expression will be TRUE, so skips evaluating the second operand. Similarly, a false operand causes the evaluation of && to short-circuit. In most cases, this behavior does not cause problems, but occasionally a script will rely on a side-effect of evaluating the second operand, as in

FALSE  &&  ++nCounter < 25

where the value of the variable nCounter is changed when evaluating the second operand. However, since the evaluation is short-circuited, the second expression is never evaluated, so the value of nCounter is not changed. Usually, this is a bug, an oversight by the scripter. Even when the intended behavior is for nCounter to not change when the first expression is false, this is poor programming practice as it relies on a not-obvious mechanism (the short-circuit).

In terms of the order of operations, the logical-not has a high priority and is evaluated as soon as it has something to operate on. (Only grouping with parentheses has a higher priority.) On the other hand, the logical-or and logical-and operators have a low priority, evaluated after most other operators, but before assignment operators and the conditional operator. Furthermore, the logical-and has a higher priority than the logical-or, so the expression TRUE || TRUE && FALSE is evaluated as TRUE || (TRUE && FALSE), which short-circuits to TRUE. It is not evaluated like (TRUE || TRUE) && FALSE, which evaluates to FALSE.

Advertisement