Is it possible for a switch to be called when an Item is added into the inventory?

This is going to take a bit of explanation so stay with me. I have bottles in my game which can hold liquids like water and vinegar. I want these bottles to be reusable, like you can drink the water in the bottle and the game gives you an empty bottle. So far I've been able to keep the bottles reusable, but I've ran into a problem. I also have a crafting system in my game (this one) and some recipes require bottles with liquid in them (EX: The player can make pickles and it requires cucumber and a bottle of vinegar to make.) The problem is, with this crafting plugin, the bottles just get deleted once used, effectively ruining the reusability of my bottles.

I've tried a couple solutions, but all of them were either completely un-effective, or cannot be completed without what I'm going to ask. Which leads me to it, is it possible to trigger a common event/switch/variable-change via script/event/plugin after a specific item is added to the inventory?

My current attempt at solving this: I have a parallel event running which checks if the player has Pickles (which require a bottle of vinegar to make.) If they do, it goes to another conditional branch which checks if a variable is set to 0. If it is, it gives the player an empty bottle, making it seem like the bottle was just emptied and not totally deleted then re-added. The first problem with this lies in where to change the variable. Obviously, if the variable is not turned, the game will give the player an infinite amount of bottles if the player has pickles in their inventory. I've tried putting the variable change in a couple places to no real effect. What I really need is a check to trigger when the item is added, not if it persists in the inventory. This way, I can safely add the item to the inventory without the possibility of it giving the player an infinite amount.

I assume this problem dose not have a simple solution. But I hope it does. Thank you guys for the help and suggestions!
1714983203062.png
1714983315428.png
if there are not too many variants of the bottle, you can try going an entire other sequence:

store the amount of filled bottles in different variable for each.
then in a common event, check if any of those amounts was decreased - if yes, add empty bottles.

depending on if you start the crafting by event or by menu you can either call that check after each crafting event, or have to use a parallel process withg a wait(10) or so.
save this as a plugin and load it below the crafting plugin. If you place the notetag <bottle: x> in an item's notebox, when that item is used up as a recipe ingredient, the player will be given the same quantity of item x on craft

Spoiler
Code:
    Scene_CraftingMenu.prototype.loseIngredients = function(){
        var item = this._indexWindow.item();
           var ing;
           var db;
           var needed;

           item._ingredients.forEach(function(ing){
               needed = ing['amount'];
               if (ing['item'] == currencyText){
                   $gameParty.loseGold(needed);
               } else {
                   if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
                $gameParty.loseItem(ing['item'], needed);
               }
           }, this);
        this._ingredientsWindow.refresh();
    };
save this as a plugin and load it below the crafting plugin. If you place the notetag <bottle: x> in an item's notebox, when that item is used up as a recipe ingredient, the player will be given the same quantity of item x on craft

Spoiler
Code:
    Scene_CraftingMenu.prototype.loseIngredients = function(){
        var item = this._indexWindow.item();
           var ing;
           var db;
           var needed;

           item._ingredients.forEach(function(ing){
               needed = ing['amount'];
               if (ing['item'] == currencyText){
                   $gameParty.loseGold(needed);
               } else {
                   if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
                $gameParty.loseItem(ing['item'], needed);
               }
           }, this);
        this._ingredientsWindow.refresh();
    };
Thank you for the code! I've tried doing what you said, and I haven't been able to recreate the outcome, I think I need more detail.

I copy/pasted your code onto notepad++ and saved it as a .js file, then imported it into rpg maker and moved it just below the crafting plugin. Then, in the note tags of the vinegar bottle, I added: <39: 1> (39 is the Item ID of the empty bottle). This didn't work, so I tried: <Empty bottle gourd: 1>('Empty bottle gourd' is the name of the empty bottle item.) This also didn't work. I then went back to notepad++ and removed the 4 spaces before every line and retried the note tags, but this didn't work either.

Hopefully I've presented enough info to spot my discrepancy. Again, thank you for the code, I know very little about java script and its a huge help! If I have misread or misunderstood what you posted I apologies and would be very grateful to receive more insight!
1715023141220.png

1715023162920.png

1715023193705.png


1715023240053.png
Then, in the note tags of the vinegar bottle, I added: <39: 1> (39 is the Item ID of the empty bottle). This didn't work, so I tried: <Empty bottle gourd: 1>('Empty bottle gourd' is the name of the empty bottle item.)
That's not matching Robro's instructions. He said:
place the notetag <bottle: x> in an item's notebox, when that item is used up as a recipe ingredient, the player will be given the same quantity of item x on craft

So in the Notes field for item ID 41, you should put
Code:
<bottle: 39>
to get back one item ID 39 (empty gourd) for every filled gourd that's consumed by a recipe.
if there are not too many variants of the bottle, you can try going an entire other sequence:

store the amount of filled bottles in different variable for each.
then in a common event, check if any of those amounts was decreased - if yes, add empty bottles.

depending on if you start the crafting by event or by menu you can either call that check after each crafting event, or have to use a parallel process withg a wait(10) or so.
I tried something like this, but I may have did it wrong; I don't have too many variants of the bottle item currently, but I do plan on adding a lot more, which may muddle things.

The problem I'm running into is that, when using a vinegar bottle when crafting, it simply deletes the vinegar bottle. I cant really change a variable when its deleted (to my knowledge), so setting up variables for this would prove challenging. Not to mention, keeping a bunch of variables and having them constantly changing throughout the game would be daunting, but if its the only way, I would do it.

If you know of a work around I would be very grateful if you shared it, thank you!

That's not matching Robro's instructions. He said:


So in the Notes field for item ID 41, you should put
Code:
<bottle: 39>
to get back one item ID 39 (empty gourd) for every filled gourd that's consumed by a recipe.
Ah, thank you for catching my mistake!

I tried what you said, but I still didn't receive the empty bottle once it was consumed. I may have overcomplicated things a bit so I'm just going to ask here: Should the code in the .js file have the 4 spaces before every line or should they not? I'm pretty sure they aren't, but I don't know entirely. I tried both plugins (with and without the spaces) and neither of them worked with what you said.

Thank you for your time, sorry for my naivety lol.
1715025341939.png

1715025383054.png

1715025441187.png
I may have overcomplicated things a bit so I'm just going to ask here: Should the code in the .js file have the 4 spaces before every line or should they not?
As a rule, you shouldn't edit code you're given unless the instructions say to.

In JavaScript, spaces don't matter - they're entirely for correct formatting, so someone who knows how to read the code can do so comfortably.

At first glance, it looks like Robro's code should work, but I haven't tested it. If you press F8 in the game and go to the Console tab, do you see any errors?
Just making sure. You've since then saved the project and you are creating a new save file with every test instead of loading an old one?
I still didn't receive the empty bottle once it was consumed
As a rule, you shouldn't edit code you're given unless the instructions say to.

In JavaScript, spaces don't matter - they're entirely for correct formatting, so someone who knows how to read the code can do so comfortably.

At first glance, it looks like Robro's code should work, but I haven't tested it. If you press F8 in the game and go to the Console tab, do you see any errors?

I see, thank you. I would've downloaded it and not have to worry about the spaces, but I don't think there its a way to just download the code, so I just copy/pasted.

I ran the OG code plugin and did get an error relating to the crafting. (bottlefix2 is the plugin name)

1715026664000.png
Just making sure. You've since then saved the project and you are creating a new save file with every test instead of loading an old one?
Yep. I have a devroom I load into once I create a new save file so I can test this stuff.
I ran the OG code plugin and did get an error relating to the crafting.
Ah. That'll do it. I tested it with a direct edit. Didnt think about it not being loaded.

Go within the plugin, search for "Scene_CraftingMenu.prototype.loseIngredients = function(){" with ctrl+f and paste over that function with what I gave before (or just take the extra line I added
Code:
if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
and put it where I did.)
I wont be home for another hour and all I have is my phone, so I cant give you the line number or write in an alias
Ah. That'll do it. I tested it with a direct edit. Didnt think about it not being loaded.

Go within the plugin, search for "Scene_CraftingMenu.prototype.loseIngredients = function(){" with ctrl+f and paste over that function with what I gave before (or just take the extra line I added
Code:
if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
and put it where I did.)
I wont be home for another hour and all I have is my phone, so I cant give you the line number or write in an alias
Thank you for the response! I'm a bit confused as to what you mean by:
paste over that function with what I gave before (or just take the extra line I added and put it where I did.)
I don't see what you mean when you say "put it where I did". I feel like maybe there's a reference that was left out in your post or is something I'm missing. Is it possible for you to maybe edit the original code with the new line added in so that I don't accidently mess the code up? I tried copy and pasting the new line under the "Scene_CraftingMenu.prototype.loseIngredients = function(){" and got the "ReferenceError" error message. I then tried to replace the "Scene_Crafting" function with the new line (which I'm positive is the wrong way to do it) and got the "SyntaxError" error message. I think its possible the syntaxerror was a mistake on my part, but I wouldn't know for sure.

I mean no disrespect, and I understand it may be slightly aggravating to deal with someone with such little java script understanding as I do. I just want to keep this gameplay mechanic intact and this seems to be the most efficient way of doing so. As for time, I'm willing to wait as long as need be for a solution to be found, so no pressure there. Again thank you for the code, I hope we can get this sorted out!

1715035944318.png

1715035873521.png

1715034834831.png

1715034814660.png
I hope we can get this sorted out!
i was a bit unclear and was in a bit of a rush when i said what to do earlier, no worries.

The only change that was made was the addition of a line. on a copy of the original plugin with no edits, go to line 366. it should look like this
Code:
} else {
click on the very end of the line and hit enter to add a new one. in that new line (now 367), paste in
Code:
if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
and save it. thats all thats needed. no need for the separate js file
i was a bit unclear and was in a bit of a rush when i said what to do earlier, no worries.

The only change that was made was the addition of a line. on a copy of the original plugin with no edits, go to line 366. it should look like this
Code:
} else {
click on the very end of the line and hit enter to add a new one. in that new line (now 367), paste in
Code:
if (ing.item.meta.bottle) $gameParty.gainItem($dataItems[Number(ing.item.meta.bottle)], needed);
and save it. thats all thats needed. no need for the separate js file
Yes! That works! Just tested it with my bottles and it works flawlessly. The amount of time you've saved me is incomprehensible, thank you.

I appreciate all the responses guys, very glad we could get this working together!
1715040443509.png

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-for-a-switch-to-be-called-when-an-item-is-added-into-the-inventory.168407/