Difference between revisions of "Boolean constants"

From Dragon Age Toolset Wiki
Jump to: navigation, search
(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.)
m (Rewritten)
Line 23: Line 23:
 
== Remarks ==
 
== Remarks ==
 
<!-- This section contains additional comments, observations and known issues. -->
 
<!-- This section contains additional comments, observations and known issues. -->
 +
In [[dascript]] there is no Boolean type. Instead [[int|integer]] values are used to represent the Boolean states: zero represents the '''false''' state and any non-zero integer represents the '''true''' state. For convenience the [[boolean constants]] have been defined in [[script.ldf]] to represent the '''true''' and '''false''' states.
  
Note that the DA scripting language does not have an explicit boolean type, and using [[integer]]s to simulate it in this manner can lead to potentially subtle ambiguity.
+
These constants are used in the following ways in '''script.ldf''', the [[Core Game Resources]] and the [[Single Player Campaign]]:
 +
<dascript>
 +
// Assigning a value to a integer variable.
 +
int bStillLooking = TRUE;
  
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)
+
// Setting the a value of an integer argument in a function call.
 +
SetItemDroppable(oItem, TRUE);
  
== Examples ==
+
// Setting the default value of an integer argument in a function's declaration or definition.
<!-- This section contains examples transcluded from the snippet library. -->
+
void AddAbility(object oObject, int nAbility, int bSendNotification = FALSE);
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:
+
<pre>
+
if( (x == y) == TRUE ) ...  <-- redundant comparison with TRUE
+
if( (x == y) != FALSE ) ...  <-- redundant comparison with FALSE
+
</pre>
+
because the sub-expression (x == y) will evaluate to TRUE or FALSE already. One should instead write:
+
<pre>
+
if (x == y) ...
+
if ( !(x == y) ) ...
+
</pre>
+
  
If you follow this pattern the ambiguity described in "Remarks" above will never impact your code's behaviour.
+
// Setting a return value from an integer function.
 +
return TRUE;
 +
</dascript>
  
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.
+
The constants are also referenced in the comments describing a function's return values, for example, as seen is [[IsObjectValid]], to indicate that the function is constrained to return 0 or 1. This is probably reliable for engine functions (i.e. those in '''script.ldf''') which are presumably converting from a native C++ '''bool''' type but should be treated with scepticism for scripted functions.
<pre>
+
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
+
</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:
+
Finally the constants are also used in boolean expressions, for example, in an '''if''' statement:
  
<pre>
+
<dascript>
if( IsObjectValid( oObject) == FALSE)
+
if(<expression> == TRUE)
</pre>
+
{
 +
    ...
 +
}
 +
</dascript>
 +
 
 +
Typically this will be seen in connection with the various '''Is*''' and '''Has*''' functions, for example, as seen with the use of '''IsObjectValid''' in the '''item_singletarget''' script:
 +
 
 +
<dascript>
 +
    // make sure there is a location, just in case
 +
    if(IsObjectValid(stEvent.oTarget) == TRUE)
 +
    {
 +
        stEvent.lTarget = GetLocation(stEvent.oTarget);
 +
    }
 +
</dascript>
 +
 
 +
While not technically wrong - provided the function returns 0 or 1 as it does here - the comparison with the boolean constant is redundant and is not considered best practice. The above example should have been simplified to:
 +
 
 +
<dascript>
 +
    // make sure there is a location, just in case
 +
    if(IsObjectValid(stEvent.oTarget))
 +
    {
 +
        stEvent.lTarget = GetLocation(stEvent.oTarget);
 +
    }
 +
</dascript>
 +
 
 +
The rationale for avoiding comparisons with the boolean constants, specifically with the '''TRUE''' constant, is because the '''TRUE''' constant is only one of many values that can represent the '''true''' state and therefore can introduce ambiguity or unintended behaviour (i.e. a bug).
 +
 
 +
An example of this might be where an include file contains the following function:
 +
 
 +
<dascript>
 +
int HasSuperValuableGems(object oCreature)
 +
{
 +
    return CountItemsByTag(oCreature, "SuperValuableGem");
 +
}
 +
</dascript>
 +
 
 +
And the action script or plot script contains the following:
 +
 
 +
<dascript>
 +
// The PC needs a "super valuable gem" in order to bribe the noble!
 +
if(HasSuperValuableGems(oHero) == TRUE)
 +
{
 +
    ...
 +
}
 +
</dascript>
 +
 
 +
The logic may appear correct however because the function is not constrained to return a boolean constant but is being compared against one the result will be that the PC can only bribe the noble when they have EXACTLY one gem which is probably not the intended behaviour. Removing the comparison against the '''TRUE''' constant would resolve this (although it could also be argued that the function should also be renamed to better indicate its behaviour).
 +
 
 +
Note that while comparisons against the '''FALSE''' constant do not suffer from the same ambiguity (as both it and the '''false''' state only ever equate to zero) they are nonetheless redundant and should be simplified using the [[logical negation operator]]:
 +
 
 +
<dascript>
 +
    // Replace this:
 +
    if(IsObjectValid(oTarget) == FALSE)
 +
    {
 +
        ...
 +
    }
 +
 
 +
 
 +
    // With this:
 +
    if(!IsObjectValid(oTarget))
 +
    {
 +
        ...
 +
    }
 +
</dascript>
 +
 
 +
In conclusion it is best to only use the boolean constants for the following purposes:
 +
# Assigning a value to a integer variable.
 +
# Setting the a value of an integer argument in a function call.
 +
# Setting the default value of an integer argument in a function's declaration or definition.
 +
# Setting a return value from an integer function.
  
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. -->
*See [[bool (2da type)]] for booleans used in [[2DA]]s.
+
*[[bool (2da type)]] for boolean values in [[2DA]]s
  
 
[[Category:Constants]]
 
[[Category:Constants]]

Revision as of 18:28, 13 April 2020

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

Remarks

In dascript there is no Boolean type. Instead integer values are used to represent the Boolean states: zero represents the false state and any non-zero integer represents the true state. For convenience the boolean constants have been defined in script.ldf to represent the true and false states.

These constants are used in the following ways in script.ldf, the Core Game Resources and the Single Player Campaign:

// Assigning a value to a integer variable.
int bStillLooking = TRUE;
 
// Setting the a value of an integer argument in a function call.
SetItemDroppable(oItem, TRUE);
 
// Setting the default value of an integer argument in a function's declaration or definition.
void AddAbility(object oObject, int nAbility, int bSendNotification = FALSE);
 
// Setting a return value from an integer function.
return TRUE;

The constants are also referenced in the comments describing a function's return values, for example, as seen is IsObjectValid, to indicate that the function is constrained to return 0 or 1. This is probably reliable for engine functions (i.e. those in script.ldf) which are presumably converting from a native C++ bool type but should be treated with scepticism for scripted functions.

Finally the constants are also used in boolean expressions, for example, in an if statement:

if(<expression> == TRUE)
{
    ...
}

Typically this will be seen in connection with the various Is* and Has* functions, for example, as seen with the use of IsObjectValid in the item_singletarget script:

    // make sure there is a location, just in case
    if(IsObjectValid(stEvent.oTarget) == TRUE)
    {
        stEvent.lTarget = GetLocation(stEvent.oTarget);
    }

While not technically wrong - provided the function returns 0 or 1 as it does here - the comparison with the boolean constant is redundant and is not considered best practice. The above example should have been simplified to:

    // make sure there is a location, just in case
    if(IsObjectValid(stEvent.oTarget))
    {
        stEvent.lTarget = GetLocation(stEvent.oTarget);
    }

The rationale for avoiding comparisons with the boolean constants, specifically with the TRUE constant, is because the TRUE constant is only one of many values that can represent the true state and therefore can introduce ambiguity or unintended behaviour (i.e. a bug).

An example of this might be where an include file contains the following function:

int HasSuperValuableGems(object oCreature)
{
    return CountItemsByTag(oCreature, "SuperValuableGem");
}

And the action script or plot script contains the following:

// The PC needs a "super valuable gem" in order to bribe the noble!
if(HasSuperValuableGems(oHero) == TRUE)
{
    ...
}

The logic may appear correct however because the function is not constrained to return a boolean constant but is being compared against one the result will be that the PC can only bribe the noble when they have EXACTLY one gem which is probably not the intended behaviour. Removing the comparison against the TRUE constant would resolve this (although it could also be argued that the function should also be renamed to better indicate its behaviour).

Note that while comparisons against the FALSE constant do not suffer from the same ambiguity (as both it and the false state only ever equate to zero) they are nonetheless redundant and should be simplified using the logical negation operator:

    // Replace this:
    if(IsObjectValid(oTarget) == FALSE)
    {
        ...
    }
 
 
    // With this:
    if(!IsObjectValid(oTarget))
    {
        ...
    }

In conclusion it is best to only use the boolean constants for the following purposes:

  1. Assigning a value to a integer variable.
  2. Setting the a value of an integer argument in a function call.
  3. Setting the default value of an integer argument in a function's declaration or definition.
  4. Setting a return value from an integer function.

See also