Difference between revisions of "Follower script"

From Second Life Wiki
Jump to: navigation, search
Line 65: Line 65:
 
}
 
}
 
</lsl>
 
</lsl>
 +
 +
This following is a bit less laggy, as it does not use sensor
 +
 +
<lsl>
 +
 +
 +
integer count;
 +
key followee;
 +
vector agent;
 +
 +
 +
 +
position()
 +
{
 +
  vector pos = llList2Vector(llGetObjectDetails(followee, [OBJECT_POS]),0);
 +
                 
 +
                 
 +
                  llSetPos(pos);
 +
                 
 +
                  count += 1;
 +
                 
 +
                    if (count > 50)
 +
                    {
 +
                        llResetScript();
 +
                      // llSay(0,"resetting");
 +
                    }
 +
    position2();
 +
    }
 +
   
 +
   
 +
   
 +
position2()
 +
{
 +
  vector pos = llList2Vector(llGetObjectDetails(followee, [OBJECT_POS]),0);
 +
                 
 +
             
 +
                 
 +
                   
 +
                  if (pos == <0,0,0> | pos.z > 1000 | pos.z < 800 )
 +
                  {
 +
                     
 +
                   
 +
                     
 +
                   
 +
                   
 +
                      llRegionSay(-8765433,(string)followee);
 +
                      llSleep(1);
 +
                      llDie();
 +
                    }
 +
                 
 +
               
 +
               
 +
                  llSetPos(pos);
 +
                  count += 1;
 +
               
 +
    position();// yes this script loops between position(); and position2();
 +
   
 +
    }
 +
   
 +
   
 +
default
 +
{
 +
  state_entry()
 +
  {
 +
      llSleep(3);
 +
      llMessageLinked(LINK_THIS,700,"who",""); //asks another script in contents who is wanted to be followed (maybe the other script has a listener, getting the to-be-followed agent from a central server or something)
 +
    // llSay(0,"reseted");
 +
 
 +
  }
 +
 +
  link_message(integer sender_num, integer num, string message, key id)
 +
    {
 +
        if (num == 500)//the other script replies, giving us an agentkey to follow
 +
        {
 +
            // llfolloweeSay("I follow: " + (string) message);
 +
            followee = (key)message;
 +
          // llfolloweeSay("I will follow: " + (string) followee);
 +
          // llSleep(1);
 +
            if (followee != "")
 +
            {
 +
                llMessageLinked(LINK_THIS,600,"got it","");//we tell the other script we got the name and we are ready
 +
           
 +
         
 +
          position();
 +
             
 +
            }
 +
          return;
 +
        }
 +
    }
 +
    on_rez(integer start_param)
 +
    {
 +
        llResetScript();
 +
    }
 +
 +
 +
}
 +
</lsl>
 +
  
 
{{LSLC|Library|Follower}}  
 
{{LSLC|Library|Follower}}  
 
{{LSLC|Examples|Follower}}
 
{{LSLC|Examples|Follower}}

Revision as of 00:23, 24 June 2012

This script is based on an extract from the Batman Follower v1.2. It is very basic. If you put it in an object, that object will keep moving toward a position offset from it's owner.

<lsl>

vector offset = < -1, 0, 1>; //1 meter behind and 1 meter above owner's center.

default {

   state_entry()
   {
       llSetStatus(STATUS_PHYSICS, TRUE);
       // Little pause to allow server to make potentially large linked object physical.
       llSleep(0.1);
       // Look for owner within 20 metres in 360 degree arc every 1 seconds.
       llSensorRepeat("", llGetOwner(), AGENT, 20.0, PI,1.0);
   }
   sensor(integer total_number)
   {   // Owner detected...
       // Get position and rotation
       vector pos   = llDetectedPos(0);
       rotation rot = llDetectedRot(0);
       // Offset back one metre in X and up one metre in Z based on world coordinates.
       // use whatever offset you want.
       vector worldOffset = offset;
       // Offset relative to owner needs a quaternion.
       vector avOffset = offset * rot;
       pos += avOffset;       // use the one you want, world or relative to AV.
       llMoveToTarget(pos,0.4);     
   }

} </lsl> <lsl> //adding this script as a less laggy and more efficient way of doing the same as above //this is for havok4's new functions vector offset = < -1, 0, 1>; //1 meter behind and 1 meter above owner's center.

default {

   state_entry()
   {
       llSetStatus(STATUS_PHYSICS, TRUE);
       // Little pause to allow server to make potentially large linked object physical.
       llSleep(0.1);
       llSetTimerEvent(1.0);
   }
   timer()
   {
       list det = llGetObjectDetails(llGetOwner(),[OBJECT_POS,OBJECT_ROT]);//this will never fail less owner is not in the same sim
       // Owner detected...
       // Get position and rotation
       vector pos   = llList2Vector(det,0);
       rotation rot = (rotation)llList2String(det,1);
       // Offset back one metre in X and up one metre in Z based on world coordinates.
       // use whatever offset you want.
       vector worldOffset = offset;
       // Offset relative to owner needs a quaternion.
       vector avOffset = offset * rot;
       pos += avOffset;       // use the one you want, world or relative to AV.
       llMoveToTarget(pos,0.4);
   }

} </lsl>

This following is a bit less laggy, as it does not use sensor

<lsl>


integer count;
key followee;

vector agent;


position()
{
  vector pos = llList2Vector(llGetObjectDetails(followee, [OBJECT_POS]),0);
                  
                  
                 llSetPos(pos); 
                 
                  count += 1;
                  
                   if (count > 50)
                   {
                       llResetScript();
                      // llSay(0,"resetting");
                   }
   position2();
   }
   
   
   
position2()
{
  vector pos = llList2Vector(llGetObjectDetails(followee, [OBJECT_POS]),0);
                 
              
                 
                   
                  if (pos == <0,0,0> | pos.z > 1000 | pos.z < 800 )
                 {
                     
                    
                     
                   
                   
                     llRegionSay(-8765433,(string)followee);
                     llSleep(1);
                     llDie();
                   }
                 
                
               
                 llSetPos(pos); 
                  count += 1;
                
   position();// yes this script loops between position(); and position2(); 
   
   }
   
   

default {

 state_entry()
 {
     llSleep(3);
     llMessageLinked(LINK_THIS,700,"who",""); //asks another script in contents who is wanted to be followed (maybe the other script has a listener, getting the to-be-followed agent from a central server or something)
    // llSay(0,"reseted");
  
 }

 link_message(integer sender_num, integer num, string message, key id)
   {
       if (num == 500)//the other script replies, giving us an agentkey to follow
       {
           // llfolloweeSay("I follow: " + (string) message);
           followee = (key)message;
          // llfolloweeSay("I will follow: " + (string) followee);
          // llSleep(1);
           if (followee != "")
           {
               llMessageLinked(LINK_THIS,600,"got it","");//we tell the other script we got the name and we are ready
            
         
          position();
              
           }
          return;
       }
   }
   on_rez(integer start_param)
   {
       llResetScript();
   }


} </lsl>