Table of Contents

Change Log

26 August 2013

  • Switched to asynchronous switching between states for better compatibility with OpenSim.

Shortnote

The following script turns a primitive from red to green and vice-versa when touched. It uses the percent to gradient function.

Demonstration

This demonstrates the script below:

Code

bistable_color
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
 
vector wasPercentToGradient(float percent, string rgb) {
    if(llStringLength(rgb) != 2) {
        llSay(DEBUG_CHANNEL, "Assert failed, rgb parameter must consist of a pair of either r, g, or b.");
        return ZERO_VECTOR;
    }
    string a = llGetSubString(rgb, 0, 0);
    string b = llGetSubString(rgb, 1, 1);
    list col = [ "r", "g", "b" ];
    integer ax = llListFindList(col, (list)a);
    integer bx = llListFindList(col, (list)b);
    if(ax == -1 || bx == -1) {
        llSay(DEBUG_CHANNEL, "Asset failed, rgb parameters must contain either r, g, or b letters.");
        return ZERO_VECTOR;
    }
    col = llListReplaceList(col, (list)((100-percent)/100), ax, ax);
    col = llListReplaceList(col, (list)(percent/100), bx, bx);
    return 2*<llList2Float(col, 0), llList2Float(col, 1), llList2Float(col, 2)>;
}
 
integer i;
 
default
{
    state_entry() {
        i = 100; llSetTimerEvent(0.01);
    }
 
    timer() {
        if(i==0) llSetTimerEvent(0);
        llSetColor(wasPercentToGradient(i--, "rg"), ALL_SIDES);
    }
 
    touch_start(integer num) {
        state green;
    }
}
 
state green
{
    state_entry() {
        i = 100; llSetTimerEvent(0.01);
    }
    timer() {
        if(i==0) llSetTimerEvent(0);
        llSetColor(wasPercentToGradient(i--, "gr"), ALL_SIDES);
    }
    touch_start(integer num) {
        state default;
    }
}