AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   What is better way of printing this? (https://forums.alliedmods.net/showthread.php?t=316629)

6Yukimuro9 06-02-2019 04:49

What is better way of printing this?
 
Hello, I'm creating VIP plugin from scratch and its like 90% done by now so I' m doing optimisation...

I'm wondering what is better way of doing this:

Code:

public print_online_vips(id)
{
        new vips[100], vips2[100], iPlayers[32], iPlayersNum, i;
       
        get_players(iPlayers, iPlayersNum, "c");
        for(i = 0; i < iPlayersNum; ++i)
        {
                if(get_user_flags(iPlayers[i]) & VIP_LEVEL)
                {
                        new name[MAX_NAME_LENGTH];
                        get_user_name(iPlayers[i], name, sizeof(name));
                       
                       
                        if(strlen(vips) < 100)
                                format(vips, sizeof(vips), "%s%s%s", vips, name, !vips[0] ? "" : ", ");
                        else
                                format(vips2, sizeof(vips2), "%s, %s", vips2, name);
                }
        }
       
        if(!vips[0])
                client_print_color(id, id, "^4[VIP]^1 %L", id, "NO_VIP_ONLINE");
               
        else
                client_print_color(id, id, "^4VIP Online:^3 %s^4%s", vips, vips2[0] ? "," : ".");
               
        if(vips2[0])
                client_print_color(id, id, "^3 %s^4.", vips2);
               
        return PLUGIN_HANDLED;
}


SHIELD755 06-02-2019 06:40

Re: What is better way of printing this?
 
Use cromchat :)

redivcram 06-02-2019 06:42

Re: What is better way of printing this?
 
Quote:

Originally Posted by SHIELD755 (Post 2654064)
Use cromchat :)

Beat me to it...

6Yukimuro9 06-02-2019 08:29

Re: What is better way of printing this?
 
Quote:

Originally Posted by SHIELD755 (Post 2654064)
Use cromchat :)

Why would I do that?

This doesn't have any bugs and its better & faster :)

OciXCrom 06-02-2019 08:54

Re: What is better way of printing this?
 
Your code is limited to only 2 arrays of vips. With 32 VIPs online, it won't print all of them.

PHP Code:

public print_online_vips(id)
{
    static const 
iMax 4
    
new szVips[160], iPlayers[MAX_PLAYERS], iPnumiCountAll
    get_players
(iPlayersiPnum"ch")

    
szVips "^4VIPs Online: ^3"

    
for(new iiCountiPlayeriPnumi++)
    {
        
iPlayer iPlayers[i]

        if(
get_user_flags(iPlayer) & VIP_LEVEL)
        {
            
iCountAll++

            if(
iCount++ < iMax)
            {
                
add(szVipscharsmax(szVips), fmt("%n"iPlayer))

                if(
iPnum 1)
                {
                    
add(szVipscharsmax(szVips), "^4, ^3")
                }
            }
            else
            {
                
iCount 0
                client_print_color
(ididszVips)
                
szVips[0] = EOS
            
}
        }
    }

    if(
iCountAll)
    {
        
client_print_color(ididszVips)
    }
    else
    {
        
client_print_color(idid"^4%l""NO_VIP_ONLINE")
    }

    return 
PLUGIN_HANDLED


iMax is the maximum number of players that will get printed on each line.

HamletEagle 06-02-2019 09:45

Re: What is better way of printing this?
 
Quote:

Originally Posted by OciXCrom (Post 2654077)
Your code is limited to only 2 arrays of vips. With 32 VIPs online, it won't print all of them.

PHP Code:

public print_online_vips(id)
{
    static const 
iMax 4
    
new szVips[160], iPlayers[MAX_PLAYERS], iPnumiCountiCountAlli
    get_players
(iPlayersiPnum"ch")

    @
new_string:
    
szVips "^4VIPs Online: ^3"

    
for(new iPlayeriPnumi++)
    {
        
iPlayer iPlayers[i]

        if(
get_user_flags(iPlayer) & VIP_LEVEL)
        {
            
iCountAll++

            if(
iCount++ < iMax)
            {
                
add(szVipscharsmax(szVips), fmt("%n"iPlayer))

                if(
iPnum 1)
                {
                    
add(szVipscharsmax(szVips), "^4, ^3")
                }
            }
            else
            {
                
iCount 0
                client_print_color
(ididszVips)
                goto @
new_string
            
}
        }
    }

    if(
iCountAll)
    {
        
client_print_color(ididszVips)
    }
    else
    {
        
client_print_color(idid"^4%l""NO_VIP_ONLINE")
    }

    return 
PLUGIN_HANDLED


iMax is the maximum number of players that will get printed on each line.

goto and jumping backwards :cry:

OciXCrom 06-02-2019 11:17

Re: What is better way of printing this?
 
I guess we can skip that part. I edited the code.


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

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