Timeout-Scheduler

From Second Life Wiki
Revision as of 14:08, 16 November 2010 by Ochi Wolfe (talk | contribs) (Created page with '{{KBwarning|This page and script is currently under construction. Please use it with caution and check back later for updates.}} == What it is == Timeout-Scheduler is a functio...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
KBwarning.png Warning: This page and script is currently under construction. Please use it with caution and check back later for updates.

What it is

Timeout-Scheduler is a function which provides a very versatile tool for handling multiple independent timer events, optionally supplemented with arbitrary data.

Some of the key ideas

  • Easy handling of multiple independent, asynchronous timeout events (for example dialog timeouts for multiple users).
  • One central function (in conjunction with a timer event) for getting, setting, modifying and removing timeout events.
  • Timeout events can be supplemented with additional data which is returned for example when the respective timeout occurs.
  • Hierarchical conditional selection of events, offering many possibilities to do things with very few function calls.

<lsl> ////////// NOTES ///////////////////////

// Timeout-Scheduler v0.1-Dev (2010-11) by Ochi Wolfe

// You may use, change and distribute this code as you wish. // When distributing it in source form, it might still be desirable // to include a note where it is from and what you have changed.

////////// VARIABLES ///////////////////

list schedule = [];

////////// FUNCTIONS ///////////////////

string get(list d, string k, string r) {

   integer i = llListFindList(llList2ListStrided(d, 0, -1, 2), [k]);
   if (~i) return llList2String(d, 2*i+1);
   return r;

}

list sched(key base, list cond, integer time, list data) {

   list    res;
   integer idx = -1;
   integer now = llGetUnixTime();

   if (base) { // Select by conditions
       idx = llListFindList(schedule, [base] + cond);
   } else { // Select next due timeout
       integer til = llList2Integer(schedule, 0);
       if (til && til <= now) idx = 1;
   }

   if (~idx) { // There already exists a timeout
       res = ["_0", llList2String(schedule, idx),
              "_1", llList2String(schedule, idx+1),
              "_2", llList2String(schedule, idx+2)] +
              llParseStringKeepNulls(llList2String(schedule, idx+3), ["\n"], []);
       if (!time) { // Delete existing timeout only if time == 0
           schedule = llDeleteSubList(schedule, idx-1, idx+3);
       }
   }

   if (time > 0) { // If positive time is given, insert new timeout
       while (llGetListLength(cond) < 2) cond += [FALSE];
       schedule = llListSort([now+time, base] + cond + [llDumpList2String(data, "\n")] + schedule, 5, TRUE);
   }

   // Adjust timer if necessary
   integer til = llList2Integer(schedule, 0);
   if (!til || til > now) llSetTimerEvent((!!til) * (til-now));

   return res;

}

////////// STATES //////////////////////

default {

   state_entry() {
       
   }

   timer() {
       list tData;
       while (tData = sched("", [], 0, [])) {
           llOwnerSay("Timeout: " + get(tData, "_0", ""));
       }
   }

} </lsl>