AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   loop through all players (https://forums.alliedmods.net/showthread.php?t=208331)

hypheni 02-12-2013 03:23

loop through all players
 
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 ?

P1raten 02-12-2013 05:38

Re: loop through all players
 
http://www.amxmodx.org/funcwiki.php?go=func&id=1338

YamiKaitou 02-12-2013 05:51

Re: loop through all players
 
#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

Xalus 02-12-2013 06:03

Re: loop through all players
 
#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

hypheni 02-12-2013 06:21

Re: loop through all players
 
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.

YamiKaitou 02-12-2013 06:28

Re: loop through all players
 
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.

fysiks 02-12-2013 12:39

Re: loop through all players
 
Quote:

Originally Posted by Xalus (Post 1892912)
#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.

Xalus 02-12-2013 12:45

Re: loop through all players
 
Hmm maybe..

ConnorMcLeod 02-12-2013 13:18

Re: loop through all players
 
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).

hypheni 02-13-2013 12:29

Re: loop through all players
 
Quote:

print_message(id, const msg[])
{
message_begin(id ? MSG_ONE : MSG_ALL, gmsgSayText, _, id)
write_byte(id)
write_string(msg)
message_end()
}
So for sending message to all players I need to pass 0 as 1st param and actual message as 2nd.

Actually I don't understand this write_byte() function. It says write bytes to message. In C char is 1 byte. So how these bytes are calculated here for particular message which need to be sent to client.


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

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