1st necessity of SL

From Second Life Wiki
Revision as of 12:56, 7 October 2012 by Kireji Haiku (talk | contribs) (rewrote example script)
Jump to navigation Jump to search

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>