Raised This Month: $119 Target: $400
 29% 

Client Looping


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 05-21-2018 , 06:03   Client Looping
Reply With Quote #1

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.
__________________

Last edited by Neuro Toxin; 05-21-2018 at 06:10.
Neuro Toxin is offline
Reiko1231
Member
Join Date: Apr 2013
Location: Russia
Old 05-21-2018 , 06:28   Re: Client Looping
Reply With Quote #2

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).

Last edited by Reiko1231; 05-21-2018 at 06:29.
Reiko1231 is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 05-21-2018 , 06:37   Re: Client Looping
Reply With Quote #3

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.
__________________

Last edited by Neuro Toxin; 05-21-2018 at 06:49.
Neuro Toxin is offline
Reiko1231
Member
Join Date: Apr 2013
Location: Russia
Old 05-21-2018 , 07:37   Re: Client Looping
Reply With Quote #4

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").
Reiko1231 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 05-21-2018 , 10:20   Re: Client Looping
Reply With Quote #5

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
__________________
Mitchell is offline
psychonic

BAFFLED
Join Date: May 2008
Old 05-21-2018 , 10:54   Re: Client Looping
Reply With Quote #6

Quote:
Originally Posted by Mitchell View Post
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.
psychonic is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:59.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode