A Scripted Bagatelle

This evening, Lexx handed me a small object made by a fellow named Petr Brandenburg, that alternately flashed yellow and cyan. She couldn’t read the script inside it, so she didn’t know how it worked. She asked if I could figure it out.

Within a few minutes, I had handed her this:

// Neon Flasher Script
// Erbo Evans - 3/23/2008
/////////////////////////////////

// List of colors to cycle through, specified as  tuples.
list colors = [<0.0,1.0,1.0>, <1.0,1.0,0.0>];
float delay = 0.5;  // how long to wait between color changes
integer index;  // counter used to keep track of color state

default
{
    state_entry()
    { // initialize the color index and display the first color
        index = 0;
        llSetColor(llList2Vector(colors,0),ALL_SIDES);

        // set up the timer to flash colors
        llSetTimerEvent(delay);
    } // end state_entry

    timer()
    { // advance to next color and display it
        if (++index>=llGetListLength(colors))
            index = 0;  // wrap around back to beginning if we run off the end
        llSetColor(llList2Vector(colors,index),ALL_SIDES);
    } // end timer
} // end state default

Simple enough to understand. The script itself is basically a timer-driven state machine, using a table (the colors list) to specify the colors to be displayed on the prim (which uses a blank texture on all faces to better show the effect).

As an exercise, I asked her, “How would you extend the script to flash more than two colors?” Because of the way I wrote the code, it’s very easy (just extend the colors list). However, she stared at it and felt her brain begin to melt. I think it was mostly due to the fact that she was running on only four hours sleep, though. 🙂

Advertisements

1 Comment

Filed under Scripthackery

One response to “A Scripted Bagatelle

  1. thelexx

    I ain’t dumb I am sleep deprived! 😛

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s