Difference between revisions of "1st necessity of SL"

From Second Life Wiki
Jump to navigation Jump to search
(rewrote example script)
Line 8: Line 8:


<lsl>
<lsl>
// Improve SecondLife's performance!
// Script disables all other not-blacklisted scripts within
// Start with your own(ned) modifyable scripts/objects.
// the same prim when there's nobody in the set scope (area)
//
//
// This will disable your rootprim's script(s) when no-one is around to use/admire it
// measured script time is less than 0.002 ms on average
// thus stop wasting valuable sim-resources
//
// Measured script time is 0.002 ms on average.
//
//
// - don't use it for rental stuff or sorts, not everything is suitable -
// Don't use it for rental stuff or sorts, not everything is suitable.
//
//
// Free to use, please use it. Improvements are welcome.
// Don't reset this script or take it back to your inventory when you're
// // Thank you Norton Burns for the testing and feedback :)
//  not within the set scope (area) or you'll get unpredictable results !!
//
 
// Beer Dailey
 
//
 
// Don't reset this script or take it back in inventory when you're not within the set distance
// scope (area) can be AGENT_LIST_REGION, AGENT_LIST_PARCEL or AGENT_LIST_PARCEL_OWNER
// or you'll get unpredictable results !!
integer scope = AGENT_LIST_REGION;
//  
 
// modify below to suit your needs
// do not go below 10.0 seconds nor above 30.0
float sweepEveryXSeconds = 30.0;
 
list scriptBlacklist = ["some script", "some other script"];
list scriptWhitelist;
 
integer debugScript = FALSE;
 
 
get_list_of_scripts_to_toggle_on_or_off_without_blacklisted_ones()
{
    string thisScript = llGetScriptName();
    scriptWhitelist = [];
 
    integer numberOfScripts = llGetInventoryNumber(INVENTORY_SCRIPT);


//////////////////////////////////
    // return if just thisScript
float  distance = 48;  // scan range
    if (numberOfScripts == 1)
integer delay = 10;     // repeat scan every 10 seconds.
        return;


// added
    integer index;
// (gnore specific scripts, some scripts need to be off after initial setups
    do
// There's a bug that resets scripts in off state after a sim restart, so when activated again they re-initialise.
    {
// I've no fix for that.
        string scriptName = llGetInventoryName(INVENTORY_SCRIPT, index);
//
list ignore_scripts = ["major script", "even worse script"];  


integer debug = FALSE;   // for debugging purposes, set it to TRUE to see what it's controlling
        if (scriptName != thisScript && ~llListFindList(scriptBlacklist, [scriptName]))
        {
            scriptWhitelist += [scriptName];


            if (debugScript)
                llSay(PUBLIC_CHANNEL,
                    "/me [" + thisScript + "]: Script on whitelist: '" + scriptName + "'.");
        }
        else
        {
            if (debugScript)
                llSay(PUBLIC_CHANNEL,
                    "/me [" + thisScript + "]: Script on blacklist: '" + scriptName + "'.");
        }


//////////////////////////////////////
        ++index;
// no need to modify the code below //
    }
//////////////////////////////////////
    while (index < numberOfScripts);
}


integer g_active = FALSE;  // if FALSE disable all other scripts 
toggle_script_states(integer inputInteger)
                          // changed TRUE into FALSE cos of some bug with rezzing, thank you LordGregGreg Back :)
list control_scripts;      // list for all scriptnames
active_scripts( integer active )
{
{
     if(g_active == active) return; else g_active = active; //flip TRUE/FALSE
    string outputState = "FALSE";
     if (inputInteger) outputState = "TRUE";
     integer a;
 
     for ( a = 0; a < llGetListLength(control_scripts); a++)
     string thisScript = llGetScriptName();
     integer numberOfWhitelistedScripts = llGetListLength(scriptWhitelist);
 
    integer index;
    do
     {
     {
         llSetScriptState(llList2String(control_scripts, a), g_active); //(de)activate scripts
         string scriptToChangeState = llList2String(scriptWhitelist, index);
          
        llSetScriptState(scriptToChangeState, inputInteger);
    } 
 
    if (debug) llOwnerSay("Changed: " + llList2CSV(control_scripts) + " to: " + (string)g_active );
         if (debugScript)
            llSay(PUBLIC_CHANNEL,
                "/me [" + scriptName + "]: Changed state of '" + scriptToChangeState + "' to " + outputState);
 
        ++index;
    }
    while (index < numberOfWhitelistedScripts);
}
}
 
default
default
{
{
     state_entry()
     on_rez(integer start_param)
     {
     {
         string myname = llGetScriptName(); //don't add myself into the list
         llResetScript();
        control_scripts = []; 
       
        integer i;
        integer n = llGetInventoryNumber(INVENTORY_SCRIPT); //count scripts
       
        if (n == 1) { llOwnerSay("No other scripts found!"); } else //dont be silly ;)
       
        for(i = 0; i < n; ++i)
        {
            string name = llGetInventoryName(INVENTORY_SCRIPT, i); //parse scriptnames
            if(name != myname) //not my name then continue
            {
              //catch states
              if ( llGetScriptState(name) == TRUE) //not on ignore list & running add it
              {
                  control_scripts += [name]; 
              }
              else ignore_scripts += [name];
            }
        }
        if (debug) llOwnerSay("Controlling: " + llList2CSV(control_scripts) + "\nIgnoring: " + llList2CSV(ignore_scripts));
        llSensorRepeat("", NULL_KEY, AGENT, distance, PI, delay); // how far and how often we scan for avatars
     }
     }
 
    on_rez(integer s)
    {
        if (llGetListLength(control_scripts)== 0 && g_active == TRUE) llResetScript();
        //first time use or reset only when scripts are still active or they'll be added to the ignorelist
    }
     changed(integer change)
     changed(integer change)
     {
     {
         if (change & CHANGED_OWNER) llResetScript(); // catch new owner
         if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
        if (change & CHANGED_INVENTORY) llResetScript(); // catch new scripts
            llResetScript();
     }
     }
 
     sensor(integer num_detected)
     state_entry()
     {
     {
      active_scripts(TRUE); //activate the scripts
        get_list_of_scripts_to_toggle_on_or_off_without_blacklisted_ones();
        llSetTimerEvent(sweepEveryXSeconds);
     }
     }
 
     no_sensor() //no-one around? turn off all controlled scripts except myself
     timer()
     {
     {
      active_scripts(FALSE); //deactivate the scripts
        list agentList = llGetAgentList(scope, []);
        integer numberOfAgentsInSim = llGetListLength(agentList);
 
        if (numberOfAgentsInSim)
            toggle_script_states(TRUE);
        else
            toggle_script_states(FALSE);
     }
     }
}
}
  </lsl>
</lsl>

Revision as of 12:56, 7 October 2012

Ever since I noticed that many sims contain 1000's of wonderful objects with fancy effects and 1000's active scripts that use valuable simresources while no-one is around to enjoy it, I wanted to script something simple to offload those resources and give the visitor a better experience.

That's why I made this little script: '1st necessity of SL' that could and should be used in any project that doesn't need to be active all the time.

We are building a better place, together :)

[Beer Dailey]

<lsl> // Script disables all other not-blacklisted scripts within // the same prim when there's nobody in the set scope (area) // // measured script time is less than 0.002 ms on average // // Don't use it for rental stuff or sorts, not everything is suitable. // // Don't reset this script or take it back to your inventory when you're // not within the set scope (area) or you'll get unpredictable results !!


// scope (area) can be AGENT_LIST_REGION, AGENT_LIST_PARCEL or AGENT_LIST_PARCEL_OWNER integer scope = AGENT_LIST_REGION;

// do not go below 10.0 seconds nor above 30.0 float sweepEveryXSeconds = 30.0;

list scriptBlacklist = ["some script", "some other script"]; list scriptWhitelist;

integer debugScript = FALSE;


get_list_of_scripts_to_toggle_on_or_off_without_blacklisted_ones() {

   string thisScript = llGetScriptName();
   scriptWhitelist = [];
   integer numberOfScripts = llGetInventoryNumber(INVENTORY_SCRIPT);
   // return if just thisScript
   if (numberOfScripts == 1)
       return;
   integer index;
   do
   {
       string scriptName = llGetInventoryName(INVENTORY_SCRIPT, index);
       if (scriptName != thisScript && ~llListFindList(scriptBlacklist, [scriptName]))
       {
           scriptWhitelist += [scriptName];
           if (debugScript)
               llSay(PUBLIC_CHANNEL,
                   "/me [" + thisScript + "]: Script on whitelist: '" + scriptName + "'.");
       }
       else
       {
           if (debugScript)
               llSay(PUBLIC_CHANNEL,
                   "/me [" + thisScript + "]: Script on blacklist: '" + scriptName + "'.");
       }
       ++index;
   }
   while (index < numberOfScripts);

}

toggle_script_states(integer inputInteger) {

   string outputState = "FALSE";
   if (inputInteger) outputState = "TRUE";
   string thisScript = llGetScriptName();
   integer numberOfWhitelistedScripts = llGetListLength(scriptWhitelist);
   integer index;
   do
   {
       string scriptToChangeState = llList2String(scriptWhitelist, index);
       llSetScriptState(scriptToChangeState, inputInteger);
       if (debugScript)
           llSay(PUBLIC_CHANNEL,
               "/me [" + scriptName + "]: Changed state of '" + scriptToChangeState + "' to " + outputState);
       ++index;
   }
   while (index < numberOfWhitelistedScripts);

}

default {

   on_rez(integer start_param)
   {
       llResetScript();
   }
   changed(integer change)
   {
       if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
           llResetScript();
   }
   state_entry()
   {
       get_list_of_scripts_to_toggle_on_or_off_without_blacklisted_ones();
       llSetTimerEvent(sweepEveryXSeconds);
   }
   timer()
   {
       list agentList = llGetAgentList(scope, []);
       integer numberOfAgentsInSim = llGetListLength(agentList);
       if (numberOfAgentsInSim)
           toggle_script_states(TRUE);
       else
           toggle_script_states(FALSE);
   }

} </lsl>