Your opinion needed -
Roko_foko - 30.07.2012
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.
Re: Your opinion needed -
SEnergy - 30.07.2012
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?
Re: Your opinion needed -
Roko_foko - 30.07.2012
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.
Re: Your opinion needed -
SEnergy - 30.07.2012
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?
Re: Your opinion needed -
Roko_foko - 30.07.2012
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?
Re: Your opinion needed -
SEnergy - 30.07.2012
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
Re: Your opinion needed -
[MM]RoXoR[FS] - 30.07.2012
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.
Re: Your opinion needed -
Roko_foko - 30.07.2012
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.