[TUT] Shop Menu For Beginners

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

[TUT] Shop Menu For Beginners

Post by Archive-Poster »

I've made up a simple tut for shop menus for newer coders.

this menu will show you how to add extra items easily instead of having to add them all in manually.

Include the modules you need.

Code: Select all

#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike>

Code: Select all

Now we create the variables that will hold the shop and not enough money

Code: Select all

new g_iBlinkAcct;
Now we create the constant which holds the item names which you will see in the shop menu
for this example im using Grenade Names

Code: Select all

new g_szItems[][] = 
{
    "HE Grenade", 
    "Smoke Grenade",
    "Flash Bang"
} 
you can add as menu items as you want as long as you add the prices for items for the right amount of items as in code below.

Now we create the constant which holds the above items prices

Code: Select all

new g_iItemsPrices[] = 
{
    350, // price for HE nade
    200, // price for SM nade
    250, // price for FL nade
} 
make sure the amount of prices match the right amount of items in above code "g_szItems".

to add extra items ex.

Code: Select all

new g_szItems[][] = 
{
    "HE Grenade", 
    "Smoke Grenade",
    "Flash Bang",
    "item 4",
    "item 5",
    "item 6" // make sure the last one does NOT have a comma
}

new g_iItemsPrices[] = 
{
    350, // price for HE nade
    200, // price for SM nade
    250, // price for FL nade
    200, // price for item 4
    300, // price for item 5
    500, // price for item 6
} 
Now we create the plugin_init()

Code: Select all

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
    
    g_iBlinkAcct = get_user_msgid("BlinkAcct")
        // we register this variable to get the money blinking effect for insufficient funds 
    
    register_clcmd("say /shop", "cmdShop", 0);
        // we only need to register a client command to call the shop menu
} 
Now we create the shop

Code: Select all

public cmdShop(id)
{
    new Temp[101], money = cs_get_user_money(id);
    // we create new variables to hold menu title and players current money
    
    formatex(Temp,100, "\yShop Menu:^n^nYour Money:\r $%d", money);
    new menu = menu_create(Temp, "handler_ShopMenu")
    
    new szItemName[64]
    for(new i; i<sizeof(g_szItems) && i<sizeof(g_iItemsPrices); i++)
    {
        if( money < g_iItemsPrices[i] ) // we check here if the player has enough money for the item
        {
            formatex(szItemName, charsmax(szItemName), "\d%s ($%d)", g_szItems[ i ], g_iItemsPrices[ i ])
            // if the item cost more then players money we show the item as grey'd out color for disabled
        }
        else {
            formatex(szItemName, charsmax(szItemName), "%s (\r$%d\w)", g_szItems[ i ], g_iItemsPrices[ i ])
            // if the item cost less then players money we show the item as available
        }
        menu_additem(menu, szItemName)
        // now we add the items into the menu auto adding all items from the above constants.
    }
    
    menu_setprop(menu, MPROP_NUMBER_COLOR, "\y")
    
    menu_display(id, menu, 0);
    
    return PLUGIN_HANDLED;
}

public handler_ShopMenu(id, menu, item)
{
    if( item == MENU_EXIT )
    {
        menu_destroy(menu);
        return PLUGIN_HANDLED;
    }

    new money = cs_get_user_money(id);
    new new_money = cs_get_user_money(id) - g_iItemsPrices[item];
    // we create new variables to hold players current money
    // and to set players new money after purchasing items
    // g_iItemsPrices[item] gets the items price from which number you select from menu and checks that against
    // the item number from the constant from above plugin_init
    
    // now we check if players money is less then the item
    // and if it is then we show a insuffisent message and make the players money hud flash 
    if( money < g_iItemsPrices[item] ) 
    {
        NotEnoughMoney( id );
        menu_display(id, menu);
        return PLUGIN_HANDLED;
    }
    
    switch(item)
    {
        case 0: 
        {
            // if player already holds this item we show a cant carry anymore messge
            if( user_has_weapon(id, CSW_HEGRENADE) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
            // otherwise we give item to player
                give_item(id, "weapon_hegrenade");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);

                // and then reset players money to current money minus item price.
                cs_set_user_money(id, new_money);
            }
        }
        case 1:
        {
            // if player already holds this item we show a cant carry anymore messge
            if( user_has_weapon(id, CSW_SMOKEGRENADE) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
            // otherwise we give item to player
                give_item(id, "weapon_smokegrenade");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);
                
                // and then reset players money to current money minus item price.
                cs_set_user_money(id, new_money);
            }
        }
        case 2:
        {
            // if player already holds this item we show a cant carry anymore messge
            if( user_has_weapon(id, CSW_FLASHBANG) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
            // otherwise we give item to player
                give_item(id, "weapon_flashbang");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);
                
                // and then reset players money to current money minus item price.
                cs_set_user_money(id, new_money);
            }
        }
    }

    menu_destroy(menu);
    return PLUGIN_HANDLED;
} 
Now lets create those functions for the Cannot_Carry_Anymore( id ) & NotEnoughMoney( id )

Code: Select all

NotEnoughMoney( id )
{
    client_print(id, print_center, "#Cstrike_TitlesTXT_Not_Enough_Money");
    
    // and if it is then we show a insuffisent message and make the players money hud flash 
    message_begin(MSG_ONE_UNRELIABLE, g_iBlinkAcct, .player=id);
    {
        write_byte(2);
    }
    message_end();
}

Cannot_Carry_Anymore( id )
{
    client_print(id, print_center, "#Cstrike_TitlesTXT_Cannot_Carry_Anymore");
} 
now we have gone thru all the codes and how to use them..
i will show you the whole code without comments.

Code: Select all

#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike>

#define PLUGIN "Shop Tut For Newbies"
#define VERSION "1.0"
#define AUTHOR "Blizzard"

new g_iBlinkAcct;

// EDIT ITEMS BELOW
new g_szItems[][] = 
{
    "HE Grenade",
    "Smoke Grenade",
    "Flash Bang"
}
// END EDIT ITEMS

// EDIT ITEM PRICES BELOW
new g_iItemsPrices[] = 
{
    350, // Price Item 1
    200, // Price Item 2
    250, // Price Item 3
}
// END EDIT PRICES

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
    
    g_iBlinkAcct = get_user_msgid("BlinkAcct")
    
    register_clcmd("say /shop", "cmdShop", 0);
}

public cmdShop(id)
{
    new Temp[101], money = cs_get_user_money(id);
    
    formatex(Temp,100, "\yShop Menu:^n^nYour Money:\r $%d", money);
    new menu = menu_create(Temp, "handler_ShopMenu")
    
    new szItemName[64]
    for(new i; i<sizeof(g_szItems) && i<sizeof(g_iItemsPrices); i++)
    {
        if( money < g_iItemsPrices[i] )
        {
            formatex(szItemName, charsmax(szItemName), "\d%s ($%d)", g_szItems[ i ], g_iItemsPrices[ i ])
        }
        else {
            formatex(szItemName, charsmax(szItemName), "%s (\r$%d\w)", g_szItems[ i ], g_iItemsPrices[ i ])
        }
        menu_additem(menu, szItemName)
    }
    
    menu_setprop(menu, MPROP_NUMBER_COLOR, "\y")
    
    menu_display(id, menu, 0);
    
    return PLUGIN_HANDLED;
}

public handler_ShopMenu(id, menu, item)
{
    if( item == MENU_EXIT )
    {
        menu_destroy(menu);
        return PLUGIN_HANDLED;
    }

    new money = cs_get_user_money(id);
    new new_money = cs_get_user_money(id) - g_iItemsPrices[item];
    
    if( money < g_iItemsPrices[item] )
    {
        NotEnoughMoney( id );
        menu_display(id, menu);
        return PLUGIN_HANDLED;
    }
    
    switch(item)
    {
        case 0: 
        {
            if( user_has_weapon(id, CSW_HEGRENADE) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
                give_item(id, "weapon_hegrenade");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);

                cs_set_user_money(id, new_money);
            }
        }
        case 1:
        {
            if( user_has_weapon(id, CSW_SMOKEGRENADE) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
                give_item(id, "weapon_smokegrenade");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);
                
                cs_set_user_money(id, new_money);
            }
        }
        case 2:
        {
            if( user_has_weapon(id, CSW_FLASHBANG) )
            {
                Cannot_Carry_Anymore( id );
            }
            else {
                give_item(id, "weapon_flashbang");
                
                client_print(id, print_center, "You Bought (%s)", g_szItems[item]);
                
                cs_set_user_money(id, new_money);
            }
        }
    }

    menu_destroy(menu);
    return PLUGIN_HANDLED;
}

NotEnoughMoney( id )
{
    client_print(id, print_center, "#Cstrike_TitlesTXT_Not_Enough_Money");

    message_begin(MSG_ONE_UNRELIABLE, g_iBlinkAcct, .player=id);
    {
        write_byte(2);
    }
    message_end();
}

Cannot_Carry_Anymore( id )
{
    client_print(id, print_center, "#Cstrike_TitlesTXT_Cannot_Carry_Anymore");
} 
I hope this tut has been helpful with easily adding items to a shop menu with less amount of code.

Who is online

Users browsing this forum: No registered users and 1 guest