Checking how much players are currently online -
Moron - 04.12.2012
How to check the amount of players online? I want to make my command work when it's higher than an x number of players.
Re: Checking how much players are currently online -
cosbraa - 04.12.2012
pawn Код:
stock GetOnlinePlayers()
{
new amount;
foreach(Player, i)
{
//if(IsPlayerConnected(i))
amount++;
}
return amount;
}
This wasn't tested, though I assume it would work how you want.
I can't remember if foreach checks if the ploayer is connected manually itself. If not, just uncomment that line.
Example of use;
pawn Код:
if(GetOnlinePlayers() < 20) return SendClientMessage(playerid, -1, "There are not enough players online to do this!");
Re: Checking how much players are currently online -
Moron - 04.12.2012
error 017: undefined symbol "foreach"
Re: Checking how much players are currently online -
cosbraa - 04.12.2012
You need foreach include.
Download it here:
http://pastebin.com/Seseuh2x
(click download just above the code, and rename the file to "foreach.inc", place it in your pawno > include folder then #include <foreach> at teh top of your script)
Or alternatively, use this;
pawn Код:
stock GetOnlinePlayers()
{
new amount;
for(new i; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
amount++;
}
return amount;
}
Re: Checking how much players are currently online -
Moron - 04.12.2012
I tried downloading foreach, but got many loose indentation warnings.
The alternative works, thanks.
Re: Checking how much players are currently online -
Sinner - 04.12.2012
Quote:
Originally Posted by Moron
I tried downloading foreach, but got many loose indentation warnings.
The alternative works, thanks.
|
Download foreach from the post on this forum, ofcourse pastebin will give loose indentations.
Re: Checking how much players are currently online -
cosbraa - 04.12.2012
I took the link of the pastebin from foreach thread. Oh well.
Didn't know about Iter_Count, nice.
Re: Checking how much players are currently online -
Bicentric - 04.12.2012
Come on guys, don't assume he's using foreach, use plain PAWN code, granted using foreach is better but not all new scripters know about libraries yet.
pawn Код:
stock GetOnlinePlayers()
{
new count;
for(new i; i != GetMaxPlayers(); ++i)
{
if(!IsPlayerConnected(i)) continue;
count++;
}
return count;
}
Re: Checking how much players are currently online -
Bicentric - 04.12.2012
Quote:
Originally Posted by ******
Surely that's a good reason for telling them?
|
Yes, it's a perfectly good reason to tell them, that doesn't answer his original question. But, I suppose I shall explain to him what it is.
Basically, a library is another script what different people have created to add more functionality to your gamemode / filterscript. This is not just something to do with PAWN, most programming languages have libraries. But I am specifically talking about SA:MP here. Some of them are 'Plug & Play' where all you have to do is include it into your gamemode and it will work straight away, this is achieved by using methods such as ALS. And others add a set of new functions and callbacks, take a look at
YSI, it's basically a whole set of libraries which adds many more useful features. People tend to call them includes, but they're basically the same thing. A whole bunch of them can be found here:
http://forum.sa-mp.com/forumdisplay.php?f=83
Probably didn't explain that as well as I could of, but it was rushed so meh. And there was really no point in me writing this if you already knew :P, but I guess other people can use it as an explanation.
Re: Checking how much players are currently online -
iggy1 - 04.12.2012
Maybe a better way than looping.
pawn Код:
new gConnectedPlayers = 0;
public OnPlayerConnect(playerid)
{
++gConnectedPlayers;
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
--gConnectedPlayers;
return 1;
}