User:ANSI Soderstrom/Threading with LSL

From Second Life Wiki
Jump to navigation Jump to search

Leave a comment

All About : Threating with LSL (Creating Low-Lag-Items)

Some functions in SL creating a lot of lag on the server-side. But the greatest lag is not coming from llSetRot() or something like that, but from loops !!! Do->while and for->to->next loops are the greatest lagproducers at the beginners level in SL. Furthermore its impossible to trigger a Event while a loop is running.

Some people say, timers are the godlike-laggers, but now we learn how to use timers for the opposite :)

We try to rotate the Prim around a angle of 360 degrees and producing no lag, because we wait at every loop for free server-power and can handle events while the loop is running.

Timer Example <lsl> integer LOOP_COUNT; float THREATING_SPEED = 0.001; // 0.25 is nearby a NULL-Lag item !

default {

   state_entry()
   {
       // use the timer event instead of a loop and make sure we can handle events while the loop is running...
       LOOP_COUNT = 360;
       llSetTimerEvent(THREATING_SPEED);
   }
   timer() {
       // save serverPower
       llSetTimerEvent(0);
       // Do a loop (if possible)
       if(LOOP_COUNT) {
           // Rotate the Prim around his axis
           llSetRot(llGetRot()*llEuler2Rot(<0,0,LOOP_COUNT>*DEG_TO_RAD));
           // increment the loop
           --LOOP_COUNT;
           // look for events and do again the loop
           llSetTimerEvent(THREATING_SPEED);
       }
   }

} </lsl>

Sensor Example <lsl> default {

   state_entry()
   {
       llSensor("",NULL_KEY,AGENT,96,PI);
   }
   sensor(integer i) {
       // do something...
       // and AFTER that repeat the Sensor ! (DONT use SensorRepeat)
       llSensor("",NULL_KEY,AGENT,96,PI);
   }

} </lsl>

Multiple Timers in a single Script <lsl> // use more than one timer in a script // by ANSI Soderstrom

// holds the timers list timers = [];

// the timer itself, call it with his name and the order to stop or not after reaching the timer time(string name, integer stopafter) {

   integer pos = llListFindList(timers,[name]);
   if(~pos) {
       if(!((integer)llGetTime()%llList2Integer(timers, pos+1))) {
           llMessageLinked(llGetLinkNumber(),1,llList2String(timers,pos),"");
           if(stopafter) {
               timers = llDeleteSubList(timers, pos, pos+1);
           }            
       }
   }

}

// set a timer, give it a name and a time set_timer(string name, integer time) {

   integer pos = llListFindList(timers,[name]);
   if(~pos) {
       timers = llListReplaceList(timers,[name, time],pos,pos+1);
   } else {
       timers += [name, time];   
   }

}

// start the timers start_timers() {

   llResetTime();
   llSetTimerEvent(1);    

}

default {

   state_entry()
   {    
       set_timer("timer1",10);
       set_timer("timer2",20);
       start_timers();
   }

   timer() {    
       time("timer1",TRUE);
       time("timer2",FALSE);
   }
   
   link_message(integer link_num, integer num, string str, key id) {
       if(num == 1) {
           if(str == "timer1") {
               llOwnerSay("Doing this");   
           }   
           if(str == "timer2") {
               set_timer("timer1",10);
               llOwnerSay("and this");   
           }               
       }   
   }

} </lsl>