Wednesday, January 28, 2009

Loops

Written 27 January, 2009

Loops

It took me a long time to figure out how to do simple loops in Linden Scripting Language. The wikis weren’t much help—but the free program Scratch finally gave me the proper form. Hopefully this will prove of use to someone:
//-----

  for (i1=0; i1<38;i1++)>//-----
That’s the actual loop. Here it is in a complete script.

//-----

// Chey's Simple Loop

integer i1;
// Define the integer we will use in the loop

default
{
  touch_start(integer total_number)
  // When touched
  {
  for (i1=0; i1<5;i1++)
  // Repeat five times; change the numbers 0 and 5 to change the number of repeats in the loop
  {
  llSay(0, "Cheyenne Palisades");
  // Put your desired actions here, within the brackets
  }
  }
}

//------

The above is exactly what I did with my VIC-20 within five minutes of first turning it on. I’m what—two and a quarter years in Second Life, and I’ve only lately figured out the how to do the same in Linden Scripting Language. Perhaps my brain isn’t what it used to be, but I think it says rather more about LSL than it does about me.

3 comments:

Melissa Yeuxdoux said...

Nope... what it says is that, like a number of other programming languages these days, its syntax and control flow constructs are cribbed from those of the programming language C (which in turn borrowed from BCPL, which is in the grand tradition of Algol 60 inspired programming languages).

Jesse said...

That's what I was going to say! :)

Just for fun, here's an equivalent in Ruby, which is a more natural kind of language.

for num in (1..5)
puts "Cheyenne Palisades"
end


or

(1..5).each do |num|
puts "Cheyenne Palisades"
end



The 1..5 is a range. You could substitute an object that has more meaning, maybe people_nearby and iterate for each person in your vicinity. :) I'm kind of glad to see that LSL is like C, which I already know, but I'm not sure the leading alternative to reality should require knowledge of C. :)

Cheyenne Palisades said...

In good old BASIC it would have been:

10 For k = 1 to 100;
20 Print "Cheyenne Palisades ";
30 Next k