Raised This Month: $51 Target: $400
 12% 

[TUT] Shop Menu For Beginners


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 04-12-2013 , 08:21   [TUT] Shop Menu For Beginners
Reply With Quote #1

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.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike> 
Now we create the variables that will hold the shop and not enough money
PHP Code:
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
PHP Code:
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
PHP Code:
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.
PHP Code:
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()
PHP Code:
public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
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
PHP Code:
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 
ii<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(szItemNamecharsmax(szItemName), "\d%s ($%d)"g_szItems], g_iItemsPrices])
            
// if the item cost more then players money we show the item as grey'd out color for disabled
        
}
        else {
            
formatex(szItemNamecharsmax(szItemName), "%s (\r$%d\w)"g_szItems], g_iItemsPrices])
            
// if the item cost less then players money we show the item as available
        
}
        
menu_additem(menuszItemName)
        
// now we add the items into the menu auto adding all items from the above constants.
    
}
    
    
menu_setprop(menuMPROP_NUMBER_COLOR"\y")
    
    
menu_display(idmenu0);
    
    return 
PLUGIN_HANDLED;
}

public 
handler_ShopMenu(idmenuitem)
{
    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] ) 
    {
        
NotEnoughMoneyid );
        
menu_display(idmenu);
        return 
PLUGIN_HANDLED;
    }
    
    switch(
item)
    {
        case 
0
        {
            
// if player already holds this item we show a cant carry anymore messge
            
if( user_has_weapon(idCSW_HEGRENADE) )
            {
                
Cannot_Carry_Anymoreid );
            }
            else {
            
// otherwise we give item to player
                
give_item(id"weapon_hegrenade");
                
                
client_print(idprint_center"You Bought (%s)"g_szItems[item]);

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

    
menu_destroy(menu);
    return 
PLUGIN_HANDLED;

Now lets create those functions for the Cannot_Carry_Anymore( id ) & NotEnoughMoney( id )
PHP Code:
NotEnoughMoneyid )
{
    
client_print(idprint_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_UNRELIABLEg_iBlinkAcct, .player=id);
    {
        
write_byte(2);
    }
    
message_end();
}

Cannot_Carry_Anymoreid )
{
    
client_print(idprint_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.

PHP Code:
#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(PLUGINVERSIONAUTHOR)
    
    
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 
ii<sizeof(g_szItems) && i<sizeof(g_iItemsPrices); i++)
    {
        if( 
money g_iItemsPrices[i] )
        {
            
formatex(szItemNamecharsmax(szItemName), "\d%s ($%d)"g_szItems], g_iItemsPrices])
        }
        else {
            
formatex(szItemNamecharsmax(szItemName), "%s (\r$%d\w)"g_szItems], g_iItemsPrices])
        }
        
menu_additem(menuszItemName)
    }
    
    
menu_setprop(menuMPROP_NUMBER_COLOR"\y")
    
    
menu_display(idmenu0);
    
    return 
PLUGIN_HANDLED;
}

public 
handler_ShopMenu(idmenuitem)
{
    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] )
    {
        
NotEnoughMoneyid );
        
menu_display(idmenu);
        return 
PLUGIN_HANDLED;
    }
    
    switch(
item)
    {
        case 
0
        {
            if( 
user_has_weapon(idCSW_HEGRENADE) )
            {
                
Cannot_Carry_Anymoreid );
            }
            else {
                
give_item(id"weapon_hegrenade");
                
                
client_print(idprint_center"You Bought (%s)"g_szItems[item]);

                
cs_set_user_money(idnew_money);
            }
        }
        case 
1:
        {
            if( 
user_has_weapon(idCSW_SMOKEGRENADE) )
            {
                
Cannot_Carry_Anymoreid );
            }
            else {
                
give_item(id"weapon_smokegrenade");
                
                
client_print(idprint_center"You Bought (%s)"g_szItems[item]);
                
                
cs_set_user_money(idnew_money);
            }
        }
        case 
2:
        {
            if( 
user_has_weapon(idCSW_FLASHBANG) )
            {
                
Cannot_Carry_Anymoreid );
            }
            else {
                
give_item(id"weapon_flashbang");
                
                
client_print(idprint_center"You Bought (%s)"g_szItems[item]);
                
                
cs_set_user_money(idnew_money);
            }
        }
    }

    
menu_destroy(menu);
    return 
PLUGIN_HANDLED;
}

NotEnoughMoneyid )
{
    
client_print(idprint_center"#Cstrike_TitlesTXT_Not_Enough_Money");

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

Cannot_Carry_Anymoreid )
{
    
client_print(idprint_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.
__________________

Last edited by Blizzard_87; 04-13-2013 at 02:46. Reason: Updated.
Blizzard_87 is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 04-12-2013 , 10:27   Re: [TUT] Dynamic Shop Menu For Beginners
Reply With Quote #2

Tell me what you think will happen when I open the shop menu, and then you open the shop menu, and one of us chooses an option?
Either build the menu once, or don't make the menu handle global.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-12-2013 , 13:35   Re: [TUT] Dynamic Shop Menu For Beginners
Reply With Quote #3

public handler_ShopMenu(id, g_shop_menu, item)

You shouldn't pass g_shop_menu here but menu

Also, rather than cmdShop(id); to show again menu, do menu_display(id, menu), where menu is g_shop_menu replacement from the function handler.
Also there you were quitting menu without destroying it.

As said, format the menu once since menu is not dynamic at all as your title seems to say.
Menu is easy to edit, but is not dynamic.


Write a tutorial means you have a good understanding of its subject, which is obvliously not the case here.
Anyway, you have now all keys to fix it.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 04-12-2013 , 18:02   Re: [TUT] Dynamic Shop Menu For Beginners
Reply With Quote #4

Quote:
Originally Posted by ConnorMcLeod View Post
public handler_ShopMenu(id, g_shop_menu, item)

You shouldn't pass g_shop_menu here but menu

Also, rather than cmdShop(id); to show again menu, do menu_display(id, menu), where menu is g_shop_menu replacement from the function handler.
Also there you were quitting menu without destroying it.

As said, format the menu once since menu is not dynamic at all as your title seems to say.
Menu is easy to edit, but is not dynamic.


Write a tutorial means you have a good understanding of its subject, which is obvliously not the case here.
Anyway, you have now all keys to fix it.
Conner i hope i understood what you've said and edited it according to your help?
__________________
Blizzard_87 is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-13-2013 , 01:55   Re: [TUT] Shop Menu For Beginners
Reply With Quote #5

It's better now.

You should consider those private functions so strings are stored only once in memory :
PHP Code:
NotEnoughMoneyid )
{
    
client_print(idprint_center"#Cstrike_TitlesTXT_Not_Enough_Money");

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

Cannot_Carry_Anymoreid )
{
    
client_print(idprint_center"#Cstrike_TitlesTXT_Cannot_Carry_Anymore");


menu_setprop(menu, MPROP_EXIT, MEXIT_ALL);

This is default setting, so useless to specify.
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 04-13-2013 at 01:56.
ConnorMcLeod is offline
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 04-13-2013 , 02:47   Re: [TUT] Shop Menu For Beginners
Reply With Quote #6

Quote:
Originally Posted by ConnorMcLeod View Post
It's better now.

You should consider those private functions so strings are stored only once in memory :
PHP Code:
NotEnoughMoneyid )
{
    
client_print(idprint_center"#Cstrike_TitlesTXT_Not_Enough_Money");

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

Cannot_Carry_Anymoreid )
{
    
client_print(idprint_center"#Cstrike_TitlesTXT_Cannot_Carry_Anymore");


menu_setprop(menu, MPROP_EXIT, MEXIT_ALL);

This is default setting, so useless to specify.
edited my post to your suggestions thanks.
__________________
Blizzard_87 is offline
Sherazaa
Member
Join Date: Jun 2012
Old 04-13-2013 , 05:16   Re: [TUT] Shop Menu For Beginners
Reply With Quote #7

Good tut ! But do not include amxmisc it's useless in this case.
__________________
[B]Bhop & Kz Player
Sherazaa is offline
Old 08-04-2014, 17:50
mFsethh
This message has been deleted by YamiKaitou. Reason: not the place for plugin requests
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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