AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to order items by price? (https://forums.alliedmods.net/showthread.php?t=226189)

colossus 09-14-2013 16:06

How to order items by price?
 
An example:


PHP Code:

show_menu_extras(id)
{
    if (!
is_user_alive(id))
        return;
    
    static 
menuidmenu[128], teambuffer[32]

    
formatex(menucharsmax(menu), "Items Menu")
    
menuid menu_create(menu"menu_extras")

    for (new 
0g_extraitem_ii++)
    {
        
formatex(menucharsmax(menu), "%s \r[Cost: %d]"bufferArrayGetCell(g_extraitem_costi))
        
buffer[0] = i
        buffer
[1] = 0
        menu_additem
(menuidmenubuffer)
    }

    if (
menu_items(menuid) <= 0)
    {
        
client_print(idprint_chat"No items in menu")
        
menu_destroy(menuid)
        return;
    }
    
    
// Back - Next - Exit
    
menu_setprop(menuidMPROP_BACKNAME"Back page")
    
menu_setprop(menuidMPROP_NEXTNAME"Next page")
    
menu_setprop(menuidMPROP_EXITNAME"Exit menu")
        
    
// If remembered page is greater than number of pages, clamp down the value
    
MENU_PAGE_EXTRAS min(MENU_PAGE_EXTRASmenu_pages(menuid)-1)
    
    
// Fix for AMXX custom menus
    
if (pev_valid(id) == PDATA_SAFE)
        
set_pdata_int(idOFFSET_CSMENUCODE0OFFSET_LINUX)
    
    
menu_display(idmenuidMENU_PAGE_EXTRAS)


This plugin what the zombie_plague40 kick and I need to know how to sort the items by price ...

PS: These items are added with a native ...

Example:

zp_register_extra_item ("Super Gun", 30, ZP_TEAM_HUMAN)

Black Rose 09-14-2013 16:54

Re: How to order items by price?
 
Since I'm guessing you have 2 dynamic arrays that stores name and cost ArraySort() is out of the question since it won't match the other array.

You could either make your own sorting function using ArraySwap on both arrays to keep them synced.
Another option is to create a new array that works like a key to both arrays and sort that using 1DCustom.

I can't write it for you since the function you supplied is missing the name array...

EDIT:
Here's the basics of the first method:
Code:
new size = ArraySize(hArray); for ( new i = 1 ; i < size ; i++ ) {     for ( new j = i ; j > 0 ; j-- ) {         if ( ArrayGetCell(hArray, j) < ArrayGetCell(hArray, j - 1) ) { // Use < for ascending and > for descending.             ArraySwap(hArray, j, j - 1);             ArraySwap(hStringArray, j, j - 1);         }         else             break;     } }


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

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