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

LSL Wiki : CrashCourse7

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are
Chapter Listing
< 1 2 3 4 5 6 7 8 9 >

7) Loops, typecasting (meat and potatoes 2)
Okay, so far, you've learned a fair bit, and with a bit of practice and effort, you can make a bunch of simple scripts already. But most of those scripts will just be reactions to things, without much sophistication. If you want to make more powerful scripts, you'll need to consider loops.

Looping is quite simple, really. It just means a block of code is repeated several times, based on various conditions, usually with something changing each time. Say, for example, that you need to write thank-you's for an event. You have a guest list in front of you, and you gradually work your way down the list, writing a thank you to each person on it. This is a loop.

One of the simplest loops is the while loop. It basically says "as long as this is true, do this".

while ( 1 == 1 )
    llOwnerSay( "Yes, yet again, 1 equals 1" );

This would cause the object to go into something called an infinite loop, which is not advisable. It would repeatedly IM the owner saying "Yes, yet again, 1 equals 1".
A slightly more complex loop is the do while loop.

    llOwnerSay( "Either this is the first execution of this loop, or 1 equals 1" );
} while ( 1 == 1 )

Have you guessed the difference? A do while loop will always execute the code inside it at least once, and then keep on repeating if the condition is true, while a while loop will only execute the code inside it if the condition is true.

The most advanced loop in LSL is the for loop. It's most common usage is to have a counter progress between two values, such as 0-9, executing code each time. However, it can be more complex than that. The syntax of a for loop is:

for (initialization; test; update)

The statement of initialization executes when it first begins, it continues so long as test is true, and after each iteration, the update statement is triggered. That is a pretty abstract level of thinking about it, so I'll give you a more complete example now.

integer n;
for ( n = 0 ; n < 5 ; n++ )
    llOwnerSay( "n = " + (string)n );

This would result in the following being messaged to you:

n = 0
n = 1
n = 2
n = 3
n = 4

Now, you probably noticed the (string)n, and wondered what it did. This is called typecasting. Remember that functions take only certain variable types, and sometimes you have one type that you need to pass to function that requires a different type; suppose you have an integer, and you need to pass a string along to a function, such as llOwnerSay. To convert it, simply put the desired new type in parentheses in front of the variable. A temporary copy of its value will be converted to the type you want. So, I know that llOwnerSay takes a string as its second argument. I typecast the integer n to a string, and then concatenated it with (tacked it onto) the literal string "n = ".

Is it always possible to typecast one thing to another? No. For instance, there is a type called vector, which is an aggregate (group) of 3 floats. (<1.0,1.0,1.0> is an example.) Does it make sense to typecast a vector to a single float? (1.0 is a float.) Nope. How could one number represent three distinct ones? That's nonsense (simply speaking). However, a vector variable could be rendered as a string, right? So, that's fine. In fact, almost everything can be typecast to a string.

It is often useful in linked prim communication to typecast vectors or floats as a key. This is done by typecasting the vector or float as a string and then typecasting that string as a key:

vector alpha = <1.0,2.0,3.0>;
string beta = (string)alpha;
key gamma = (key)beta;
(This can be condensed into one line.)

Hence to decode:

vector alpha = (vector)((string)gamma)

(Comment from reader: I don't understand how that last line is a condensed version of the previous box? Where does it define gamma as a key, and where does it define alpha as <1.0,2.0,3.0>? Please delete my comment if I am just being slow.)
(It is not, it is how to decode it.)
(Comment from reader: Looks to me like a piece got left out. The condensed version of the 3 lines should be:
key gamma = (key)((string)<1.0,2.0,3.0>);
and the vector alpha line is the single line version of getting back to a vector from the key. The point being (I think) that it is easier to pass the key around between functions, than the vector, or that multiple types of things can be passed as keys / strings, and the receiving function figures out what to do with it.)
(It is not supposed to be how to condense it, but how to decode it after being passed in a link message.)

Previous | Next

Homepage | Tutorial | CrashCourse | Glossary
There is no comment on this page. [Display comments/form]