Don't click here unless you want to be banned.

LSL Wiki : LSL101Chapter4

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org
<Int - Fo - Ch1 - Ch2 - Ch3 - Ch 4 - Ch5 - Ch6 - App1 - App2>

Chapter 4: Logic


"What do you mean invalid parameters?! 9,000 gigs of RAM and it can't answer a simple question!"
- Earthworm Jim


LSL is an extremely robust language, but only once you know how to make stuff do what it's supposed to! This section is devoted to teaching the inner workings of how to manipulate variables, syntax, loop behavior, and other useful stuff.

Everyone that knows C or C++ is dismissed, as LSL resembles that to the letter.


Variables Revisited

Now that we've a better understanding of how this stuff works, let's return to variables for a bit.

Like C++, the variable accepting data appears to the left. The rest of the spam appears on the right. For example:

integer this = 1; // Stores "1" into "this"
integer two = 2; // Stores "2" into "two"

this = 1 + 2; // Stores "1 + 2," or "3" into "this"
this = two; // Stores the value of "two" into "this"

Typically, variable math (eg. 1 + 2) can only occur within a function or event. While you can store values fairly easily, the compiler may occasionally get upset.
For this reason, state_entry can be extremely useful.


There are also a series of special commands you may use to change variables. In addition to Linden functions, here are a couple commands you may run across:

integer i = 0; // Just a quick variable

++i; // Increments (adds one to) the value of i
i++; // Same thing, only it does so after the line of code is executed

--i; // Decrements (removes one from) the value of i
i--; // Same thing, except it happens after.

i += 1; // Shorthand for "i equals i plus one." Very handy.
i -= 1; // "i equals i minus one."
i *= 1; // "i equals i times one."
i /= 1; // "i equals i divided by one."

i = TRUE; // "TRUE" is a constant for 1.
i = FALSE; // "FALSE" is a constant for 0.




Equal versus Double-Equal

You may notice that some parts of the code use a single equal (=), whereas others use it twice(==). This is by design.

A single equal sign is used to declare a value for a variable, like the above. A double-equal is used to evaluate a value for a variable, and is generally used in loops and if-else statements (see below).




True/False and If/Else Statements

If and else statements are the meat and potatoes of LSL.

To start such a statement, begin with an if, like so:

if(TRUE)
{
}


Note the curly braces and the "TRUE" in parentheses. This is there for a reason. An If Statement requires a condition to be met and evaluate to TRUE (1). If this condition is not met, the code within the curly braces is ignored completely.

In LSL, an if statement will work with any value greater than zero. For example, "if(15)" would run the code inside. This is an interesting caveat for some coders.

If statements need not have curly braces after them. Sometimes, they're omitted entirely. When this happens, the next line of code is considered to be conditional - that is, it will only run if the condition is met.



How does one meet these conditions? There are a variety of ways:

integer one = TRUE;
integer zero = FALSE;

if(!one)
{
    // This will never execute. In LSL, the exclamation point, or "Not" command,
    // flips the conditional. Not True must equal False, and likewise, not False must be True.
}
if(!zero)
{
    // In other words, this would run
}

if(one == TRUE)
{
    // If one equals true, this statement runs. 
    // The double equal returns one if the values match,
    // and zero if they do not.
}
if(zero == FALSE)
{
    // So this would run too.
}

if(zero < one)
{
    // Here we see another conditional. This is TRUE,
    // since zero IS less than one.
}

key stuff = "Yar";
if(stuff)
{
    // Interestingly, you can toy with the values of keys 
    // and strings with conditionals. Try it!
}

Once you have an if statement, you can place an else statement directly after it.


A simple else will run if the if statement preceeding it fails. You can also use "else if" to create chains.

For example:

if(FALSE)
{
}
else if(FALSE)
{
}
else
{
    // Only this code will run.
}


For more information, see IfElse.



And and Or

Same commands, vastly different execution.

A single amperstand (&) is a binary and. In other words, it will evaluate the bits of the current variable.

A single vertical line (|) is a binary or. This handy device will flip a desired bit.

Double amperstands and vertical lines are comparison ands and ors. They are used in if statements and loops.


Example:

if(change & CHANGED_LINK)
{
    // If change is greater than zero, and the bits of CHANGED_LINK are on, do stuff.
}

llSetStatus(STATUS_PHANTOM | STATUS_PHYSICS, TRUE);
// Using Phantom + Physics bits to define a status.

if(TRUE && FALSE)
{
    // Will never run. Both parts must be TRUE.
}

if(TRUE || FALSE)
{
    // Will run, since only one part needs to be TRUE.
}

No idea what any of this means? Try this and this.




Loop Behavior

There are several ways to create a loop. These are ripped almost straight out of C++.

The while loop runs code while a condition is TRUE. Like an if statement, the code does not run when the condition is false. Unlike an if statement, a while loop will run infinitely until the condition is broken.


For example:

while(TRUE)
{
    // Runs infinitely.
}

integer i = TRUE;
while(i == TRUE)
{
    // Runs once
    i = FALSE;
}


The do-while loop is basically a while loop that always executes once.

do{
    // Runs this no matter what
}
while(FALSE);



The for loop creates a simple flow of events with three parameters, like so:

integer i;
for(i = 0; i < 10; ++i)
{
    // Start i as 0, loop while i is less than ten, add one to i each step.
    // This will run ten times
}


Lost? Try this.





Functions that Return Values

Some functions can be used as values. For example, you may see "number = llSin(2);" Some functions return values, while others do not. These functions can be used as if they were a value.

You can see which ones return values here.





Global Functions

Global functions (or user functions) can also be created. These functions can only be created outside of states, and look something like this:

float i_am_a_function(integer item)
{
    // Here's what we just defined. This function returns a float value.
    // It also accepts one parameter, which is defined here as "item"
    // "item" will act as if it has been declared, since it can be in the start of a function like this.
    item = 2;

    // To call this function in the code, you would write something like "i_am_a_function(1)"

    // Because this function returns a value, you MUST include this line:
    return 0.0; // Returns a float.
}




States

States are very useful. Since there's already an excellent tutorial on it, how about I send you there?




Order of Operations

Within math and if statements, note that certain commands execute in a specific order.

Operation (Higher ones executed first) Description
() [] . Parentheses, Square Brackets, Dot Operator
! ~ ++ -- NOT, One's Complement, Prefix Increment, Prefix Decrement
* / % Multiply, Divide, Modulus
+ - Addition, Subtraction
<< >> Left Shift, Right Shift
< <= > >= Less Than, Less Than Or Equal To, Greater Than, Greater Than or Equal To
== != Comparison Equal, Comparison Not Equal
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
&& Comparison AND
|| Comparison OR
= += -= *= /= %= ++ -- Value functions, Suffix Increment, Suffix Decrement

In general, it is considered good coding practice to just use a whole lot of parentheses if you're unsure at the order of operations.

Phew. Long, wasn't that?

<Int - Fo - Ch1 - Ch2 - Ch3 - Ch 4 - Ch5 - Ch6 - App1 - App2>

Homepage | LSL101 | Tutorial | CrashCourse | Glossary
There are 6 comments on this page. [Display comments/form]