Process to check and take a number of items with different IDs

Hi folks,

My game is a farming game, and has crop items with multiple quality levels (like Stardew Valley). Each crop of a different quality is a different item in the database with a unique item ID, the same name, and a different sell price.

I have an NPC that asks the player to give them X of a certain crop (of which there are many possible kinds). I want to remove X of items with the matching name from the players inventory, in any combination of qualities (item IDs).

I keep over-complicating this in my head when I try to think through an efficient way to do this (such as setting a variable to the item name (string), cycling through the player's entire inventory, checking names of each item they have, and incrementing a variable each time to track quantity. Then, if the variable is high enough to pay the price, cycling through again to take the items with a matching name one at a time starting with the lowest quality while incrementing a second variable, and stopping when the variables match).

Before I waste a lot of time trying to make something work, do any of you RPG Maker wizards have a better idea?

In short, the process needs to be something like:

If the player has X or more of items named Y, remove items named Y, starting with the lowest item ID until X are removed.

Any help is much appreciated!
Here's the code that can do what you want with script calls; note that case matters (e.g., 'potion' is not the same as 'Potion') and everything that matches will be checked (e.g., 'This is not a Potion' will also be included as a 'Potion', and weapons/armor that have 'Potion' in their names are included).

Some of these can be easily changed - we can just convert everything to lowercase so that the case doesn't matter, we can only check items where the name starts with that string, we can only check items, we can automatically remove the items in the conditional branch so that we don't have to first check the quantities then remove them, etc.

Also, this plugin is based on the default RMMZ way of sorting the inventory by id so it assumes you're not using any other plugin that sorts things differently.

JavaScript:
/*:
 * @target MZ
 * @plugindesc Check for items matching a certain name and remove them, starting from the lowest id.
 * @author SeaPhoenix
 *
 */

function checkItemQuantity(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    const partyQuantity = items.reduce((sum, item) => sum + $gameParty.numItems(item), 0);
    return partyQuantity >= quantity;
};

function removeMatchingItems(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    let remainingQuantity = quantity;
    for (item of items) {
        const partyQuantity = $gameParty.numItems(item);
        const removeQuantity = Math.min(partyQuantity, remainingQuantity);
        $gameParty.loseItem(item, removeQuantity);
        remainingQuantity -= removeQuantity;
        if (remainingQuantity === 0) break;
    }
};

Example:
1742346276859.png
Here's the code that can do what you want with script calls; note that case matters (e.g., 'potion' is not the same as 'Potion') and everything that matches will be checked (e.g., 'This is not a Potion' will also be included as a 'Potion', and weapons/armor that have 'Potion' in their names are included).

Some of these can be easily changed - we can just convert everything to lowercase so that the case doesn't matter, we can only check items where the name starts with that string, we can only check items, we can automatically remove the items in the conditional branch so that we don't have to first check the quantities then remove them, etc.

Also, this plugin is based on the default RMMZ way of sorting the inventory by id so it assumes you're not using any other plugin that sorts things differently.

JavaScript:
/*:
 * @target MZ
 * @plugindesc Check for items matching a certain name and remove them, starting from the lowest id.
 * @author SeaPhoenix
 *
 */

function checkItemQuantity(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    const partyQuantity = items.reduce((sum, item) => sum + $gameParty.numItems(item), 0);
    return partyQuantity >= quantity;
};

function removeMatchingItems(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    let remainingQuantity = quantity;
    for (item of items) {
        const partyQuantity = $gameParty.numItems(item);
        const removeQuantity = Math.min(partyQuantity, remainingQuantity);
        $gameParty.loseItem(item, removeQuantity);
        remainingQuantity -= removeQuantity;
        if (remainingQuantity === 0) break;
    }
};

Example:
View attachment 334924

Wow, this is amazing! This works much better than anything I could have come up with.

One question though: Would it be possible to somehow check only for an exact name match, instead of partial? This way, I could check for "Mushroom" without getting matches for "Mushroom Soup", etc.
For an exact name match, just change item.name.includes(name) to item.name === name in both functions.
For an exact name match, just change item.name.includes(name) to item.name === name in both functions.

Amazing, thank you!
Or you can use notetags, like <category:corn> and check item.meta.category=='corn', so you can use different names without worry about it. Like good corn, bad corn, rotten corn, maize, etc.
Or you can use notetags, like <category:corn> and check item.meta.category=='corn', so you can use different names without worry about it. Like good corn, bad corn, rotten corn, maize, etc.

Wow, that seems very useful as well! All of the items I'm using for this already have notetags to denote their generic type to work with the CGMZ Crafting plugin, so it I wouldn't even need to set anything up.
Here's the code that can do what you want with script calls; note that case matters (e.g., 'potion' is not the same as 'Potion') and everything that matches will be checked (e.g., 'This is not a Potion' will also be included as a 'Potion', and weapons/armor that have 'Potion' in their names are included).

Some of these can be easily changed - we can just convert everything to lowercase so that the case doesn't matter, we can only check items where the name starts with that string, we can only check items, we can automatically remove the items in the conditional branch so that we don't have to first check the quantities then remove them, etc.

Also, this plugin is based on the default RMMZ way of sorting the inventory by id so it assumes you're not using any other plugin that sorts things differently.

JavaScript:
/*:
 * @target MZ
 * @plugindesc Check for items matching a certain name and remove them, starting from the lowest id.
 * @author SeaPhoenix
 *
 */

function checkItemQuantity(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    const partyQuantity = items.reduce((sum, item) => sum + $gameParty.numItems(item), 0);
    return partyQuantity >= quantity;
};

function removeMatchingItems(name, quantity) {
    const items = $gameParty.allItems().filter(item => item.name.includes(name));
    let remainingQuantity = quantity;
    for (item of items) {
        const partyQuantity = $gameParty.numItems(item);
        const removeQuantity = Math.min(partyQuantity, remainingQuantity);
        $gameParty.loseItem(item, removeQuantity);
        remainingQuantity -= removeQuantity;
        if (remainingQuantity === 0) break;
    }
};

Example:
View attachment 334924
how can I modify this to only match exact item names (not partial matches) and make it case-insensitive at the same time?
how can I modify this to only match exact item names (not partial matches) and make it case-insensitive at the same time?
JavaScript:
// Check item name lower case against parameter name lowercase
const items = $gameParty.allItems().filter(item => item.name.toLowerCase() === name);
This allows the item name to be lowercase (you could also force the parameter name to be lowercase by setting it to be === name.toLowerCase() but this is probably not needed since you are entering the parameters).
JavaScript:
// Check item name lower case against parameter name lowercase
const items = $gameParty.allItems().filter(item => item.name.toLowerCase() === name);
This allows the item name to be lowercase (you could also force the parameter name to be lowercase by setting it to be === name.toLowerCase() but this is probably not needed since you are entering the parameters).
Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/process-to-check-and-take-a-number-of-items-with-different-ids.176349/