Jump to content

llSensor variables changing after sensor is initiated


Raena Parx
 Share

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

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

Recommended Posts

Hello. I'm using llSensor for the first time. My doctor wears a bracelet that sends commands to my button (this script that uses llSensor). Before any command is processed, this script (button) checks to be sure the doctor is within range of 2.5 meters.  I had a problem with finding where to place the commands llSensor() and sensor ().  I believe they are placed correctly, as the sensor does detect whether the doctor is close or not.  However, the calling llSensor() command is later in the script. 

When the llSensor() command calls the sensor() subroutine, it detects the doctor and assigns a value of   isDoctorNear=1   . This works fine, and debugging show that.  But...

The problem is once the sensor() subroutine is completed, the value of isDoctorNear ALWAYS comes up zero, even tho it was detected in the sensor() routine.  This is the line directly after llSensor().
I cannot figure out why the value of isDoctorNear=1, and then the next script line llOwnerSay show it as zero value.

It's a huge script, so I dummied it down to bare bones to show the problem.  It most likely will not run as-is, but is more a placeholder so you can see the structure of the code.

Any anyone explain why the isDoctorNear value changes on the next line after llSensor() ?  And how to fix it?

Thank you so much!

Quote

//Doctor touches a bracelet that sends message to this button (this script).
//Script/button checks if message was sent by doctor.
//Then script/button checks if the doctor is within range (2.5 meters).

//--intial parameters here

//--functions here 

//--doctor touches the button (this object) and pairs to this button, assigning themself as the doctor.

//------------- (STATE PAIRED) --------------- 
state PAIRED  //start listening
{


    sensor (integer num_detected)  //***sensor will check if paired doctor is withing 2.5 meters of this script.
    { 
       string message = "Detected " + (string)num_detected + " avatar(s): " + llDetectedName(0);

        isDoctorNear=0;   // start new search
        
        integer index = 1;
        while (index < num_detected)
            DetectedName=llDetectedName(index++); CurrentName=llDetectedName(index-1);
            message += ", " + DetectedName;

            //check if searched avatar is found in results
                if (doctorName==CurrentName) 
                    {
                    isDoctorNear = 1; //isDoctorNear will be 1 if doctor is in range, or 0 if too far away
                    foundName=CurrentName;
                    }
    
           // llOwnerSay ("State Paired + sensor, isDoctorNear="+(string)isDoctorNear);  //debug
        
    }

    no_sensor()
    {
        isDoctorNear=0;  //doctor is not found within range
        llRegionSayTo(doctorKey, PUBLIC_CHANNEL, "*Out of Range* Please come closer to the VSM Exam Table.");    
           // llOwnerSay ("State Paired + NO sensor, isDoctorNear="+(string)isDoctorNear);  //debug            
        state PAIRED;  // start over
    }    
    
   
    
  state_entry()  //for paired state
  {
   
   // llOwnerSay("state PAIRED");
    //LISTENERS 
    llListen (ChInCmdLine220, "", "", "");  //COMMAND CHANNEL - listens to bracelet on CH -220 for commands
   

   }  //end state_entry
    
  listen (integer channel, string name, key id, string message)
  {

 if (ChInCmdLine220==channel)  //CH -220  //=================START CHANNEL -220/COMMAND
 {    

        //***Checks if paired doctor is within range of 2.5 meters  each time a command is heard from bracelet
        llSensor(doctorName, NULL_KEY, AGENT_BY_LEGACY_NAME, 2.5, PI);   //***<---CALLING LLSENSOR()  

        //***PROBLEM: isDoctorName ALWAYS TURNS TO ZERO AFTER llSensor command above, but should be 1 when doctor is in range.
       // llOwnerSay ("After Sensor: isDoctorNear="+(string)isDoctorNear); //DEBUG

 
        if (isDoctorNear==0) 
            {
            state PAIRED;  //doctor not in range - go back to beginnine
            } else{
            //do nothing and move foward to command processing below
            }


 
//-------------------
//--Process commands here based on command received from the bracelet
             
//end channel
}
    

 

 

Link to comment
Share on other sites

2 minutes ago, Frionil Fang said:

llSensor does not call the sensor event: it happens at a later time once the current event (listen, in your case) is complete.The code hasn't had a chance to run when you try to debug the value. 

..because the llSensor() command is asynchronous..

Good catch! I missed they had llSensor() in the same listen() event as the debug statement. 
 

Link to comment
Share on other sites

Now that I'm no longer just trying to type single-handed during a phone call downtime: yeah, the llSensor is a request for "I'd like to receive a sensor/no_sensor event at a later date", it will never be fulfilled at the time the llSensor call finishes but only when the event queue can resume processing, i.e. after the listen event has finished.

How to fix it: you'd have to move any handling into the sensor/no sensor events completely, and the listen event only has the single llSensor call. Also not related to this issue, your loop in the sensor event starts at the wrong index. DetectedNNN functions index starting from 0, not 1, so you're skipping the first result (and probably the only result, considering what it's trying to do) every time.

Edited by Frionil Fang
  • Like 1
Link to comment
Share on other sites

To demonstrate what Frionil means:

 

//Doctor touches a bracelet that sends message to this button (this script).
//Script/button checks if message was sent by doctor.
//Then script/button checks if the doctor is within range (2.5 meters).

//--intial parameters here

//--functions here 

//--doctor touches the button (this object) and pairs to this button, assigning themself as the doctor.

//------------- (STATE PAIRED) --------------- 
state PAIRED  //start listening
{


    sensor (integer num_detected)  //***sensor will check if paired doctor is withing 2.5 meters of this script.
    { 
       string message = "Detected " + (string)num_detected + " avatar(s): " + llDetectedName(0);

        isDoctorNear=0;   // start new search
        
        integer index = 0;   // LSL is a zero-indexed language
        while (index < num_detected)
            DetectedName=llDetectedName(index++); CurrentName=llDetectedName(index-1);
            message += ", " + DetectedName;

            //check if searched avatar is found in results
                if (doctorName==CurrentName) 
                    {
                    isDoctorNear = 1; //isDoctorNear will be 1 if doctor is in range, or 0 if too far away
                    foundName=CurrentName;
                    }
    
           // llOwnerSay ("State Paired + sensor, isDoctorNear="+(string)isDoctorNear);  //debug
                                    
         
     // move your processing stuff here
 
        if (isDoctorNear==0) 
            {
            //state PAIRED;  //doctor not in range - go back to beginnine
            // since you are in state PAIRED already and you have not closed the listener you dont need to call state PAIRED; again. The listener is still listening for commands.
            // calling a state within the same state will do nothing.    
              
            
            } else{
            //do nothing and move foward to command processing below
            }


 
//-------------------
//--Process commands here based on command received from the bracelet                          
        
    }

    no_sensor()
    {
        isDoctorNear=0;  //doctor is not found within range
        llRegionSayTo(doctorKey, PUBLIC_CHANNEL, "*Out of Range* Please come closer to the VSM Exam Table.");    
           // llOwnerSay ("State Paired + NO sensor, isDoctorNear="+(string)isDoctorNear);  //debug            
        state PAIRED;  // start over
    }    
    
   
    
  state_entry()  //for paired state
  {
   
   // llOwnerSay("state PAIRED");
    //LISTENERS 
    llListen (ChInCmdLine220, "", "", "");  //COMMAND CHANNEL - listens to bracelet on CH -220 for commands
   

   }  //end state_entry
    
  listen (integer channel, string name, key id, string message)
  {

 if (ChInCmdLine220==channel)  //CH -220  //=================START CHANNEL -220/COMMAND
 {    

        //***Checks if paired doctor is within range of 2.5 meters  each time a command is heard from bracelet
        llSensor(doctorName, NULL_KEY, AGENT_BY_LEGACY_NAME, 2.5, PI);   //***<---CALLING LLSENSOR()  

       
             
 }//end channel
} // end listen
} //end state PAIRED

 

Edited by Gayngel
Link to comment
Share on other sites

Sorry for the double post. You can totally avoid a sensor (which can be laggy) for this anyway by doing this:

 

key doctorKey = "a9ba2797-81af-429d-9833-51127ad5593c"; // change this to the uuid of the doctor
integer ChInCmdLine220 = -220;
integer listen_handle;
integer isDoctorNear;

 //***Checks if paired doctor is within range of 2.5 meters  each time a command is heard from bracelet
integer checkDoctorRange(key id)
{
    vector obj_pos = llGetPos(); // get the position of the button (this script)
    
    list tmp = llGetObjectDetails(id, [OBJECT_POS]); // get the position of the doctor in a list
    
    
    vector doctor_pos = llList2Vector(tmp,0); // assign the position from the list to a variable
    
     // compare a distance between two vectors. 
    if(llVecDist(obj_pos,doctor_pos) <= 2.5) 
    return 1; //  // if the postion of the button and doctor is within range of 2.5 meters return 1
    
     
   else 
   return 0; //return 0 if not in range
   
  
}

default
{
    
   /// default stuff
   state_entry()
   {
       
      // do other stuff then change state
       state PAIRED;
       
    } 
    
    
}

//Doctor touches a bracelet that sends message to this button (this script).
//Script/button checks if message was sent by doctor.
//Then script/button checks if the doctor is within range (2.5 meters).

//--intial parameters here

//--functions here 

//--doctor touches the button (this object) and pairs to this button, assigning themself as the doctor.

//------------- (STATE PAIRED) --------------- 
state PAIRED  //start listening
{   
    
  state_entry()  //for paired state
  {
   
   // llOwnerSay("state PAIRED");
    //LISTENERS 
   listen_handle = llListen (ChInCmdLine220, "", "", "");  //COMMAND CHANNEL - listens to bracelet on CH -220 for commands

  

   // best to assign llListen to a handle so at any point you dont need the listener open can remove it with llListenRemove()
   

   }  //end state_entry
    
  listen (integer channel, string name, key id, string message)
  {
   
 if (ChInCmdLine220==channel)  //CH -220  //=================START CHANNEL -220/COMMAND
 {    
 
     if(llGetOwnerKey(id) == doctorKey) // if the owner of the object sending the message is the doctor 
     {
         
      isDoctorNear =  checkDoctorRange(doctorKey);  // or isDoctorNear =  checkDoctorRange(llGetOwnerKey(id));
      
       if (isDoctorNear==0) 
            {
                
               llOwnerSay("Doctor is too far away.");  
             //doctor not in range - go back to beginnine
               
             
            
            } 
       
       else{
                
               llOwnerSay("Doctor is nearby.");  
            
		//--Process commands here based on command received from the bracelet
              
              
            }  
         
     }    // end if(llGetOwnerKey(id) == doctorKey) 
             
 }//end channel
} // end listen
} //end state PAIRED

 

Edited by Gayngel
Link to comment
Share on other sites

Wow.  Definately some great options. It's so awesome to have such great scripters here!

And Gayngel, I really like the alternative of not having to use the llSensor altogether. Will definately be checking that option out also.  Thank you!!!

Thank you everybody!

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 310 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...