Jump to content

How do you put a timer() inside a if(xxxx=??)


Sari6t
 Share

You are about to reply to a thread that has been inactive for 2638 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello all you smart people

I run into the situation now and then where I would like to have a timer() inside a conditional if else if.  

I just get errors trying it the few ways I can think of. Is it possible to do in the first place?

For example I would like to have a simple agent scanner. It uses llGetAgentList  and was written by someone else. Works a treat and has a timer. I adapted it to be inside an on/off switch but the timer ever had to go in order for it to work. 

The problem is that it wont update , thats what the timer was for.  

Oh wait , maybe a custom function? how about that?

Cheers :)

 

Link to comment
Share on other sites

I don't truly understand what you are trying to do, but the answer is that you can't do that. ;)

You cannot interrupt an event in LSL.  Once you are in the event, it has to finish what it's doing.  You can't hop in and out.  So, for example, if you want to update llGetAgentList every 20 seconds, you need to write

default{    state_entry()    {        llSetTimerEvent(20.0);    }    timer()    {        list lEveryone = llGetAgentList(AGENT_LIST_PARCEL,[]);        llRegionSayTo(llGetOwner(),0,llList2CSV(lEveryone));    }}

If you really wanted to use the most recent list of lEveryone in some other event, you could make lEveryone global.  Suppose, though, that you don't want to get lEveryone every 20 seconds, but you only want to get it if it has been at least a minute since the last person entered the parcel? You could write something like

integer iOldPopulation;default{    state_entry()    {        llSensorRepeat("","",AGENT,40.0,PI,1.0);  //Repeat a 40m sensor every second    }    sensor (integer num)   {        if (num > iOldPopulation)    // Assuming here that we expect no more than 16 people        {            llSetTimerEvent(60.0);        }        iOldPopulation = num;    }    timer()    {        llSetTimerEvent(0.0);        list lEveryone = llGetAgentList(AGENT_LIST_PARCEL,[]);        llRegionSayTo(llGetOwner(),0,llList2CSV(lEveryone));    }}

That's just a quick schematic, of course, but the idea is that you use two "timers" running and engage the second one only if the test in the first one is TRUE.

There are loads of ways you can do this.  Try using llGetTime and llResetTime too.

 

   

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

You may be confusing yourself about how event-driven programs work.

To get past a possible syntax hurdle: timer() is the start of an event handler, which is code that runs when the corresponding event occurs. To arrange for a timer event to be generated, call llSetTimerEvent().

So, to start a timer when some condition applies, call the llSetTimerEvent() function inside the conditional, then when the timer expires processing will proceed to the code in the timer() handler.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

You cannot have an event (timer) inside of a conditional (if) or another event (like state_entry).

If you're trying to turn the timer on and off, you can either:

1. Use llSetTimerEvent(x) to turn the timer on (x is seconds between each time the timer event happens) and then llSetTimerEvent(0) to turn the timer off, or..

2. Use an on/off variable and an "if" check inside of the timer event to check whether or not a scan should happen.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Ah thanks everyone now I get it.

 

Yes, I am ignorant more than confused about how events work but now I totally understand so thank you, my poor brain aches from all this lol ...whew more clarity. 

I keep trying to do thing that are WAYYYYY beyond my ability but what the heck its fun. You should see the grid wide chat /teleport llMap /external mysql url  tardis network thing Im frankensteining together lol.

I'll put all this together and get what I need now for a simple on/off local region scanner.

Thanks again.

 

 

Link to comment
Share on other sites

It sort of works now.

 

So to clarify the original idea it's like this. Imagine you are on your spaceship, of course, and you want to see who is around. You go to your scifi looking panel and hit a button , it shows the Avi's that are near and keeps updating while its on, you are satisfied with the info and turn it off and it goes back to not displaying anything. You then grab space coco and watch netflix.

Here is the mostly working frankenstein code.

list gPeople = [];list gDist = [];integer myswitch;default{    state_entry()    {         myswitch = FALSE;        llSetTimerEvent(2.0);           }    touch_start(integer total_number){    if(myswitch==FALSE)      {        myswitch=TRUE;        llSetTimerEvent(2.0);               }    else if (myswitch==TRUE)    {             llSetText("Region Scanner",<0,1.0,0>, 1.0);        myswitch=FALSE;        llSetTimerEvent(0.0);    }     }    timer()    {         if(myswitch==FALSE)        {              gPeople = llGetAgentList(AGENT_LIST_REGION, []);        gDist = [];        integer x;        integer num = llGetListLength(gPeople);        vector pos = llGetPos();        for(;x<num;x++)            gDist += [llVecDist(pos, llList2Vector(llGetObjectDetails(llList2Key(gPeople, x), [OBJECT_POS]), 0)), llKey2Name(llList2Key(gPeople, x))];          gDist = llListSort(gDist, 2, 1);        llSetText(llDumpList2String(gDist, "\n"), <0,1,0>, 1.0);        myswitch=TRUE;    }          else if (myswitch==TRUE)    {         myswitch=FALSE;    }}} 

 

 

Link to comment
Share on other sites

looks good :)

here ia a lil bit cleaner method of your code, no need to reset the switch etc...

list gPeople = [];list gDist = [];integer k;default{    state_entry()    {   llSetText("Region Scanner: Inactive.",<0,1.0,0>, 1.0);      }    touch_start(integer total_number)    {      if(k = !k)  // IF this is TRUE (meaning 1 ..not 0)...k equals the opposite of k .. if k =1 , it is switched to zero, if it is 0, it is switched to 1      {  llSetTimerEvent(2.0);           llSetText("Region Scanner: Scanning...",<0,1.0,0>, 1.0);           }      else       {  llSetTimerEvent(0.0);               llSetText("Region Scanner: Inactive.",<0,1.0,0>, 1.0);             }        }   timer()   {    gPeople = llGetAgentList(AGENT_LIST_REGION, []);        gDist = [];        integer x;        integer num = llGetListLength(gPeople);        vector pos = llGetPos();        for(;x<num;x++)        {   gDist += [llVecDist(pos, llList2Vector(llGetObjectDetails(llList2Key(gPeople, x), [OBJECT_POS]), 0)), llKey2Name(llList2Key(gPeople, x))];          }        gDist = llListSort(gDist, 2, 1);        llSetText(llDumpList2String(gDist, "\n"), <0,1,0>, 1.0);            }} 
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2638 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...