Raised This Month: $ Target: $400
 0% 

loop through all players


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hypheni
Senior Member
Join Date: Jul 2011
Location: India
Old 02-12-2013 , 03:23   loop through all players
Reply With Quote #1

Just to clarify my ideas I'm posting this.

PHP Code:
print_message(idmsg[]) 
{
    
message_begin(MSG_ONEgmsgSayText, {0,0,0}, id)
    
write_byte(id)
    
write_string(msg)
    
message_end()

#1
PHP Code:
for(new client=1client<=g_maxplayersclient++)
{
    if(
is_user_connected(client))
    {
        
print_message(clientbuffer)
    }

#2
PHP Code:
new players[32], playerCountplayer
get_players
(playersplayerCount)

for(new 
i=0i<playerCounti++)
{
    
player players[i]        
        
print_message(playerbuffer)

In the 1st code snippet it just enumerate through all current players and print a message, 2nd code does the same as I dont specify any flags in get_players(). If I put some flag like 'c' it will return only those numbers matches with that flag.

Am I correct ?

Last edited by hypheni; 02-12-2013 at 03:56.
hypheni is offline
Send a message via Skype™ to hypheni
P1raten
Senior Member
Join Date: Feb 2010
Old 02-12-2013 , 05:38   Re: loop through all players
Reply With Quote #2

http://www.amxmodx.org/funcwiki.php?go=func&id=1338
__________________
No salvation. Only madness.
P1raten is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 02-12-2013 , 05:51   Re: loop through all players
Reply With Quote #3

#2 does NOT do the same thing as #1, even with no flags specified. #1 is guaranteed to loop through every index up to MaxPlayers. Whereas #2 will only loop through all indexes that are occupied in the server.

Say you only have 5 users connected and 32 slots max.
#1 will loop through 32 times
#2 will loop through 5 times
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 02-12-2013 , 06:03   Re: loop through all players
Reply With Quote #4

#2 is worse way that I wont recomment.

get_players works like this:
- First it loops trought 32 results, check if they accept with the flags and put it in players[32]. After it did that, you gonna do a loop again, with the amount of results it added to players[32] with the needed flags.

So results in 2 loops, that results in more overlows.

So I recomment #1
__________________
Retired.
Xalus is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-12-2013 , 12:39   Re: loop through all players
Reply With Quote #5

Quote:
Originally Posted by Xalus View Post
#2 is worse way that I wont recomment.

get_players works like this:
- First it loops trought 32 results, check if they accept with the flags and put it in players[32]. After it did that, you gonna do a loop again, with the amount of results it added to players[32] with the needed flags.

So results in 2 loops, that results in more overlows.

So I recomment #1
This is incorrect. There may be two loops but the get_players() loop is done in a module which is much much faster than any looping in AMX Mod X calling is_user_connected() 32 times.
__________________
fysiks is offline
hypheni
Senior Member
Join Date: Jul 2011
Location: India
Old 02-12-2013 , 06:21   Re: loop through all players
Reply With Quote #6

Well nice explanation.

How AMX handles players ID. Is it like ID: 1 to 32 as per players join into the server. And when a player leave that id is assigned to other who joins the server. So the ID is actually one serial number assigned to each player.

Last edited by hypheni; 02-12-2013 at 06:22.
hypheni is offline
Send a message via Skype™ to hypheni
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 02-12-2013 , 06:28   Re: loop through all players
Reply With Quote #7

Index is assigned by the Server, not AMXX. The server does it in sequential order.

5 Players join and get assigned Indexes 1-5.
Player 2 drops.
A new player connects and gets assigned Index 2


Also, I still recommend using get_players over looping through every user.
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 02-12-2013 , 12:45   Re: loop through all players
Reply With Quote #8

Hmm maybe..
__________________
Retired.
Xalus is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 02-12-2013 , 13:18   Re: loop through all players
Reply With Quote #9

Not maybe, it is 100% sure, get_players make checks internally, when the second way has to check if each player is connected, so maxclient * is_user_connected natives calls for nothing.


Anyway, if it is just to print a message, you can send 1 message to everyone :

client_print(0, print_chat, "Your message with some patterns like %s", szName)


Or you can make your own if it is justified :

PHP Code:
print_message(id, const msg[]) 
{
    
message_begin(id MSG_ONE MSG_ALLgmsgSayText_id)
    
write_byte(id)
    
write_string(msg)
    
message_end()
}

print_message_format(id, const fmt[], any:...) 
{
    new 
msg[192]
    
vformat(msgcharsmax(msg), fmt3)
    
message_begin(id MSG_ONE MSG_ALLgmsgSayText_id)
    
write_byte(id)
    
write_string(msg)
    
message_end()


Anyway, do never use code #1, never.
And about code #2, in your example, 'player' variable is not required because you use it only once, so you can directly use players[i] instead. 'player' is usefull for optimization to prevent multiple use of players[i] (called re-indexing if you read wiki about optimizations).
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 02-12-2013 at 13:21.
ConnorMcLeod is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-13-2013 , 21:15   Re: loop through all players
Reply With Quote #10

Quote:
Originally Posted by ConnorMcLeod View Post
Or you can make your own if it is justified :
@hypheni, What are you doing that you "think" you need to do it this way?
__________________

Last edited by fysiks; 02-13-2013 at 21:15.
fysiks 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 11:11.


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