AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Client Looping (https://forums.alliedmods.net/showthread.php?t=307688)

Neuro Toxin 05-21-2018 06:03

Client Looping
 
Hi Team,

Just looking for some solid input from devs around client index looping.

I loop clients like (depending on needs):

Code:

for (int client=1; client <= MaxClients; client++)
or

Code:

for (int client=1; client < MAXPLAYERS; client++)
I see people adding +1 to MaxClients and MAXPLAYERS which seems like a waist of CPU cycles as I would assume MaxClients and MAXPLAYERS already do +1 for optimisation.

Reiko1231 05-21-2018 06:28

Re: Client Looping
 
MaxClients is not equal to MAXPLAYERS. MAXPLAYERS is always 64 and MaxClients is equal to server slots count.
When using MAXPLAYERS it is necessary to add +1 because numeration starts from zero, and last available slot is 64, so that is MAXPLAYERS+1 (i.e. while i < 65, so last i will be 64).
So basically you should use MAXPLAYERS+1 to declare global variables and MaxClients in local variables and for loop.
For optimization you should use <= as it is same instructions as < (require same amount of time to execute).

Neuro Toxin 05-21-2018 06:37

Re: Client Looping
 
Doesn't MAXPLAYERS == 65?

https://github.com/alliedmodders/sou...lients.inc#L72
https://github.com/alliedmodders/sou...rHelpers.h#L52

Edit: My question is mainly around MaxClients. I understand the zero indexing for creating arrays using MAXPLAYERS.

Reiko1231 05-21-2018 07:37

Re: Client Looping
 
My mistake, MAXPLAYERS is 65. But because SourceTV is legal fake client, you still have to add +1 to MAXPLAYERS, otherwise last client will be outside of loop.
I don't understand question about MaxClients then. Yes, it is waste of CPU cycles if you check for (< MaxClients+1) since for cycle check this expression every iteration. It is better to check for (<= MaxClients). But MaxClients is equal to max players count (for my local server it is 24, if I try to IsClientInGame(25), I'll recieve error "Client index 25 is invalid").

Mitchell 05-21-2018 10:20

Re: Client Looping
 
I don't think you ever need to loop all the way to MAXPLAYERS, even when setting variables back to a 'default' etc. The server can't go above MaxClients until it's shut off changes slot count and restarted which by then the plugin would be reloaded etc.
You should size for MAXPLAYERS+1
and you should plan with MaxClients

psychonic 05-21-2018 10:54

Re: Client Looping
 
Quote:

Originally Posted by Mitchell (Post 2593101)
I don't think you ever need to loop all the way to MAXPLAYERS, even when setting variables back to a 'default' etc. The server can't go above MaxClients until it's shut off

That's not true. It can change at map change if initiated by SourceTV or Replay bot being added, or by a plugin.


All times are GMT -4. The time now is 06:00.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.