Difference between revisions of "Boolean constants"

From Dragon Age Toolset Wiki
Jump to: navigation, search
m (added another example)
(add in a bit about FALSE, as mentioned on talk. Feel free to edit this more, Axe Murderer, my previous trimming was merely an attempt to find a compromise position.)
Line 51: Line 51:
 
</pre>
 
</pre>
  
 +
Note that since the FALSE constant is not ambiguous (only the value 0 ever means "false") you ''can'' test for it without potential side effects. For example:
 +
 +
<pre>
 +
if( IsObjectValid( oObject) == FALSE)
 +
</pre>
 +
 +
However, to avoid the possibility of confusion it might be better to instead use the form:
 +
<pre>
 +
if( !IsObjectValid( oObject) )
 +
</pre>
 
== See also ==
 
== See also ==
 
<!-- This section contains links to articles, functions or constant groups. -->
 
<!-- This section contains links to articles, functions or constant groups. -->

Revision as of 18:53, 28 October 2009

Source: script.ldf
Constant name Type Value Description Source
FALSE int 0 script.ldf
TRUE int 1 script.ldf

Remarks

Note that the DA scripting language does not have an explicit boolean type, and using integers to simulate it in this manner can lead to potentially subtle ambiguity.

If you test for a variable's truth value by testing if it is equal to TRUE you may not get the correct result since TRUE == x is a true expression only when x is 1, yet all non-zero values are also not FALSE; FALSE != x is true when x is not zero. There is an ambiguity whenever x is not 1 even though all non-zero values are treated logically as true. When x is 2, for instance, both TRUE == x and FALSE == x are false statements, leading one to conclude that x is neither TRUE nor FALSE when its value is 2 (or any other value besides 0 and 1)

Examples

Note also that it is never necessary, and furthermore wasteful, to explicitly "test for TRUE" or "test for NOT FALSE" in a boolean expression anyway. One would never write, for example:

if( (x == y) == TRUE ) ...  <-- redundant comparison with TRUE
if( (x == y) != FALSE ) ...  <-- redundant comparison with FALSE

because the sub-expression (x == y) will evaluate to TRUE or FALSE already. One should instead write:

if (x == y) ...
if ( !(x == y) ) ...

If you follow this pattern the ambiguity described in "Remarks" above will never impact your code's behaviour.

The identifiers TRUE and FALSE are only really useful for defining default values of boolean type parameters in a function definition, passing a true value to a function with a boolean parameter when you call it, returning a boolean result from a function, or setting the value of a boolean type variable. There is no other place where you need it.

void Dance( int bChaCha = TRUE)... <-- default value in function def
SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call
return TRUE; <-- return value from a function
int bStillLooking = TRUE; <-- set a variable value

Note that since the FALSE constant is not ambiguous (only the value 0 ever means "false") you can test for it without potential side effects. For example:

if( IsObjectValid( oObject) == FALSE)

However, to avoid the possibility of confusion it might be better to instead use the form:

if( !IsObjectValid( oObject) )

See also