Optimizing MAX_PLAYERS?
#1

So I have heard a lot about MAX_PLAYERS being not the best option out there for player loops and such so I had this idea (not sure of anyone else suggested it before) to improve, but wanted to get more opinions before actually doing it.


So I was thinking about creating a whole new different variable, gMaxPlayers for example, and each time a player connects the variable gets incremented, and when a player disconnects the variable gets decremented.
Then I'd basically use the new variable when needing to do loops for the online/connected players.


Any thoughts if it would bug the server?
Reply
#2

It wouldn't work because if 4 players connect (IDs 0, 1, 2 and 3) and the first 2 (ID 0 and 1) leave the server, the variable will hold the number 2 but the current IDs of the players is 3 and 4 so the loop will never reach higher.

Yes, using MAX_PLAYERS for player-loops is not the best choice, especially if you haven't re-defined it.

So the best two I can suggest you is either GetPlayerPoolSize or foreach/y_iterate (the most recommended).
Reply
#3

Problem is with the ID's.
Let's imagine this:
gMaxPlayers = -1

You connect to the server
gMaxPlayers = 0

I connect to the server
gMaxPlayers = 1

Somebody else connects
gMaxPlayers = 2

Somebody else connects
gMaxPlayers = 3


Okay, everything seems fine for now.
But if I disconnect, players that are left on server are "0, 2 and 3", while your variable is gonna be at only 2, and when you loop it, it wouldn't reach last player.

You can just use an include called "foreach" which does what you want, but more optimized
Reply
#4

No point reinventing the wheel, use foreach so you only loop thru valid results, you can create your own iterators for example for house veh and business and have an optimized loop for those too.
Reply
#5

How about foreach? (i dont really know if there's anything different in it from for loop)
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
It wouldn't work because if 4 players connect (IDs 0, 1, 2 and 3) and the first 2 (ID 0 and 1) leave the server, the variable will hold the number 2 but the current IDs of the players is 3 and 4 so the loop will never reach higher.

Yes, using MAX_PLAYERS for player-loops is not the best choice, especially if you haven't re-defined it.

So the best two I can suggest you is either GetPlayerPoolSize or foreach/y_iterate (the most recommended).
Quote:
Originally Posted by BloodMaster
Посмотреть сообщение
Problem is with the ID's.
Let's imagine this:
gMaxPlayers = -1

You connect to the server
gMaxPlayers = 0

I connect to the server
gMaxPlayers = 1

Somebody else connects
gMaxPlayers = 2

Somebody else connects
gMaxPlayers = 3


Okay, everything seems fine for now.
But if I disconnect, players that are left on server are "0, 2 and 3", while your variable is gonna be at only 2, and when you loop it, it wouldn't reach last player.

You can just use an include called "foreach" which does what you want, but more optimized
This is exactly what I want.
The gMaxPlayers would be variable to store the number of the connected players not each player with his ID.
If 4 players connect, gMaxPlayers would be 4, then 2 players quit, gMaxPlayers would be 2. (It starts with 0 on OnGameModeInit as it's always 0 players at that particle moment and increments on OnPlayerConnect)

So basically I would be looping with a max number of the gMaxPlayers rather than the MAX_PLAYERS.

Hopefully I didn't miss-understand what you guys said, and again, it's a simple variable to store the number of the currently connected players.


Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
No point reinventing the wheel, use foreach so you only loop thru valid results, you can create your own iterators for example for house veh and business and have an optimized loop for those too.
Quote:
Originally Posted by iLearner
Посмотреть сообщение
How about foreach? (i dont really know if there's anything different in it from for loop)
I never really looked into the foreach structure, but I do think programming is a place where the sky's the limit. Why should I count on someone's else work? (Not taking anything from ******, he's in fact, more of an idol for me)
Reply
#7

And what's wrong with using foreach or GetPlayerPoolSize?
Reply
#8

Quote:
Originally Posted by oMa37
Посмотреть сообщение
And what's wrong with using foreach or GetPlayerPoolSize?
As stated, I got no clue about the foreach structure, however, on the "GetPlayerPoolSize" wiki - according to the description:


Quote:

Description:
Gets the highest playerid currently in use on the server.

Taking the previous example into consideration, if IDs (0,1,2,3) join the server, and then ID(0,1) leave, the GetPlayerPoolSize (if I'm not mistaken) would return a value of 3.
Meaning you'd run your loop an extra 2 times for nothing and my aim is to get the best possible optimization.
Reply
#9

Quote:
Originally Posted by MattTucker
Посмотреть сообщение
So basically I would be looping with a max number of the gMaxPlayers rather than the MAX_PLAYERS.
Your method won't work as the first slots can be empty due to players leaving, that's what both BloodMaster and I tried to explain by giving an example.

Player 3 and 4 are online. Your loop will go 0 .. 1 .. stop (2 players online only) and basically it will do nothing for those two connected players.

Quote:
Originally Posted by MattTucker
Посмотреть сообщение
I never really looked into the foreach structure, but I do think programming is a place where the sky's the limit. Why should I count on someone's else? (Not taking anything from ******, he's infact, more of an idol for me)
That's a bad way of thinking. There are several includes and plugins that make the way we code easier.

Quote:
Originally Posted by MattTucker
Посмотреть сообщение
[..] and my aim is to get the best possible optimization.
Then go for foreach as everyone has suggested. It is the best method for player-loops.
Reply
#10

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Your method won't work as the first slots can be empty due to players leaving, that's what both BloodMaster and I tried to explain by giving an example.

Player 3 and 4 are online. Your loop will go 0 .. 1 .. stop (2 players online only) and basically it will do nothing for those two connected players.



That's a bad way of thinking. There are several includes and plugins that make the way we code easier.



Then go for foreach as everyone has suggested. It is the best method for player-loops.
Tried scripting it and I actually get what you guys said.
Thanks for the help, and sorry for the thick head lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)