Jump to content
  • 0

Q>Select random player in map


Question

3 answers to this question

Recommended Posts

Posted

I was trying to do this years ago.

However it's not optimized and query_sql will really simplify and speed up everything, that's a sample of what I planned years ago (also, when my scripting abilities were not great.)

First, I was saving all the players IDs into an array each time they log in, and deleting them each time they log out.

OnPCLoginEvent:
set .PeopleOnline[getarraysize(.PeopleOnline)], getcharid(0);
//...

OnPCLogoutEvent:
set .@tmp$, strcharinfo(0);
for( .@i = 0; .@i < getarraysize(.PeopleOnline$); .@i++ )
   if( .@tmp$ == PeopleOnline$[.@i] ) {
    deletearray PeopleOnline$[.@i], 1;
    break; //Useless to keep the for loop
   }

Second, I looped all the online players to check whoever was in that map.

then, I save players into a temporary array

//Save my AID
set .@myAid, getcharid(3);

for( for( .@i = 0; .@i < getarraysize(.PeopleOnline$); .@i++ ) {
   attachrid(getcharid(3,.PeopleOnline$[.@i])); //Actually it shouldn't be necessary an online check for obvious reason.
   if( strcharinfo(3) == "Your_map" )
    set .@array$[getarraysize(.@array)], .PeopleOnline$[.@i];
}

//Attaching back my RID
attachrid(.@myAid); //There, maybe a check should be added.

So, you now have .@array$ array that has stored the names of who is in "Your_map" map.

So you can choose one randomly...

set .@randomPlayer$, .@array$[rand(getarraysize(.@array$))];
dispbottom "You randomly selected " + .@randomPlayer$;

  • Upvote 1
Posted

are you doing this for event ?

( I can't think of other situation that need to do something like this )

if it is, then save them into an array when they register

then its very easy to retrieve them later on

example like this script

  • Upvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...