User:ANSI Soderstrom/Misc useful functions

From Second Life Wiki
Jump to navigation Jump to search

Leave a comment

Misc useful functions

Some are useful, some not...

<lsl>

// modify a string in ANSI-C style // returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content string printf(string sentence, list insertation) {

   string modifier = "%s";
   if(llGetListLength(insertation)) {
       integer i;
       integer match;
       for(i=0;i<llGetListLength(insertation);++i) {
           match = llSubStringIndex(sentence,modifier);
           if(~match) {
               sentence = llDeleteSubString(sentence,match,match+llStringLength(modifier)-1);
               sentence = llInsertString(sentence,match,llList2String(insertation,i));
           }
       }
       return sentence;
   } 
   return "Error, the insertation list is empty!\n(sentence: " + sentence + ")";

}

default {

   state_entry() {
       llOwnerSay(printf("Hello %s, How %s you?\n%s is a number.",["Avatar","are",3]));
       // Output:
       // Hello Avatar, How are you? 
       // 3 is a number.
   }

} </lsl>

Multiple Timers in only one 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>