Use a random item from the player's inventory... possible?

Is the following possible...?

I want to have an event pick a *random item* from the player's inventory, use it (triggering it's effect as normal, even if just a common event) and thus remove it (consumable) from the player's inventory.

I can't seem to figure out how to do this, either via plain eventing or scripting. 🙁

Alternately, is it easier to simply create an array/list of items and pick/use/remove from that instead?
I think this thread will help you solve your problem.

edit: this thread discusses giving the player a random item. But using the same concepts and adding conditional branches to check if the item is in your player's inventory will probably allow for you to build on what the thread discusses as far as choosing a random item.
I think this thread will help you solve your problem.

edit: this thread discusses giving the player a random item. But using the same concepts and adding conditional branches to check if the item is in your player's inventory will probably allow for you to build on what the thread discusses as far as choosing a random item.
Thanks for the quick reply! That thread was what I was just reading, but I can't figure out how to use an existing item from the player's inventory that way, just how to add a random item (based on a variable) to said inventory. I can also remove one the same way, but I don't know how to "use" one rather than removing it.

EDIT: I was presuming ".useItem" was what I wanted to trigger, but no idea how it works in script calls in MV!
Okay, maybe you could set a variable to the amount of a particular consumable item like this:

Code:
$game_variables[X] = $game_party.item_number($data_items[Y])

Note: I would try to use the same variable ID number that corresponds to the database number for the items, just to be less confusing.

Then you could check to see if all the variables have a value greater than one like this:

Code:
[variableid, variableid, variableid].some(function(id) { return $gameVariable.value(id) ===  value}){

Example Use:
[121, 122, 123, 124, 125, 126, 127].some(function(id){ return $gameVariables.value(id) === 1})

This means that from Variables 121 - 127, it will check if one or more of them has the value of 1. Check Master script call list.

Now the trick would be use whichever variables have value greater than or equal to 1 as a pool to then randomly choose one variable. This is the part that has me stumped at the moment.

I know you can create a pool of numbers, then pick one of those at random, like this:

Code:
pool = [1, 2, 3, 4];

item = pool[Math.floor(Math.random()*pool.length)];

The issue then is how to get those variables that are only greater than or equal to 1 into the pool.

But you could use 'item = x' and create a bunch of nested conditional branches to then create the effect that you want to happen.

As for the force use of an action/item, that only works in battle. So if you're wanting this to happen on the map then you would simulate what the item does within the event. For example, the item used is a potion that adds 500 HP; you will use the event to increase actors HP by 500, play the animation ID that corresponds to the item on the actor, and finally remove the item from player inventory. Essentially, you're just making it look like the player used the item.
EDIT: I was presuming ".useItem" was what I wanted to trigger, but no idea how it works in script calls in MV!
No, .useItem does not use the item, it only consumes it (if it's marked as consumable). To actually use the item you need to construct a Game_Action, something like this (untested):

JavaScript:
var action = new Game_Action($gameActors.actor(1), true);
action.setItem($dataItems[4]);
action.setTargetActor($gameActors.actor(1));
action.apply();

The general idea of that code is correct, some of the details may be wrong though.
We had that use Item in VX-Ace in combination with key item list selection, than i asked for help that someone can translate it to MV,
that was done in the Forums. I sadly forgot who all exactly participated in doing that, i just dont want that it looks that i did this, because i did not =).

This might not work right away for your case, but it maybe contains all the needed Syntax Elements, so a Scripter can maybe continue from here on. The Code is mostly like Chinese to me.

Code:
(function(v,i,t,a,c,u) {
  if (i = $dataItems[+v || 0]) {
    u = $gameParty.members()[0];
    a = new Game_Action(u);
    a.setItemObject(i);
    t = i.scope === 7 ? [u] : a.makeTargets();
    for (n in t) c = c || a.testApply(t[n]);
    if (u.canUse(i) && (i.scope === 0 || c)) {
      u.useItem(i);
      for (n in t) for (var m = a.numRepeats(); m--;) a.apply(t[n]);
      a.applyGlobal();
}}})($gameVariables.value(1));
I sadly forgot who all exactly participated in doing that
👋 Looks like it was mostly Aesica and me:
So now you'd just need to figure out how to get a random item ID from what's currently in the inventory. Maybe Control Variables > Script:
JavaScript:
var o = $gameParty.items(); (o[Math.randomInt(o.length)] || {}).id || 0
This will pick a random item ID from inventory, ignoring its scope/etc. It will also ignore item quantity, e.g. if you have 5 of item #1 and 1 of item #2, items #1 & #2 are still equally likely to be picked. I think you'd need something more complex to account for item quantities. 😴

Original thread: https://forums.rpgmakerweb.com/threads/use-a-random-item-from-the-players-inventory-possible.123254/