AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Max Usage Of A Item In Shop (https://forums.alliedmods.net/showthread.php?t=331516)

DON KHAN 1 03-24-2021 04:47

Max Usage Of A Item In Shop
 
Hello.
I am running a RECSDM mod.
I have added a Shop plugin.
I want to add limit of each item and limit get reset after getting killed.
Thank You

AnimalMonster 03-24-2021 09:33

Re: Max Usage Of A Item In Shop
 
Suggestions / Requests , wrong section buddy and provide sma to the item shop next time.

OciXCrom 03-24-2021 09:40

Re: Max Usage Of A Item In Shop
 
Quote:

Originally Posted by AnimalMonster (Post 2741582)
Suggestions / Requests , wrong section buddy and provide sma to the item shop next time.

How is it the wrong section when he's asking how to do it?

--------------

Create a global variable with 2 dimensions - the player index and item index.
Increase [id][item] by 1 when buying the item.
Check if [id][item] reached your desired limit, stop the code if it did.
When the player dies, loop through all player indexes and use arrayset() to reset the limit for each item.

AnimalMonster 03-24-2021 15:23

Re: Max Usage Of A Item In Shop
 
Quote:

Originally Posted by OciXCrom (Post 2741584)
How is it the wrong section when he's asking how to do it?

--------------

Create a global variable with 2 dimensions - the player index and item index.
Increase [id][item] by 1 when buying the item.
Check if [id][item] reached your desired limit, stop the code if it did.
When the player dies, loop through all player indexes and use arrayset() to reset the limit for each item.

Umm, i thought we wanted us to edit the plugin? + he didn't provide any sma either

DON KHAN 1 03-24-2021 15:34

Re: Max Usage Of A Item In Shop
 
Can u define me with some examples? it would be great.

Crackhead69 03-25-2021 06:35

Re: Max Usage Of A Item In Shop
 
That's just one way of doing it
PHP Code:

#include <amxmodx>

new ItemUsed // its purpose is Global
new ItemId[33// its purpose is to hold the items

public plugin_init()    
    
register_clcmd"say /menu","AwesomeMenu" );

public 
AwesomeMenuid )
{
    new 
menu menu_create"Look at this awesome Menu!:""menu_handler" );

    
menu_additemmenu"I'm Selection #1""");
    
menu_additemmenu"I'm Selection #2""");

    
menu_setpropmenuMPROP_EXITMEXIT_ALL );

    
menu_displayidmenu);

}

public 
menu_handleridmenuitem )
{
    if(
ItemId[item] == ItemUsed//if the selected item equals 5 (gItemUsed) we return
    
{
        
client_print(id,print_chat,"Limit reached")
        return
    }
    
    
ItemId[item]++ //we increase the picked item's number by 1

    
switch(item)
    {
        case 
0:client_printidprint_chat"Item 1 can be used: %i/%i times"ItemId[item],ItemUsed)
        case 
1:client_printidprint_chat"Item 2 can be used: %i/%i times"ItemId[item],ItemUsed)
    }



OciXCrom 03-25-2021 08:29

Re: Max Usage Of A Item In Shop
 
That's a global limit for all items, not per player.

redivcram 03-25-2021 10:55

Re: Max Usage Of A Item In Shop
 
Normally you would do something like this instead.

PHP Code:

enum _:eShopItems {

        
ITEM_NAME[32],
        
ITEM_COST,
        
ITEM_MAX_USES,
        
etc...
};

// Here are your item definitions
new const g_ShopItems[MAX_ITEMS][eShopItems] {

        { 
"Item 1"160005etc.}, 
        { 
"Second item"80003etc.}, 
        
etc.


Then accordingly acces each element using the enumeration for better coordination. What you did there was define a max usage for every item.

Crackhead69 03-25-2021 11:59

Re: Max Usage Of A Item In Shop
 
Yes, my bad.
Ideally then it should be somewhat like this
PHP Code:

#include <amxmodx>

new ItemUsed[33][33]

enum _:Items
{
    
Name[64],
    
Limit[33]
}

new const 
szItemInfo[2][Items] =
{
    {
"Item 1"5},
    {
"Item 2"3}
}

public 
plugin_init()
    
register_clcmd("say /menu","openmenu")

public 
openmenu(id)
{
    new 
menu menu_create("Testing","menu_handle")
    for(new 
ii<2i++)
    {
        static 
szMenuTitle[64], szInfo[3]
        
        
formatex(szMenuTitlecharsmax(szMenuTitle), "%s",szItemInfo[i][Name])
        
num_to_str(iszInfocharsmax(szInfo))
        
menu_additem(menuszMenuTitleszInfo)
    }
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL)
    
menu_display(idmenu0);
    return 
PLUGIN_HANDLED
}

public 
menu_handle(id,menu,item){
    if(
item == MENU_EXIT || !is_user_alive(id))
    {
        
menu_cancel(id)
        return
    }
    if(
ItemUsed[id][item]++ >=  szItemInfo[item][Limit])
    {
        
client_print(id,print_chat,"Item limit reached")
        return
    }
    switch(
item){
        case 
0:client_print(idprint_chat"Option 1");
        case 
1:client_print(idprint_chat"Option 2");
    }



redivcram 03-25-2021 12:28

Re: Max Usage Of A Item In Shop
 
PHP Code:

enum _:Items
{
    
Name[64],
    
Limit[33]


Why is Limit an array? It only takes one number. Name is an array of characters or a string with a maximum of 64 characters available in this case.


If you're on AMXX 1.9 or greater, consider ditching the formatex function and using fmt(). You'd get this:
PHP Code:

    for(new ii<2i++)
    {
        static 
szInfo[3]
        
        
num_to_str(iszInfocharsmax(szInfo))
        
menu_additem(menufmt("%s"szItemInfo[i][Name]), szInfo)
    } 

Be careful though, if you were to use fmt inside another fmt, you'd get errors, but this is a much better approach in your case.


Also, there is no need to increment ItemUsed in there if the condition results in false, just put it under the switch statement.


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

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