Raised This Month: $ Target: $400
 0% 

Proper client iteration


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Gecko
SourceMod Donor
Join Date: Nov 2010
Location: Vancouver, Canada
Old 09-17-2013 , 21:45   Proper client iteration
Reply With Quote #1

Should iterating over all clients in the server go like this

Code:
for (new i = 1; i <= MAXPLAYERS; i++)
{
}
Or like this?

Code:
for (new i = 0; i < MAXPLAYERS; i++)
{
}
__________________
"My brain is so left-sided, it isn't even funny... at all."

Last edited by Gecko; 09-17-2013 at 21:45.
Gecko is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 09-17-2013 , 21:46   Re: Proper client iteration
Reply With Quote #2

Client indexes start at 1 so it'd be <= MAXPLAYERS or MaxClients. Listen servers start at 0, but it's not a case you should take into consideration since clients shouldn't be hosting on listen servers.

*Edit: Rather, you should use MAXPLAYERS + 1 for declarations inside the global scope, MAXPLAYERS within OnPluginStart, and MaxClients for anything afterwards.
__________________

Last edited by thetwistedpanda; 09-17-2013 at 21:48.
thetwistedpanda is offline
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 09-17-2013 , 21:48   Re: Proper client iteration
Reply With Quote #3

Depending on your situation you may consider GetMaxHumanPlayers() instead of MaxClients.
hlstriker is offline
psychonic

BAFFLED
Join Date: May 2008
Old 09-17-2013 , 22:27   Re: Proper client iteration
Reply With Quote #4

Quote:
Originally Posted by hlstriker View Post
Depending on your situation you may consider GetMaxHumanPlayers() instead of MaxClients.
Not necessarily. Just because that there is a separate maximum amount of humans that can join doesn't mean that there can't be bot indexes between them, up to a total of MaxClients.
psychonic is offline
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 09-17-2013 , 22:30   Re: Proper client iteration
Reply With Quote #5

Quote:
Originally Posted by psychonic View Post
Not necessarily. Just because that there is a separate maximum amount of humans that can join doesn't mean that there can't be bot indexes between them, up to a total of MaxClients.
So that function doesn't include bots (as the name suggests humans but I was under the impression bots were a part of this but SourceTV was not)?

Last edited by hlstriker; 09-17-2013 at 22:32.
hlstriker is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-17-2013 , 22:32   Re: Proper client iteration
Reply With Quote #6

Quote:
Originally Posted by hlstriker View Post
So that function doesn't include bots?
No, which is why it's called GetMaxHumanPlayers.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Gecko
SourceMod Donor
Join Date: Nov 2010
Location: Vancouver, Canada
Old 09-17-2013 , 22:09   Re: Proper client iteration
Reply With Quote #7

Okay, the only reason I ask is because I'm using the first iteration method that I posted, but for some reason I'm getting errors of invalid client index #, where the # is equal to my max players + 1.
__________________
"My brain is so left-sided, it isn't even funny... at all."
Gecko is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 09-17-2013 , 22:10   Re: Proper client iteration
Reply With Quote #8

Can ya show us the code that's giving you issues? But you shouldn't be using MAXPLAYERS + 1 in a loop, not with <=. Use < MAXPLAYERS + 1 or <= MAXPLAYERS, assuming that's related. But without seeing code, can't say much more.
__________________

Last edited by thetwistedpanda; 09-17-2013 at 22:26.
thetwistedpanda is offline
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 09-17-2013 , 22:40   Re: Proper client iteration
Reply With Quote #9

Here is the bug report that made me think this:
https://bugs.alliedmods.net/show_bug.cgi?id=5757

So if GetMaxHumanPlayers only returns the number of humans, then the only option for CS:GO is to use MaxClients which always returns 64. Is there no function to get the number of humans + bots so we don't need to iterate over 64 indexes every time when most of the time only 1/2 - 1/3 of those indexes will actually have players.

Even after adding bots to the server GetMaxHumanPlayers is still showing 16 (which is what I have maxplayers set to).

Last edited by hlstriker; 09-17-2013 at 22:45.
hlstriker is offline
psychonic

BAFFLED
Join Date: May 2008
Old 09-18-2013 , 08:11   Re: Proper client iteration
Reply With Quote #10

Quote:
Originally Posted by hlstriker View Post
Here is the bug report that made me think this:
https://bugs.alliedmods.net/show_bug.cgi?id=5757

So if GetMaxHumanPlayers only returns the number of humans, then the only option for CS:GO is to use MaxClients which always returns 64. Is there no function to get the number of humans + bots so we don't need to iterate over 64 indexes every time when most of the time only 1/2 - 1/3 of those indexes will actually have players.

Even after adding bots to the server GetMaxHumanPlayers is still showing 16 (which is what I have maxplayers set to).
It's on a game-by-game basis since that function can be implemented differently in different games.

In this specific case, you can use it for the count of players that can be in the server, including bots, since CS:GO doesn't add bots beyond that count. It just uses them to replace humans when desired, up to that count.

Last edited by psychonic; 09-18-2013 at 17:38.
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 14:27.


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