Return Consumable item Id

Hello,
I'm trying to get store the id of a consumable as it's used. Long story short, I creating several alcoholic consumables that run through a common event that checks how sloppy the characters gotten. When they've had too many, the system kicks back a message saying so and prevents further use. The problem is that I couldn't find a way to do this without consuming the item. My solution was to use a $gameParty.gainItem with the items id to restore the lost item. I can't figure out the script to return the used item's id though. Any help would be much appreciated.
If you have a Game_Item object (i.e. anything from the party inventory) then you should be able to retrieve its ID as item.itemId(). If you're using an entry from $dataItems then it'll be item.id. Plugins may affect this (e.g. independent items from Yanfly's Item Core).

I actually made a simple plugin a while back that lets you set script conditions in notetags for consuming items; the item still gets applied to the target as usual, but if the condition isn't met then it won't be consumed. Not sure if that's quite what you want, and my approach is probably not the most efficient, but here's a link in case you want to have a look: view/download file (Google Drive).

When selecting an item to use, the game first checks whether anyone in the party can use the item, by calling the canUse function on each party member. So another approach could be to alias the canUse function. 🙂
you shouldnt need a script call for this. just use Change Item to add 1 back into inventory same time as the "to drunk" message in the common event
Thanks for the quick responses. Apologies in advance for my lack of knowledge on how to work a forum. This is like the third time in my life I've used one.
you shouldnt need a script call for this. just use Change Item to add 1 back into inventory same time as the "to drunk" message in the common event
The reason this didn't work for me is that Change Item requires you to select the item. I was trying to get the common event to store the itemId as a variable and then run Change Item taking the variable as one of the arguments. That way I wouldn't need to build a conditional branch for each item.
If you have a Game_Item object (i.e. anything from the party inventory) then you should be able to retrieve its ID as item.itemId(). If you're using an entry from $dataItems then it'll be item.id. Plugins may affect this (e.g. independent items from Yanfly's Item Core).

I actually made a simple plugin a while back that lets you set script conditions in notetags for consuming items; the item still gets applied to the target as usual, but if the condition isn't met then it won't be consumed. Not sure if that's quite what you want, and my approach is probably not the most efficient, but here's a link in case you want to have a look: view/download file (Google Drive).
Your plugin worked like a charm. For my own learning though, what would be the proper way to call the item.itemId() function. I'm a total novice with javascript. I've tried:
Code:
$gameVariables.setValue(15,$dataItems[item.itemId()])
as well as several other iterations of that. In the common events, it just doesn't work. Using notetag scripts (with the HIME_FormulaEffects plugin), it breaks the game and returns a type error.
Your plugin worked like a charm. For my own learning though, what would be the proper way to call the item.itemId() function. I'm a total novice with javascript. I've tried:
Code:
$gameVariables.setValue(15,$dataItems[item.itemId()])
as well as several other iterations of that. In the common events, it just doesn't work.
This is because script calls are executed in the context of an event interpreter, where there's generally no "item" variable to reference (thus, probably, a "cannot read property x of undefined" error). When an item is used from the inventory (Scene_Item) it performs the effects then, if there's a common event to run, it returns to Scene_Map, unloading the previous scene and its data in the process. Long story short, I'm fairly sure the engine has forgotten which item was used by the time it runs any attached common event. 😅

Without using a plugin to extend/override the basic code, one workaround could be to assign a variable in the item formula (assuming your item makes use of the formula box). E.g. if your item has a formula like this:
Code:
50 + a.mat
You could change it to something like this:
Code:
v[15] = this.item().id; 50 + a.mat
This should set game variable 15 equal to the ID of the item being used, then evaluate and return the original formula as usual. 😂

The damage formula is evaluated in a Game_Action context, hence the access to the item function. This function retrieves the database entry for the appropriate item, so the item ID is found in the id property. 🙂

If you want to avoid using the formula box, Yanfly's Skill Core is a plugin that offers, for example, an <After Eval> skill/item notetag. With that, you could try something like this:
Code:
<After Eval>
$gameVariables.setValue(15, item.id);
</After Eval>
Yanfly's plugin explicitly defines the item variable before processing the notetag eval, that's why it can be referenced here so concisely/easily~
Oh I see. Both methods took care of it. I guess the plugin running the notetags before weren't defining the variable before running the common event. I'm quickly discovering I need to pick apart how the software runs to make full use of the plugins. I planned on it anyways, just not so soon. Thank you for the help.

Original thread: https://forums.rpgmakerweb.com/threads/return-consumable-item-id.107413/