Your opinion needed
#1

Hi guys.
As title says I need your opinion.
Is this good way of looping all players?
Is there any way it can be improved?
Do you see a situation when this will not work?
Thank you.

pawn Код:
new AllPlayers[MAX_PLAYERS+1]=-1;//added after 1st reply, +1 so when there is 500 players, it still has end(-1)

#define foreach(%0) for(%0=0;AllPlayers[%0]!=-1;%0++)

public OnPlayerConnect(playerid)
{
    if(IsPlayerNPC(playerid)==0)
    {
        for(i=0;AllPlayers[i]!=-1;i++){}//finding first cell that does not have any player id.
        AllPlayers[i]=playerid;// save the playerid
        AllPlayers[i+1]=-1;// make sure there is one "free" cell for next player, and -1 is also EOS(only to this array)
    }
    //rest of the code
}
public OnPlayerDisconnect(playerid, reason)
{
    if(!IsPlayerNPC(playerid))
    {
        for(i=0;AllPlayers[i]!=playerid;i++){}//finding the cell where is stored playerid that has just been disconnected
        while(AllPlayers[i]!=-1)AllPlayers[i]=AllPlayers[i+1];//cleaning one( playerid -disconnected) cell and every cell after it moving one place to left.
    }
}
//usage:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/printfallids",cmdtext)==0)
    {
        new i;
        foreach(i)printf("%d",AllPlayers[i]);
        return 1;
    }
}
Don't know if this is in right section. If not, I apologize and I would like an administrator to move the thread to right section.

Thank you for your attention.
EDIT: Please reply guys. You don't need to read first page as I see it as useless.
Reply
#2

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
Hi guys.
As title says I need your opinion.
Is this good way of looping all players?
Is there any way it can be improved?
Do you see a situation when this will not work?
Thank you.

pawn Код:
#define foreach(%0) for(%0=0;AllPlayers[%0]!=-1;%0++)

public OnPlayerConnect(playerid)
{
    if(IsPlayerNPC(playerid)==0)
    {
        for(i=0;AllPlayers[i]!=-1;i++){}
        AllPlayers[i]=playerid;
        AllPlayers[i+1]=-1;
    }
    //rest of the code
}
public OnPlayerDisconnect(playerid, reason)
{
    if(!IsPlayerNPC(playerid))
    {
        for(i=0;AllPlayers[i]!=playerid;i++){}
        while(AllPlayers[i]!=-1)AllPlayers[i]=AllPlayers[i+1];
    }
}
//usage:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/printfallids",cmdtext)==0)
    {
        new i;
        foreach(i)printf("%d",AllPlayers[i]);
        return 1;
    }
}
Don't know if this is in right section. If not, I apologize and I would like an administrator to move the thread to right section.

Thank you for your attention.
pawn Код:
for(i=0;AllPlayers[i]!=-1;i++){}
        AllPlayers[i]=playerid;
        AllPlayers[i+1]=-1;
what is this supposed to do?
Reply
#3

Quote:
Originally Posted by SEnergy
Посмотреть сообщение
pawn Код:
for(i=0;AllPlayers[i]!=-1;i++){}
        AllPlayers[i]=playerid;
        AllPlayers[i+1]=-1;
what is this supposed to do?
That code loops AllPlayers array until the AllPlayers[i]!=-1. In other words, it finds the first place in the array, where there is not stored any playerid.(playerid are from 0 to 499).
After finding it, it stores the playerid in the "free" array space, and make sure the next cell is free.
After seeing your question I realized I missed one part of the code. Edited first post.
Reply
#4

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
That code loops AllPlayers array until the AllPlayers[i]!=-1. In other words, it finds the first place in the array, where there is not stored any playerid.(playerid are from 0 to 499).
After finding it, it stores the playerid in the "free" array space, and make sure the next cell is free.
After seeing your question I realized I missed one part of the code. Edited first post.
I have one question for you, do you know how functions/loops work?
Reply
#5

Quote:
Originally Posted by SEnergy
Посмотреть сообщение
I have one question for you, do you know how functions/loops work?
I guess, I don't? How do they work?
Reply
#6

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
I guess, I don't?
so this code

pawn Код:
for(i=0;AllPlayers[i]!=-1;i++){}

will do ABSOLUTELY nothing, this bracket "{" means "head" of function, and this one "}" closes function, so everything between them will happen in that function, I suggest you learn some basic programming first, and then go to script some game modes, also I have NO IDEA what are you trying to do, could you tell me what do you want to do with this whole script, not only onplayerconnect callback
Reply
#7

Try this
pawn Код:
new AllPlayers[MAX_PLAYERS];//global
//Under OnGameModeInIt
{
   for(new i=0;i<MAX_PLAYERS;++i) AllPlayers[i] = -1;
}
//This was because if I did AllPlayers[MAX_PLAYERS] = -1 in definaion, it may have put value of -1 to just AllPlayers[0]..
//Happened to me once.

//When Player COnnects
public OnPlayerConnect(playerid)
{
 for(new i=0;i<MAX_PLAYERS;++i)
    if(AllPlayers[i] == -1) { AllPlayers[i] = playerid; break;}
//Or maybe AllPlayers[playerid] = 1;
}

//When he disconnects
{
  for(new i=0;i<MAX_PLAYERS;++i)
    if(AllPlayers[i] == playerid){AllPlayers[i] = -1; break;}
//Or maybe AllPlayers[playerid] = -1;
}
So, that's it.You may use any of the way.
Reply
#8

Quote:
Originally Posted by [MM]RoXoR[FS]
Посмотреть сообщение
Try this
pawn Код:
new AllPlayers[MAX_PLAYERS];//global
//Under OnGameModeInIt
{
   for(new i=0;i<MAX_PLAYERS;++i) AllPlayers[i] = -1;
}
//This was because if I did AllPlayers[MAX_PLAYERS] = -1 in definaion, it may have put value of -1 to just AllPlayers[0]..
//Happened to me once.

//When Player COnnects
public OnPlayerConnect(playerid)
{
 for(new i=0;i<MAX_PLAYERS;++i)
    if(AllPlayers[i] == -1) { AllPlayers[i] = playerid; break;}
//Or maybe AllPlayers[playerid] = 1;
}

//When he disconnects
{
  for(new i=0;i<MAX_PLAYERS;++i)
    if(AllPlayers[i] == playerid){AllPlayers[i] = -1; break;}
//Or maybe AllPlayers[playerid] = -1;
}
So, that's it.You may use any of the way.
Thank you very much for responding.
If you look at the part of when "he disconnects", you can see a problem there.
Lets say there AllPlayers[] look like this {1,2,3,4,5,-1,...} if 2 disconnects array will look like this {1,-1,3,4,5,-1,..} but it should look like this {1,3,4,5,-1,-1,...}
And about:" //This was because if I did AllPlayers[MAX_PLAYERS] = -1 in definaion, it may have put value of -1 to just AllPlayers[0].."
this is why I have put
pawn Код:
public OnPlayerConnect(playerid)
{
    if(IsPlayerNPC(playerid)==0)
    {
        for(i=0;AllPlayers[i]!=-1;i++){}//finding first cell that does not have any player id.
        AllPlayers[i]=playerid;// save the playerid
        AllPlayers[i+1]=-1;// make sure there is one "free" cell for next player, and -1 is also EOS(only to this array)
    }
    //rest of the code
}
// make sure there is one "free" cell for next player, and -1 is also EOS(only to this array)
Actually this code works fine. I am asking if there is a way how can player be disconnected from server and that he avoids OnPlayerDisconnect callback.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)