[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
#2

NPC's (Bots)

Just as a side thought; another thought - GetMaxPlayers() returns the maximum player slots available. When you connect/insert an NPC, this limit is actually reduced. I'm not too sure on this but as GetMaxPlayers() returns the maximum player slots, it will not return the missing slots due to bots. This could be good as with bots the limit will be dynamically changing; this means you do not have to edit your script dependant on the amount of bots you have!
Reply
#3

YESS! Just what I have been looking for i knew there was another way, but I couldnt remember, you reminded me

Nice one ash! Keep up the good work!
Reply
#4

Using GetMaxPlayers() is slower than using a pre-defined macro. If you undefine MAX_PLAYERS and set it to GetMaxPlayers() it's still going to call the function each time - so it really doesn't make a difference.

Also, you're never going to beat the speed of a macro with a function - so really, this is not the "better way of doing it" it is in fact the worst way you can "do it" and it should be avoided.

If you really want to make sure that loops only call the max amount of players for your server efficiently and with the MAX_PLAYERS macro you can undefine it and define it again with the amount of player slots that you server has been allocated.

Do your research prior to creating a tutorial - this has already been explained hundreds of times on the forums. While you're at it, you should also read the pawn-lang.pdf guide and the other various PDF guides for Pawn so you can gain a better understanding of how macros, variables and functions work.

Furthermore, your benchmark is useless - you don't compare it to anything at all. The point in using benchmarks is usually to compare two or more items together to prove that your solution is the fastest (even though it is not at all).
Reply
#5

Okay; I knew this would happen. I based my tutorial on opinion - I never actually said it was faster either. Could I ask that you re-read my post?

500 (drfault MAX_PLAYERS) is not going to be faster than calling a function; I know and it's obvious...

I never said it would be faster, as the pre-processor ensures that MAX_PLAYERS is actually faster (as it just replaces it with the defined value) however GetMaxPlayers() can't be replaced as it needs to be called while the server is running. While using GetMaxPlayers, you are ensuring that any changes to the player limit will mean the script is automatically ready for the new limit, rather than going back an editing it

All I can say is please re-read; your points are invalid as quite frankly; I never mentioned any of the points you posted as fact. The whole thing is mainly opinion based and so you may find defining MAX_PLAYERS as an integer is better.

the benchmark for MAX_PLAYERS would be zero. As the PAWN pre-processor removes the need to process/call such information so the same loop as in my example would look like:
pawn Код:
for(new i; i < 500; i++)
- thought that'd be obvious to be honest...

Oh, and for your reference, I know how macro's, functions and variables work thanks...
Reply
#6

Good tuto, thanks. (Y)
Reply
#7

Quote:
Originally Posted by funky1234
i'm just saying that there are better ways of doing it.
It's not better, it's the worst way of doing it.

Player slots don't change and I'm positive that you can't change them during runtime - the only thing that will change it is adding NPCs, even then some code you'll want to run by NPCs and some IDs may be high and might clash with NPC IDs.

My statements are clearly valid - you should read them.
Reply
#8

Great tutorial,although I personally wouldn't use GetMaxPlayers as a definition of MAX_PLAYERS..
Reply
#9

Quote:
Originally Posted by __
Посмотреть сообщение
It's not better, it's the worst way of doing it.

Player slots don't change and I'm positive that you can't change them during runtime - the only thing that will change it is adding NPCs, even then some code you'll want to run by NPCs and some IDs may be high and might clash with NPC IDs.

My statements are clearly valid - you should read them.
Okay, i'm going to get this in the open: You can make a script run as fast as possible; but have fun changing it all because you've defined loads of integers based on MAX_PLAYERS that need changing due to upgrade, or NPC's...
I never said they would change during runtime either... I clearly stated that an upgrade or NPC's could change the maximum player limit. This; as I said, would make GetMaxPlayers() better (No, not for speed) because it constantly reads the actual player limit. So, let say you upgraded your server, from a 10 slot to a 15 slot - but you used an integer definition of MAX_PLAYERS; guess what, you have to back and change it all otherwise the new 15 slots wouldn't get called in player loops etc.

They are valid, granted. But not for the purposes you were giving them for; I never mentioned anything in that topic, I never said that you would beat the speed; in fact I did state it was slower...

I don't mean to be rude, but I genuinely think you mis-understood a great chunk of my original post.

Quote:
Originally Posted by Montserrat
Посмотреть сообщение
Great tutorial,although I personally wouldn't use GetMaxPlayers as a definition of MAX_PLAYERS..
Thank You. For my own information; Why wouldn't you use it in such a way?
Reply
#10

Quote:
Originally Posted by funky1234
Посмотреть сообщение
Okay, i'm going to get this in the open: You can make a script run as fast as possible; but have fun changing it all because you've defined loads of integers based on MAX_PLAYERS that need changing due to upgrade, or NPC's...
I never said they would change during runtime either... I clearly stated that an upgrade or NPC's could change the maximum player limit. This; as I said, would make GetMaxPlayers() better (No, not for speed) because it constantly reads the actual player limit. So, let say you upgraded your server, from a 10 slot to a 15 slot - but you used an integer definition of MAX_PLAYERS; guess what, you have to back and change it all otherwise the new 15 slots wouldn't get called in player loops etc.

They are valid, granted. But not for the purposes you were giving them for; I never mentioned anything in that topic, I never said that you would beat the speed; in fact I did state it was slower...

I don't mean to be rude, but I genuinely think you mis-understood a great chunk of my original post.



Thank You. For my own information; Why wouldn't you use it in such a way?
This one is faster for me:
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 200
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)