03.06.2011, 18:09
(
Последний раз редактировалось Ash.; 04.06.2011 в 08:41.
)
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());
}
Код:
TruckingWorld Maximum Players: 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!" |
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] ----------------------------
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
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()
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.