AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Get max array of X flags in the loop (https://forums.alliedmods.net/showthread.php?t=336108)

Supremache 01-28-2022 04:55

Get max array of X flags in the loop
 
Hi all,
I have question i trying to make a code for check vip members in the chat but i had a small issue when compared vips number with max array number as example:

PHP Code:

@OnVipsOnlineid )
{
    new 
szBufferMAX_TEXT_LENGTH ], szPlayersMAX_PLAYERS ], iPlayeriNum;
    
formatexszBuffercharsmaxszBuffer ), "&x04Online:&x01 " );
    
    
get_playersszPlayersiNum "ch" ); // iNum = it is max player numbers in the server if i used it any where in the loop it also return all players in the server

    
for( new iIndexiiNumi++  )
    {
        if( 
g_iPlayer[ ( iIndex szPlayers] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettingsAccess_OnlineList ] )
        { 
// i tried to compare iIndex with iNum and it work like adding symbol (,)  behinde all player names and another symbol (.) to the last player but work only when all players has that vip flag in the server so if anyone does not have that flag will add symbol (,) till if it was the last player
// I tried these ways but didn't work
            
formatszBuffercharsmaxszBuffer ), "%s%s%s"szBufferg_iPlayeriIndex ][ Name ], iNum < ( iPlayer iIndex /* return the vip numbers */ ) ? ", " "." );
            
formatszBuffercharsmaxszBuffer ), "%s%s%s"szBufferg_iPlayeriIndex ][ Name ], ( ( iPlayer iIndex ) < iNum ? ( iIndex <= iNum iIndex ) : ( iIndex <= iNum || iNum iIndex ) ) ? ", " "." );
            
formatszBuffercharsmaxszBuffer ), "%s%s%s"szBufferg_iPlayeriIndex ][ Name ], iNum == ( iPlayer iIndex ) ? "." ", " );
        }
    }
    
    if( !
iPlayer )
    {
        
addszBuffercharsmaxszBuffer ), "There are no vip's online." );
    }
    
    
CC_SendMessageidszBuffer );


I can do this with two loop but i think it's possible to do it with just one loop.

jimaway 01-28-2022 05:31

Re: Get max array of X flags in the loop
 
Code:
new istrlen for( new iIndex, i; i < iNum; i++  )     {         if( g_iPlayer[ ( iIndex = szPlayers[ i ] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettings[ Access_OnlineList ] )         {             istrlen += formatex( szBuffer[istrlen], charsmax(szBuffer) - istrlen, "%s,",g_iPlayer[ iIndex ][ Name ]);         }     } szBuffer[istrlen-1] = '.'

Supremache 01-28-2022 06:09

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by jimaway (Post 2769667)
Code:
new istrlen for( new iIndex, i; i < iNum; i++  )     {         if( g_iPlayer[ ( iIndex = szPlayers[ i ] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettings[ Access_OnlineList ] )         {             istrlen += formatex( szBuffer[istrlen], charsmax(szBuffer) - istrlen, "%s,",g_iPlayer[ iIndex ][ Name ]);         }     } szBuffer[istrlen-1] = '.'

It's not what i want, what i want is adding symbol ( , ) after vip names except for the last one without creating a double loop in the same function

jimaway 01-28-2022 06:29

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by Supremache (Post 2769669)
what i want is adding symbol ( , ) after vip names except for the last one without creating a double loop in the same function

that's exactly what it does

Supremache 01-28-2022 07:15

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by jimaway (Post 2769670)
that's exactly what it does

Yes, i was think adding strlen out the loop will do bug but it's work
Thanks you :)

PHP Code:

@OnVipsOnlineid )
{
    new 
szBufferMAX_TEXT_LENGTH ], szPlayersMAX_PLAYERS ], iPlayeriNum;
    
formatexszBuffercharsmaxszBuffer ), "&x04Online:&x01 " );
    
    
get_playersszPlayersiNum "ch" );

    for( new 
iIndexiiNumi++ )
    {
        if( 
g_iPlayer[ ( iIndex szPlayers] ) ][ VIP ] & g_iSettingsAccess_OnlineList ] )
        {  
// ( iPlayer = iIndex ) is see this way is better than using this iPlayer++ 
            
formatszBuffercharsmaxszBuffer ), "%s%s, "szBufferg_iPlayer[ ( iPlayer iIndex ) ][ Name ] );
        }
    }

    if( 
iPlayer )
    {
        
szBufferstrlenszBuffer ) - ] = '.' // Changed the strlen form -1 to -2 because i added a space 
    
}
    else
    {
        
addszBuffercharsmaxszBuffer ), "There are no vip's online." );
    }
    
    
CC_SendMessageidszBuffer );



Natsheh 01-28-2022 08:29

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by jimaway (Post 2769667)
Code:
new istrlen for( new iIndex, i; i < iNum; i++  )     {         if( g_iPlayer[ ( iIndex = szPlayers[ i ] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettings[ Access_OnlineList ] )         {             istrlen += formatex( szBuffer[istrlen], charsmax(szBuffer) - istrlen, "%s, ",g_iPlayer[ iIndex ][ Name ]);         }     } szBuffer[istrlen-2] = '.' szBuffer[istrlen-1] = EOS;


Duhhhhh !??!?

Supremache 01-28-2022 08:33

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by Natsheh (Post 2769676)
Duhhhhh !??!?

what do you mean ?
szBuffer[istrlen-2] = '.' // it work like replace so i think i dont need to clear it !!

Natsheh 01-28-2022 09:22

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by Supremache (Post 2769677)
what do you mean ?
szBuffer[istrlen-2] = '.' // it work like replace so i think i dont need to clear it !!

I meant why you didn't use jimmy's code and instead you kept using your code, jimmy's code is more efficient and faster.

And also you don't need the iPlayer variable just check whether the buffer is empty or not, like this...


PHP Code:

if( szBuffer[0] != EOS )
    {
        
szBufferstrlenszBuffer ) - ] = '.' // Changed the strlen form -1 to -2 because i added a space 
         
CC_SendMessage(idszBuffer);
    }
    else
    {
        
CC_SendMessageid"There are no vip's online." );
    } 


Shadows Adi 01-28-2022 09:35

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by Supremache (Post 2769677)
what do you mean ?
szBuffer[istrlen-2] = '.' // it work like replace so i think i dont need to clear it !!

PHP Code:

new istrlen
for( new iIndexiiNumi++  )
{
    if( 
g_iPlayer[ ( iIndex szPlayers] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettingsAccess_OnlineList ] )
    { 
        
istrlen += formatexszBuffer[istrlen], charsmax(szBuffer) - istrlen"%s%s "g_iPlayeriIndex ][ Name ], == iNum -"" ",");
    }



Natsheh 01-28-2022 09:38

Re: Get max array of X flags in the loop
 
Quote:

Originally Posted by Shadows Adi (Post 2769687)
PHP Code:

new istrlen
for( new iIndexiiNumi++  )
{
    if( 
g_iPlayer[ ( iIndex szPlayers] ) /* Check who has a vip flag */  ][ VIP ] & g_iSettingsAccess_OnlineList ] )
    { 
        
istrlen += formatexszBuffer[istrlen], charsmax(szBuffer) - istrlen"%s%s "g_iPlayeriIndex ][ Name ], == iNum -"" ",");
    }




This might not work because iNum might not be the last vip index.


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

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