I need to use an item from an event (MV)

Hi everyone, is there a way to use objects with code? I want certain objects to be consumed automatically when a condition is met. If you know of any methods or add-ons, please let me know.

I know there’s a way to get rid of objects, but that doesn’t work for me; I want them to be used, since that’s how their effects are applied.

@Baz In response to your comment on the previous post, that method wouldn’t work for me because I need a random item from my inventory that has a tag to be consumed. I have some items that were tagged as Food, Drink, Medicine, and Sanity, and in my event, there’s a condition that checks whether the character’s hunger, thirst, sanity, or health is less than or equal to a certain value, in order to consume a random item depending on which stat needs to be boosted.

I currently have a system that detects which items are food, drink, or something else, but the only problem I’m having is that I can’t consume them properly.

I’m using RPG Maker MV, by the way.

MV doesn’t have an event command for this, but you can build the same Game_Action the item menu builds. In a Script command:

var item = $dataItems[$gameVariables.value(1)];
if (item) {
    var user = $gameParty.leader();
    var action = new Game_Action(user);
    action.setItemObject(item);
    action.apply(user);
    action.applyGlobal();
    user.useItem(item);
}

Variable 1 there is the item ID, so your tag system just needs to drop the chosen ID into a variable and this picks it up. apply() runs the effects on whoever you pass in (the party leader here), applyGlobal() is the piece that handles the item’s Common Event effect, and useItem() is what actually pulls it out of the inventory.

Worth knowing that applyGlobal reserves the Common Event rather than running it inline, so it fires once the current event finishes instead of partway through it.

I haven’t tested this against your setup. The thing I’d watch for is MV skipping effects it decides would do nothing, like an HP recovery item on someone already at full HP, and the item still gets consumed in that case.

It worked!!! I set it up this way and now it’s working perfectly. Thank you so much :smiley:

By the way, if you want to try my game, it’s called Eternal Levels, you can download the demo from Steam. It’s a 2D survival game based on the Backrooms: Steam:Eternal Levels