Here's a function that does just that: // Gets a random PC from oArea. If oArea is invalid, gets a random online PC.
object GetRandomPC(object oArea = OBJECT_INVALID)
{
object oTarget;
if (GetIsObjectValid(oArea))
oTarget = oArea;
else
oTarget = GetModule();
int i;
object oPC = GetFirstPC();
while (GetIsObjectValid(oPC))
{
if (oTarget != oArea || GetArea(oPC) == oArea)
SetLocalObject(oTarget, "PC_LIST" + IntToString(i++), oPC);
oPC = GetNextPC();
}
return GetLocalObject(oTarget, "PC_LIST" + IntToString(Random(i)));
}
// Usage
void main()
{
// Get a random PC from the server
object oPC1 = GetRandomPC();
SpeakString("Module PC found: " + GetName(oPC1));
// Get a random PC in the caller's area
object oPC2 = GetRandomPC(GetArea(OBJECT_SELF));
SpeakString("Area PC found: " + GetName(oPC2));
} It's a little inefficient because it rebuilds the PC list every time it's called, but it may serve as a useful example.
Comments
// Gets a random PC from oArea. If oArea is invalid, gets a random online PC. object GetRandomPC(object oArea = OBJECT_INVALID) { object oTarget; if (GetIsObjectValid(oArea)) oTarget = oArea; else oTarget = GetModule(); int i; object oPC = GetFirstPC(); while (GetIsObjectValid(oPC)) { if (oTarget != oArea || GetArea(oPC) == oArea) SetLocalObject(oTarget, "PC_LIST" + IntToString(i++), oPC); oPC = GetNextPC(); } return GetLocalObject(oTarget, "PC_LIST" + IntToString(Random(i))); } // Usage void main() { // Get a random PC from the server object oPC1 = GetRandomPC(); SpeakString("Module PC found: " + GetName(oPC1)); // Get a random PC in the caller's area object oPC2 = GetRandomPC(GetArea(OBJECT_SELF)); SpeakString("Area PC found: " + GetName(oPC2)); }
It's a little inefficient because it rebuilds the PC list every time it's called, but it may serve as a useful example.