Unofficial LSL Reference

[[types:boolean]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Boolean

LSL does not support a boolean type per se; instead, its function is performed by an integer. When a variable or parameter is a boolean, it means that it's an integer which represents a truth value of false when it is zero, or true when it is not zero.

Note that the constant TRUE has the value 1, but any value different to 0 (including negatives) will serve as a truth value of true for variables that represent a boolean value.

It is not recommended to test boolean values against TRUE or FALSE. In most cases, that's not necessary. When the variables are properly named, they usually can be better read by omitting such comparisons. Consider the following example:

boolexample1.lsl
integer flag = FALSE;
 
default
{
    touch_start(integer n)
    {
        flag = !flag; // flip the flag
        if (flag == TRUE)
        {
            llTargetOmega(<0, 0, 1>, PI, 1);
        }
        else if (flag == FALSE)
        {
            llTargetOmega(<0, 0, 0>, 0, 0);
        }
    }
}

And now compare it with this version:

boolexample2.lsl
integer rotating;
 
default
{
    touch_start(integer n)
    {
        rotating = !rotating; // flip the rotating status
        if (rotating)
        {
            llTargetOmega(<0, 0, 1>, PI, 1);
        }
        else
        {
            llTargetOmega(<0, 0, 0>, 0, 0);
        }
    }
}

The latter version is more compact, more readable, faster, and uses less memory. In short, it's better in all respects. It works, because when the argument to an if statement is an integer, any value distinct to 0 will be taken as true. The trick for readability is to decide the name of the variable depending on what it represents when the truth value is true; in the latter example, when the variable is true it will represent that the object is rotating, thus the choice rotating for the variable name.

See also