How to make a loop through connected players -
badnum23 - 09.01.2013
...and without this check IsPlayerConnected(playerid). I want to tell you how to make loops a little bit faster.
The base
First of all. Create a two variables and one define.
Code:
new pList[MAX_PLAYERS], pListSize;
#define pLoop(%0) for (new %0 = 0; %0 < pListSize; %0++)
And then add two this functions:
Code:
pListPush(playerid)
{
pList[pListSize] = playerid;
pListSize++;
}
pListPop(playerid)
{
new i,j;
for (i = 0; i < pListSize; i++)
if (playerid == pList[i]) {
j = i;
break;
}
for (i = j; i < pListSize-1; i++)
pList[i] = pList[i+1];
pListSize--;
}
Ok. Next step is to add some code to OnPlayerConnect and OnPlayerDisconnect publics.
Code:
public OnPlayerConnect(playerid)
{
pListPush(playerid);
// your code
return 1;
}
public OnPlayerDisconnect(playerid)
{
// your code
pListPop(playerid);
return 1;
}
As you can see pListPush added in the start of the public and pListPop in the end.
How to use?
Simply. For example you have some loop, something like this:
Code:
for (new i = 0; i < MAX_PLAYERS; i++)
{
if (IsPlayerConnected(i))
{
....
}
}
You should make it looks like this:
Code:
new i;
pLoop(j)
{
i = pList[j];
// some of your old code...
}
One more example
Code:
for (new i = 0; i < MAX_PLAYERS; i++)
if (IsPlayerConnected(i))
RemovePlayerFromVehicle(i);
and...
Code:
pLoop(i)
RemovePlayerFromVehicle(pList[i]);
I hope you will use it in your gamemode. Just imagine how faster would work GodFather based gamemodes, if you replaced all the loops, lol.
Re: How to make a loop through connected players -
FUNExtreme - 09.01.2013
Or simply: foreach
Re: How to make a loop through connected players -
badnum23 - 09.01.2013
But if you don't use Y_Less includes you can't use foreach.
Re: How to make a loop through connected players -
FUNExtreme - 09.01.2013
EDIT: My brains are having a little herp derp because of all the studying
Re: How to make a loop through connected players -
badnum23 - 09.01.2013
2 1 3 4 0
delete 1
2 (1) 3 4 0
2 3 3 4 0
2 3 4 4 0
2 3 4 0 0
and pListSize--; so final array is
2 3 4 0
So you should not use -1 instead of +1
FUNExtreme, it's normal. Exams, exams everywhere and every fucking winter
Re: How to make a loop through connected players -
mastermax7777 - 09.01.2013
badnum23 sexy beast.. No, just use for(new i=0;i<500;i++){ What's the problem?};
Re: How to make a loop through connected players -
ReneG - 10.01.2013
Look in the foreach source, it's the same concept you've displayed in this tutorial. You also don't need YSI for foreach to work.
Re: How to make a loop through connected players -
[HK]Ryder[AN] - 10.01.2013
this is a snippet, not a tutorial. you didn't explain the code. it is a copy-paste script
Re: How to make a loop through connected players -
badnum23 - 10.01.2013
[HK]Ryder[AN], I can explain to you anything, but I think you've already know it all. I don't have good spelling skills, so I can't explain the work of all the system good.
VincentDunn, ok.
mastermax7777, maybe I should use for (new i = 0; i < 50000000; i++) {What's the problem?}. And it doesn't matter there is only 3 players on the server