- Ch 4
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.
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:
Equal versus Double-Equal
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.
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:
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;
// 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.
// 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";
// 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.
// 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.
No idea what any of this means? Try this
There are several ways to create a loop. These are ripped almost straight out of C++.
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.
// Runs infinitely.
integer i = TRUE;
while(i == TRUE)
// Runs once
i = FALSE;
loop is basically a while loop that always executes once.
// Runs this no matter what
loop creates a simple flow of events with three parameters, like so:
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 (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.
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?
- Ch 4