AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Loop Players HUD Message (https://forums.alliedmods.net/showthread.php?t=147179)

ichiban 01-06-2011 06:00

Loop Players HUD Message
 
Hi,

I am unsure of how to loop players in my HUD message

PHP Code:

/*
    Example Output:
    Players:
    Name
    Name
    Name
    Name
*/

public show_players(id)
{
    new 
szPlayers[200]
    new 
maxplayers get_maxplayers()
    
szPlayers[200] = "Players:^n"
    
    
for (new 1<= maxplayersi++)
    {
        new 
szName[32]
        
get_user_name(iszName31)
        
        
//szPlayers += ("%s^n", szName)
    
}
    
    
set_hudmessage(25500, -1.00.01)
    
show_hudmessage(idszPlayers)



SnoW 01-06-2011 06:28

Re: Loop Players HUD Message
 
You do realize that get_maxplayers returns the settings value and not the amount of connected players currently at the server, search for proper method. Strings don't have operators, so you could use some kind of native for example format to build the string. You are setting the string's value totally wrong, read some pawn tutorial for arrays and their indexes and please don't create variables inside loops. Why would you create such function as public?

Elusive138 01-06-2011 07:42

Re: Loop Players HUD Message
 
Quote:

Originally Posted by SnoW (Post 1387263)
You do realize that get_maxplayers returns the settings value and not the amount of connected players currently at the server, search for proper method. Strings don't have operators, so you could use some kind of native for example format to build the string. You are setting the string's value totally wrong, read some pawn tutorial for arrays and their indexes and please don't create variables inside loops. Why would you create such function as public?

To clarify:

Use get_players instead.
To set/modify strings (in this case) use format and/or formatex.
Put the new szName[32] outside (above) the loop.

See http://www.amxmodx.org/funcwiki.php for more information on these, and please read http://wiki.alliedmods.net/Optimizin...d_X_Scripting).

SnoW 01-06-2011 08:11

Re: Loop Players HUD Message
 
Quote:

Originally Posted by Elusive138 (Post 1387294)
To clarify:

Use get_players instead.
To set/modify strings (in this case) use format and/or formatex.
Put the new szName[32] outside (above) the loop.

See http://www.amxmodx.org/funcwiki.php for more information on these, and please read http://wiki.alliedmods.net/Optimizin...od_X_Scripting).

Clarifing with ( in this case ) you probably meant after the first time setting the value as after the initialization.

Your link doesn't work.
http://wiki.alliedmods.net/Optimizin...d_X_Scripting)

vato loco [GE-S] 01-06-2011 08:21

Re: Loop Players HUD Message
 
take look on my Show Admins Online
plugin it does what you try to do
you only need to remove the admin stuff


ichiban 01-06-2011 08:34

Re: Loop Players HUD Message
 
Thanks everyone.

Here is my final code that works. If there are any optimizations, please correct.

PHP Code:

public show_players(id)
{
    new 
szName[32]
    new 
szPlayers[201], iLen
    iLen 
+= copy(szPlayers[iLen], (201 iLen), "Players^n")
    
    for(new 
1<= get_maxplayers(); i++)
    {
        if (
is_user_connected(i))
        {
            
get_user_name(iszName31)            
            
iLen += formatex(szPlayers[iLen], (201 iLen), "%s^n"szName)
        }
    }
    
    
set_hudmessage(25500, -1.00.01)
    
show_hudmessage(idszPlayers)



vato loco [GE-S] 01-06-2011 08:44

Re: Loop Players HUD Message
 
PHP Code:

new g_iMaxPlayers
new g_SyncMsgPlayer

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
g_SyncMsgPlayer CreateHudSyncObj()
    
g_iMaxPlayers get_maxplayers()
}

public 
show_players(id)
{
    new 
szName[32], szPlayers[512], iLeni
    iLen 
+= formatex(szPlayers[iLen], 511-iLen"Players:")
    
    for(
1<= g_iMaxPlayers i++)
    {
        if (
is_user_connected(i))
        {
            
get_user_name(iszName31)            
            
iLen += formatex(szPlayers[iLen], 511-iLen"^n%s"szName)
        }
    }
    
set_hudmessage(255000.020.2__4.0__4)
    
ShowSyncHudMsg(idg_SyncMsgPlayer"%s"szPlayers)



ichiban 01-06-2011 11:01

Re: Loop Players HUD Message
 
What's the advantage of using g_iMaxPlayers instead of using get_maxplayers() in the loop?

Elusive138 01-06-2011 11:10

Re: Loop Players HUD Message
 
Quote:

Originally Posted by ichiban (Post 1387400)
What's the advantage of using g_iMaxPlayers instead of using get_maxplayers() in the loop?

Read the optimisations link I gave you... Here's the specific section, but I recommend you read the whole thing.

http://wiki.alliedmods.net/Optimizin...op_Comparisons


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

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