Difference between revisions of "LlGetAgentList"

From Second Life Wiki
Jump to navigation Jump to search
m (added "no agents found" output to second example script, rolled back to ++index instead of index++ style to improve speed)
(More fixing of bugs introduced by the same compulsive editor. Undid distance->string conversion as this corrupts the sort. Removed check for no avi's - who touched the prim? llOwnerSay() was intended.)
Line 40: Line 40:
         if (!numOfAvatars)
         if (!numOfAvatars)
         {
         {
             // PUBLIC_CHANNEL has the integer value 0
             llOwnerSay("No avatars found within the region!");
            llSay(PUBLIC_CHANNEL, "No avatars found within the region!");
             return;
             return;
         }
         }
Line 67: Line 66:
         list keys = llGetAgentList(AGENT_LIST_REGION, []);
         list keys = llGetAgentList(AGENT_LIST_REGION, []);
         integer numberOfKeys = llGetListLength(keys);
         integer numberOfKeys = llGetListLength(keys);
        if (!numberOfKeys) {
            llSay(PUBLIC_CHANNEL, "No agents found.");
            return;
        }


         vector currentPos = llGetPos();
         vector currentPos = llGetPos();
Line 78: Line 72:


         integer i;
         integer i;
         for (; i < numberOfKeys; ++i) {
         for (i = 0; i < numberOfKeys; ++i) {
             thisAvKey = llList2Key(keys,i);
             thisAvKey = llList2Key(keys,i);
             newkeys += [(string)llRound(llVecDist(currentPos,
             newkeys += [llRound(llVecDist(currentPos,
                             llList2Vector(llGetObjectDetails(thisAvKey, [OBJECT_POS]), 0))),
                             llList2Vector(llGetObjectDetails(thisAvKey, [OBJECT_POS]), 0))),
                         thisAvKey];
                         thisAvKey];
         }
         }
 
 
    //  sort strided list by descending distance
         newkeys = llListSort(newkeys, 2, FALSE);     //  sort strided list by descending distance
         newkeys = llListSort(newkeys, 2, FALSE);


         for (i = 0; i < (numberOfKeys * 2); i += 2) {
         for (i = 0; i < (numberOfKeys * 2); i += 2) {
             llSay(PUBLIC_CHANNEL, llGetDisplayName(llList2Key(newkeys, i+1))
             llOwnerSay(llGetDisplayName(llList2Key(newkeys, i+1))
                 +" ["+llList2String(newkeys, i) + "m]");
                 +" ["+ (string) llList2Integer(newkeys, i) + "m]");
         }
         }
     }
     }

Revision as of 09:53, 9 December 2012

Summary

Function: list llGetAgentList( integer scope, list options );

Requests a list of agents currently in the region, limited by the scope parameter.
Returns a list [key id0, key id1, ..., key idn] or [string error_msg] - returns avatar keys for all agents in the region limited to the area(s) specified by scope

• integer scope
AGENT_LIST_* flag specifies the selection scope
  • AGENT_LIST_PARCEL - returns only agents on the same parcel where the script is running.
  • AGENT_LIST_PARCEL_OWNER - returns only agents on any parcel in the region where the parcel owner is the same as the owner of the parcel under the scripted object.
  • AGENT_LIST_REGION - returns any/all agents in the region.
• list options Unused.

Caveats

  • There is no guaranteed understandable order or randomness to the list returned.
  • Will only return up to 100 agents.
All Issues ~ Search JIRA for related Bugs

Examples

KBcaution.png Important: To check if an avatar is in the same sim, please check whether llGetAgentSize does NOT return ZERO_VECTOR. It's much faster and easier than calling llGetAgentList and running through the list to compare a given key with each list item.
KBcaution.png Important: As of now, this function does not have a delimiter for its option list. This means, the function MIGHT return up to a hundred avatar keys. You'll need a lot of free memory to be able to store those keys, be warned of possible STACK_HEAP_COLLISION_ERRORs.

<lsl> //Displays up to 100 avatar key: name pairs detected in the entire region

default {

   touch_start(integer total_number)
   {
       list avatarsInRegion = llGetAgentList(AGENT_LIST_REGION, []);
       integer numOfAvatars = llGetListLength(avatarsInRegion);
       // if no avatars, abort avatar listing process and give a short notice
       if (!numOfAvatars)
       {
           llOwnerSay("No avatars found within the region!");
           return;
       }
       integer index;
       while (index < numOfAvatars)
       {
           key id = llList2Key(avatarsInRegion, index);
           string name = llKey2Name(id);
           llOwnerSay(name + " [ " + (string)id + " ]");
           ++index;
       }
   }

} </lsl> <lsl> // Orders new list based on distance // and returns names and distances on touch

default {

   touch_start(integer num_detected)
   {
       list keys = llGetAgentList(AGENT_LIST_REGION, []);
       integer numberOfKeys = llGetListLength(keys);
       vector currentPos = llGetPos();
       list newkeys;
       key thisAvKey;
       integer i;
       for (i = 0; i < numberOfKeys; ++i) {
           thisAvKey = llList2Key(keys,i);
           newkeys += [llRound(llVecDist(currentPos,
                           llList2Vector(llGetObjectDetails(thisAvKey, [OBJECT_POS]), 0))),
                       thisAvKey];
       }
 
       newkeys = llListSort(newkeys, 2, FALSE);     //  sort strided list by descending distance
       for (i = 0; i < (numberOfKeys * 2); i += 2) {
           llOwnerSay(llGetDisplayName(llList2Key(newkeys, i+1))
               +" ["+ (string) llList2Integer(newkeys, i) + "m]");
       }
   }

}

</lsl>

See Also

Deep Notes

History

All Issues

~ Search JIRA for related Issues
   llGetAgentList() with scope AGENT_LIST_PARCEL or AGENT_LIST_PARCEL_OWNER returns empty list when attached to avatar
   llGetNumberOfAgents() function to return an integer count of agents
   AGENT_LIST_MAX_RESULTS option for llGetAgentList()

Signature

function list llGetAgentList( integer scope, list options );