Skip to content

Request script, select random player.

Is there a way to select a random player in the server, or in an area?

Comments

  • squattingmonksquattingmonk Member Posts: 59
    edited April 2018
    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.
  • nwninterestednwninterested Member Posts: 6
    Oh nice, TY sir!!
Sign In or Register to comment.