[Tutorial] GetMaxPlayers()
#1

GetMaxPlayers()


This is both very short, and in my opinion very helpful, tutorial. I have seen many many people use MAX_PLAYERS without redefining it as there maximum player slots, or use something ludicrous in place of such a simple function. I'm not saying using MAX_PLAYERS is a bad thing, i'm just saying that there are better ways of doing it.

Function (GetMaxPlayers())
As the function name suggests, it returns the amount of player slots available. Proof? Okay...

pawn Код:
#include <a_samp>
public OnFilterScriptInit()
{
     printf("TruckingWorld Maximum Players: %i", GetMaxPlayers());
}
Printed this in my server log:
Код:
TruckingWorld Maximum Players: 75
Now, that seems pretty obvious eh. (For those of you that haven't seen TruckingWorld nor looked at its statistics, the player limit is 75.

Okay... Why are you telling me this?
Because i've just read a post telling you how to redefine MAX_PLAYERS to your maximum player count. There is one major problem with this that pops to mind straight away. UPGRADE. What happens when you upgrade your server to have more player slots, or just change player slots in general? That's right, you guessed it, go back into the script just to change the #define line. I know, most of you are going to go:
Quote:

"OMG yh but thats no effort!"

Well, when there is a better way of doing it, why bother?

Use GetMaxPlayers()! It has EXACTLY the same affect, the only difference is, GetMaxPlayers() takes time (next to none, however) to process.

Time Test (Or 'Benchmarking')

This test, just ran on TruckingWorld, states that it takes 76 milliseconds to complete 100000 "GetMaxPlayers" calls. I originally had it being called 1000 times, the total time returned as 0.

Код:
[19:01:50] ----------------------------
[19:01:50] Test Started at: 21454870
[19:01:50] Test Complete at: 21454946
[19:01:50] Test total: 76
[19:01:50] ----------------------------
The script I ran is available here:
http://pastebin.com/8hY0JfrT

I was told that my benchmarking was useless. Well; it must of not been obvious that the pre-processor** removes the need to process the MAX_PLAYERS definition. So after compile (although it obviously wouldn't look like this after a compile)
pawn Код:
//GetMaxPlayers() - COMPILED
for(new i; i < GetMaxPlayers(); i++)

//MAX_PLAYERS - COMPILED
for(new i; i < 500; i++) //Obviously the 500 would be whatever MAX_PLAYERS is defined as before compile time
You would find that if I did the same test as in my PasteBin link, I would probably get some sort of tag-mismatch or unreachable code error, because a number on it's own isn't really a function or something that can be called for a result. If it did however, there is no processing that needs to take place, and so the time tests would return 0 (as in 0 milliseconds, always.)

But... I like typing MAX_PLAYERS

Well, you still can. Instead of redefining it as a number, why not redefine it as "GetMaxPlayers()" ?

Like so:

pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS GetMaxPlayers()
It works, I use it on my scripts.

Conclusion
I'm not saying it's bad that you redefine MAX_PLAYERS, i'm just giving you better way if you know that your maximum players are likely to change (regurlarly?) + My limit doesn't, unless I personally change it. (I host my server on a dedicated linux box) - But I still like to do that anyway, it's just my way of doing it. I don't redefine either I just do GetMaxPlayers().

Sorry this is short, but needs must. (Although it's not a need)

Thanks,
Ash.

**If you want to read more on the pre-processor; ****** has posted quite a bit on it, it's well worth a read! - Here: https://sampforum.blast.hk/showthread.php?tid=166680

Also: I've just remembered that there is a topic on the differences between MAX_PLAYERS and GetMaxPlayers() - I can't find it although it is in this board somewhere. I can't remember what detail it went into either so what I have posted about may just be the same. If it is, a moderator/BETA tester, may remove this thread.
Reply


Messages In This Thread
GetMaxPlayers() - by Ash. - 03.06.2011, 18:09
Re: GetMaxPlayers() - by Ash. - 03.06.2011, 23:28
Re: GetMaxPlayers() - by Jack_Rocker - 03.06.2011, 23:35
Re: GetMaxPlayers() - by __ - 03.06.2011, 23:45
Re: GetMaxPlayers() - by Ash. - 04.06.2011, 00:34
Respuesta: GetMaxPlayers() - by [DOG]irinel1996 - 04.06.2011, 01:24
Re: GetMaxPlayers() - by __ - 04.06.2011, 02:21
Re: GetMaxPlayers() - by nuriel8833 - 04.06.2011, 06:09
Re: GetMaxPlayers() - by Ash. - 04.06.2011, 08:32
Re: GetMaxPlayers() - by nuriel8833 - 04.06.2011, 11:59

Forum Jump:


Users browsing this thread: 1 Guest(s)