Raised This Month: $ Target: $400
 0% 

get_players or get_maxplayers()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TheKidz
Senior Member
Join Date: May 2009
Old 10-21-2010 , 13:00   get_players or get_maxplayers()
Reply With Quote #1

wich what is better?
PHP Code:
new g_MaxPlayers;
public 
plugin_init() {
 
g_MaxPlayers get_maxplayers()
}

public 
func() {
 for( new 
1<= get_maxplayersi++ ) {
  
//do stuff
 
}

or:
PHP Code:
public func() {
 new 
Clients32 ], iNum
 get_players
ClientsiNum )
 for( new 
0iNumi++ ) {
  
//do stuff
 
}

maybe i wrote something wrong on there, but its just an example :z

obersavation: i always used the second one :s
__________________

Last edited by TheKidz; 11-06-2010 at 14:18.
TheKidz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-21-2010 , 13:11   Re: get_players or get_maxplayers()
Reply With Quote #2

Depends on what you are trying to do.
__________________
fysiks is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 10-21-2010 , 13:21   Re: get_players or get_maxplayers()
Reply With Quote #3

For me: get_maxplayers()
Boccoz u dont need to call it again and again !!
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Vechta
Veteran Member
Join Date: Jun 2010
Old 10-21-2010 , 13:22   Re: get_players or get_maxplayers()
Reply With Quote #4

Quote:
Originally Posted by abdul-rehman View Post
For me: get_maxplayers()
Boccoz u dont need to call it again and again !!
+1
Vechta is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-21-2010 , 13:27   Re: get_players or get_maxplayers()
Reply With Quote #5

Quote:
Originally Posted by abdul-rehman View Post
For me: get_maxplayers()
Boccoz u dont need to call it again and again !!
It really depends on what you are trying to do. Just because get_maxplayers() is only called once doesn't mean that it is the one that you should use.
__________________
fysiks is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 10-21-2010 , 13:43   Re: get_players or get_maxplayers()
Reply With Quote #6

I know how to use it properly thats why i said For me
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-21-2010 , 16:29   Re: get_players or get_maxplayers()
Reply With Quote #7

Quote:
Originally Posted by TheKidz View Post
PHP Code:
new g_MaxPlayers;
public 
plugin_init() {
 
g_MaxPlayers get_maxplayers()
}

public 
func() {
 for( new 
0get_maxplayersi++ ) {
  
//do stuff
 
}

Your loop is wrong.

Code:
new g_MaxPlayers;
public plugin_init() {
 g_MaxPlayers = get_maxplayers()
}

public func() {
 for( new i = 1; i <= g_MaxPlayers; i++ ) {
  //do stuff
 }
}
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 10-21-2010 , 16:38   Re: get_players or get_maxplayers()
Reply With Quote #8

Remember that if you are using get_maxplayers() you have to check if is_user_connected().
With get_players() you can filter the players by alive, dead, ct, tt, etc.
If you want to loop through all players in server, I recommend you use get_maxplayers() + is_user_connected()
Mxnn is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-21-2010 , 18:12   Re: get_players or get_maxplayers()
Reply With Quote #9

get_maxplayers() method uses more native calls and has more loop iterations.

Code:
get_maxplayers() x 1
is_user_connected x 32 (or whatever get_maxplayers() returns) 
Loop get_maxplayers() # times
get_players() uses only 1 native call, there's no need to check if the slot is connected, and has less loop iterations (usually).

Code:
get_players() x 1
Loop only # of times of currently connected players.
get_players() is sometimes frowned upon because using particular flags can sometimes return false results. If you are only trying to obtain connected players then get_players is totally fine and better to use. If you are checking a combination of flags (online terrorists only, dead bots, etc) it is safer to use the get_maxplayers() method.
__________________

Last edited by Bugsy; 10-21-2010 at 18:17.
Bugsy is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 10-21-2010 , 20:59   Re: get_players or get_maxplayers()
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
get_maxplayers() method uses more native calls and has more loop iterations.
... unless you cache if the player is connected

Code:
new g_iMaxPlayers
new bool:g_bOnline[33]

public plugin_init()
   g_iMaxPlayers = get_maxplayers()

public client_putinserver(id)
   g_bOnline[id] = true

public client_disconnect(id)
   g_bOnline[id] = false

public func()
{
   for(new id = 1; id <= g_iMaxPlayers; id++)
   {
      if(g_bOnline[id])
      {
          // ...
      }
   }
}
Or you could just use a g_iTeam if you cache the teams or use bits for less memory.

Still, this would do more stuff, but in a big plugin where you actually need those variables or forwards, this method shouldn't be less efficient.
__________________

Last edited by Hunter-Digital; 10-21-2010 at 21:02.
Hunter-Digital is offline
Reply



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 10:21.


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