Looping Through Players
#1

Hello guys i want to create a dialog that shows connected players (well it is not connected players but this is an example), the thing i want to make is loop through players and write the connected players into a List dialog every player on it's own list

the looping is easy i know but how do you put each player in a specific listitem ?

Thanks in advance.
Reply
#2

for + IsPlayerConnected + strcat. Try to do something on your own, and come here when you'll get stuck on some problem. Also - isn't tab doing that already?

(You might run out of stack space due to really long string, look up malloc)
Reply
#3

I use this for my old server to admins list.. Just some changes
pawn Код:
#define DIALOG_ONLINE_LIST 1001

CMD:list(playerid, params[])
{
    new string[520], pname[MAX_PLAYER_NAME], fstring[26];
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            GetPlayerName(i, pname, sizeof(pname));
            format(fstring, sizeof(fstring), "%s\n", pname);
            strcat(string, fstring, sizeof(string));
        }
    }
    ShowPlayerDialog(playerid, DIALOG_ONLINE_LIST, DIALOG_STYLE_LIST, "Online players:", string, "OK", "");
    return 1;
}
Reply
#4

lamarr - MAX_PLAYER_NAME is 24 (you should do +1 due to \0). If you have more than 22 players on server, you just got youself overflow
Reply
#5

fstring size is 26..
Reply
#6

Oh, didn't see the fstring. Still - string is only 511+1 chars long.
Reply
#7

Hey.. I donґt know, how much is max players on his server. Acknowledge, 512 chars variable is too small
Reply
#8

it should be like this.

pawn Код:
CMD:something(playerid)
{
   static a = 0, b,name[24],str[MAX_PLAYERS * MAX_PLAYER_NAME],lol[50],c = 0;
   b = GetMaxPlayers();
   for(; a < b; ++a)
   {
       if(!IsPlayerConnected(a))continue;
       ++c;
       GetPlayerName(a,name, sizeof name);
       format(str, sizeof(str), "%s\n", name);
       strcat(str, str, sizeof str);
   }
   format(lol,sizeof lol,"Amount of connected players is: %d",c);
   return ShowPlayerDialog(playerid, 0, DIALOG_STYLE_LIST,lol ,str, "Option 1", "Option 2");
}
Reply
#9

pawn Код:
strcat(str, str, sizeof str);
Oh dude. Also if you try to get string longer than 4k chars usually you run out of stack. Pagination required, or malloc
Reply
#10

Thanks guys for all of your responses and the idea is to make a list for frozen players, i just want a basic concept of how it is done.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)