MZ Consuming Item based on inventory location

I hope I can explain myself here:

I'm building an entire inventory system from scratch, and so far, things have gone swimmingly. However, all previous script calls for I've used specifically using the item based on their entry in the inventory (IE $gameParty.items()[1] being the second item I've picked up, $gameParty.items()[2] being the third, etc) have resulted in either the item just being erased from the inventory, or simply not working.

Looking for an answer, I did come across the fact that there isn't a script call for this, so now I'm stumped.

Is there any work around for this? And if push comes to shove, are there any plugins that come with a command that make this possible?

Thank you for your time.
You will have to look up array functions to do what you want. $gameParty.items() is just an array.
You will have to look up array functions to do what you want. $gameParty.items() is just an array.
Is there any documentation on array functions you'd recommend? (Thank you for the reply, by the by.)
I just Google javascript array functions/methods and go from there.
You basically want items to be sorted by date/time of acquisition, correct? I imagine there's multiple things involved: first add some kind of property to the item upon acquisition which you can use to order the items in order of acquisition (a time stamp seems most straightforward personally). Secondly you modify the array functions used to list the items. And thirdly you'll also need to be able to save the newly added item property (at least I don't think it won't do that automatically).

Judging from your post, you have already completed the first step? Perhaps it would help to show us what you have done so far.
You basically want items to be sorted by date/time of acquisition, correct? I imagine there's multiple things involved: first add some kind of property to the item upon acquisition which you can use to order the items in order of acquisition (a time stamp seems most straightforward personally). Secondly you modify the array functions used to list the items. And thirdly you'll also need to be able to save the newly added item property (at least I don't think it won't do that automatically).

Judging from your post, you have already completed the first step? Perhaps it would help to show us what you have done so far.
Not quite, what I want to do is consume an item in the player's inventory based on the space that it's occupying.

In the same way that if you pick up a Potion with your inventory completely empty, when you ask what $gameParty.items()[0].name is, it should return "Potion", I wanna find a way to consume the item in $gameParty.items()[0].

As in, force the leader of the party to consume the item in question.

I hope I managed to communicate what I'm looking for better.
You may want to read this then
Post in thread 'Script call for using/activating an item' https://forums.rpgmakerweb.com/inde...or-using-activating-an-item.78316/post-731732
You may want to read this then
Post in thread 'Script call for using/activating an item' https://forums.rpgmakerweb.com/inde...or-using-activating-an-item.78316/post-731732
For what I can gather this means I'll have to replicate the functions of each individual item by hand.

It'll be kind of annoying but it seems to be one of those situations without much of an elegant solution.
For what I can gather this means I'll have to replicate the functions of each individual item by hand.
I think it would help if you first explained what exactly you are trying to do. What is the situation in which the first item in your inventory needs to be consumed? Please explain the entire sequence of events you are envisioning.
The OP wants to use items but bypass the item menu. The problem is the use item functions run through a sequence of menu selections
I think it would help if you first explained what exactly you are trying to do. What is the situation in which the first item in your inventory needs to be consumed? Please explain the entire sequence of events you are envisioning.
I've got a custom inventory UI that uses mouse controls:

1747536414692.png


When you click the "Use" button, it should use the item via a Common Event. It's meant to be dynamic, hence why I need it to specifically use the item in $gameParty.items()[n]. Everything around it works just as intended, it's specifically getting the Common Event to do what I need what's giving me some grief.
Yeah...so RPG Maker's advantage over other engines is it has a lot of RPG-specific systems built in so it saves you time when making a (RPG) game that uses its pre-built systems. The more you deviate from the pre-built systems the more (coding) work you have to do thus RPG Maker loses its advantage.

If you want to make a mouse-based inventory the easiest route is to use a plugin like RS_inventory of PKD's map inventory.

Yeah...so RPG Maker's advantage over other engines is it has a lot of RPG-specific systems built in so it saves you time when making a (RPG) game that uses its pre-built systems. The more you deviate from the pre-built systems the more (coding) work you have to do thus RPG Maker loses its advantage.

If you want to make a mouse-based inventory the easiest route is to use a plugin like RS_inventory of PKD's map inventory.


Thank you very much for the recommendations! I'll see what I can do with this.
Not quite, what I want to do is consume an item in the player's inventory based on the space that it's occupying.

In the same way that if you pick up a Potion with your inventory completely empty, when you ask what $gameParty.items()[0].name is, it should return "Potion", I wanna find a way to consume the item in $gameParty.items()[0].

As in, force the leader of the party to consume the item in question.

I hope I managed to communicate what I'm looking for better.
Try this as a plugin:
Spoiler
JavaScript:
function useItem(actorId, targetId  itemId) {
    if (!actorId || !targetId || !itemId) return;

    const actor  = $gameActors.actor(actorId);
    const target = $gameActors.actor(targetId);
    const item   = $dataItems[itemId];
    const action = new Game_Action(actor);

    if (actor.canUse(item)) {
        action.setItemObject(item);
        action.item().effects.forEach(function(effect) {
            action.applyItemEffect(target, effect);
        }, this);
        actor.consumeItem(item);
    }
};
To call the function, use a script call (actor 1, target actor 3, item 2):
JavaScript:
useItem(1, 3, 2);
Expand as needed (add se, other effects, target specfic index such as $gameParty.leader().actorId() or data item based on click, etc.)

Edit: In terms of using items based on when you pick them up, I would build an array of items for when you add a new item to your inventory with the function gainItem() (by aliasing that function and creating a new property that just holds the item id based on pickup).

2nd Edit:
Here is some code to add to the code above that you can test based on the order that you pick the items up during gameplay (but also accounts for if it is an item you already have, it will not remove the array until the item is exhausted):
JavaScript:
function useItemOrder(actorId, targetId) {
    if (!actorId || !targetId) return;

    const items    = $gameParty._pickUp;
    const listItem = $gameParty._items[items[0]];

    if (items.length > 0) {
        useItem(actorId, targetId, listItem === 1 ? items.shift() : items[0]);
    }
};

(($) => {
    const alias = $.initialize;
    $.initialize = function() {
        alias.call(this);
        this._pickUp = [];
    }

    const alias2 = $.gainItem;
    $.gainItem = function(item, amount, includeEquip) {
        alias2.call(this, item, amount, includeEquip);
        if (!this._pickUp.includes(item.id) && amount > 0) {
            this._pickUp.push(item.id);
        }
    }
})(Game_Party.prototype);
Now, you can just do:
JavaScript:
useItemOrder(1, 4);
and it will use the first item yoy picked up for actor 1 (or any actor you specify) to use on target actor 4 and will remove it when it is the last quantity in the array.
Try this as a plugin:
Spoiler
JavaScript:
function useItem(actorId, targetId  itemId) {
    if (!actorId || !targetId || !itemId) return;

    const actor  = $gameActors.actor(actorId);
    const target = $gameActors.actor(targetId);
    const item   = $dataItems[itemId];
    const action = new Game_Action(actor);

    if (actor.canUse(item)) {
        action.setItemObject(item);
        action.item().effects.forEach(function(effect) {
            action.applyItemEffect(target, effect);
        }, this);
        actor.consumeItem(item);
    }
};
To call the function, use a script call (actor 1, target actor 3, item 2):
JavaScript:
useItem(1, 3, 2);
Expand as needed (add se, other effects, target specfic index such as $gameParty.leader().actorId() or data item based on click, etc.)

Edit: In terms of using items based on when you pick them up, I would build an array of items for when you add a new item to your inventory with the function gainItem() (by aliasing that function and creating a new property that just holds the item id based on pickup).

2nd Edit:
Here is some code to add to the code above that you can test based on the order that you pick the items up during gameplay (but also accounts for if it is an item you already have, it will not remove the array until the item is exhausted):
JavaScript:
function useItemOrder(actorId, targetId) {
    if (!actorId || !targetId) return;

    const items    = $gameParty._pickUp;
    const listItem = $gameParty._items[items[0]];

    if (items.length > 0) {
        useItem(actorId, targetId, listItem === 1 ? items.shift() : items[0]);
    }
};

(($) => {
    const alias = $.initialize;
    $.initialize = function() {
        alias.call(this);
        this._pickUp = [];
    }

    const alias2 = $.gainItem;
    $.gainItem = function(item, amount, includeEquip) {
        alias2.call(this, item, amount, includeEquip);
        if (!this._pickUp.includes(item.id) && amount > 0) {
            this._pickUp.push(item.id);
        }
    }
})(Game_Party.prototype);
Now, you can just do:
JavaScript:
useItemOrder(1, 4);
and it will use the first item yoy picked up for actor 1 (or any actor you specify) to use on target actor 4 and will remove it when it is the last quantity in the array.
Holy moly, I'll test it now and see how it fares
Try this as a plugin:
Spoiler
JavaScript:
function useItem(actorId, targetId  itemId) {
    if (!actorId || !targetId || !itemId) return;

    const actor  = $gameActors.actor(actorId);
    const target = $gameActors.actor(targetId);
    const item   = $dataItems[itemId];
    const action = new Game_Action(actor);

    if (actor.canUse(item)) {
        action.setItemObject(item);
        action.item().effects.forEach(function(effect) {
            action.applyItemEffect(target, effect);
        }, this);
        actor.consumeItem(item);
    }
};
To call the function, use a script call (actor 1, target actor 3, item 2):
JavaScript:
useItem(1, 3, 2);
Expand as needed (add se, other effects, target specfic index such as $gameParty.leader().actorId() or data item based on click, etc.)

Edit: In terms of using items based on when you pick them up, I would build an array of items for when you add a new item to your inventory with the function gainItem() (by aliasing that function and creating a new property that just holds the item id based on pickup).

2nd Edit:
Here is some code to add to the code above that you can test based on the order that you pick the items up during gameplay (but also accounts for if it is an item you already have, it will not remove the array until the item is exhausted):
JavaScript:
function useItemOrder(actorId, targetId) {
    if (!actorId || !targetId) return;

    const items    = $gameParty._pickUp;
    const listItem = $gameParty._items[items[0]];

    if (items.length > 0) {
        useItem(actorId, targetId, listItem === 1 ? items.shift() : items[0]);
    }
};

(($) => {
    const alias = $.initialize;
    $.initialize = function() {
        alias.call(this);
        this._pickUp = [];
    }

    const alias2 = $.gainItem;
    $.gainItem = function(item, amount, includeEquip) {
        alias2.call(this, item, amount, includeEquip);
        if (!this._pickUp.includes(item.id) && amount > 0) {
            this._pickUp.push(item.id);
        }
    }
})(Game_Party.prototype);
Now, you can just do:
JavaScript:
useItemOrder(1, 4);
and it will use the first item yoy picked up for actor 1 (or any actor you specify) to use on target actor 4 and will remove it when it is the last quantity in the array.

I'm absolutely doing something wrong here, but when I call the Common Event, it doesn't consume the item.

1747605813632.png
I'm absolutely doing something wrong here, but when I call the Common Event, it doesn't consume the item.

View attachment 340757
Just call the function itself:
JavaScript:
useItemOrder(1, 1);
No need for $gameParty.items()[0].
You just do useItem(x, y, z);
No $gameParty
he gave you a global function
Edit: double ninja lmao
Just call the function itself:
JavaScript:
useItemOrder(1, 1);
No need for $gameParty.items()[0].

Hmm, still nothing

I should note that I've been using the plugin on the second edit, as the first one kept giving me this error.

1747606938359.png


Is there a way to add the functionality of specifying a specific Item to consume in the second one?

By the way, thank you so very much for the help so far, I wasn't expecting someone to go above and beyond.

Original thread: https://forums.rpgmakerweb.com/threads/consuming-item-based-on-inventory-location.177752/