Single Prim Texture Changer

// Texture changer.  Will cycle through all textures
// in the prims inventory.
//
// Kimm Paulino
// Written for Bebelou Naidoo, April 2010
 
float TIMER_PERIOD = 5.0; // in seconds.
integer RANDOM = 1;     // 0 = sequential, 1 = random
 
// globals
integer gNumTextures;
integer gLastTexture;
 
default
{
    on_rez (integer n)
    {
        llResetScript();
    }
 
    state_entry()
    {
        gLastTexture = 0;
        gNumTextures = llGetInventoryNumber(INVENTORY_TEXTURE);
        llSetTimerEvent(TIMER_PERIOD);
    }
 
    timer()
    {
        integer nextTexture;
        if (RANDOM)
        {
            // Randomly choose one
            nextTexture = (integer)llFrand (gNumTextures);
        }
        else
        {
            // Move on from the last one
            gLastTexture ++;
            if (gLastTexture > gNumTextures)
            {
                gLastTexture = 0;
            }
            nextTexture = gLastTexture;
        }
        string texture = llGetInventoryName(INVENTORY_TEXTURE, nextTexture);
        if (texture != "")
        {
            llSetTexture(texture, ALL_SIDES);
        }
    }
 
    changed (integer change)
    {
        if (change & CHANGED_INVENTORY)
        {
            llResetScript();
        }
    }
}