Question: Possible to remove a random item from party's inventory and inform players about it?

Hi,

To expand on my question listed in the title:
1. I want to have a scenario whereby a random item is lost (via theft) from the party inventory. However, as I cannot be sure what items the player will have, I cannot use the "change item" in the Event Commands.
2. I have managed to locate this thread whereby they suggested using a Script to reach the effect (of removing a random item from existing inventory).

var items = $gameParty.allItems();
var count = items.length;
if (count) $gameParty.loseItem(items[Math.randomInt(count)], 1);
I.e. "If the party has any Items, Weapons, and/or Armors in their inventory, pick one randomly and remove it."

However, I believe in this manner, I will not be able to write in the text box something along the lines of "We lost item X" because this Script does not check and record what item is lost.
3. Therefore, I am seeking help on the forums here to know if there is a way to:
=> Remove a random item from the party's existing inventory, &
=> Able to know which item was removed in the previous step

Thank you in advance!
You could try this (untested):
JavaScript:
var items = $gameParty.allItems();
var count = items.length;
// If the party has items in inventory
if (count) {
  // Get a random inventory item
  var item = items[Math.randomInt(count)];
  // Lose 1 of that item
  $gameParty.loseItem(item, 1);
  // Store item ID in variable 123
  $gameVariables.setValue(123, item.id);
  // Show message using that item's icon & name
  $gameMessage.add("We lost \\i[%1] %2!".format(item.iconIndex, item.name));
}
This stores the item ID in a variable (e.g. for reference in following commands in your event) and it also shows a message. I'm guessing you'll only want one of those. Feel free to remove/change any parts as you like~

Alternatively there are plugins that can automatically show non-interrupting messages when gaining/losing items & gold. E.g. TinyGetInfoWnd.js, which ships with RMMV - in the Steam version it can be found in the install folder, under dlc/KadokawaPlugins, standalone might be organised differently.
Hi, this is luckily pretty straightforward to do since you can check which item you got out of the inventory using the id.

Ignore this part, it works but is not needed.
The annoying part here is that allItems() returns _all_ item types so we need to know what kind of item it is before checking its name. Here is a helper function to do that:
GetItemName = function(item) { let data; if (DataManager.isItem(item)) { data = $dataItems; } else if (DataManager.isWeapon(item)) { data = $dataWeapons; } else if (DataManager.isArmor(item)) { data = $dataArmors; } else { return "" } return data[item.id].name; };

I personally keep a plugin with all of my helper functions, but if you dont want to do that you can just use this inline in your script call.


Once you have the name you can store it in variables so that it can be used in a text box.

Here's an example script block using the function inline and with a variable 50 which lets assume I've named it Lost Item Name or something.

let GetItemName = function(item) { let data; if (DataManager.isItem(item)) { data = $dataItems; } else if (DataManager.isWeapon(item)) { data = $dataWeapons; } else if (DataManager.isArmor(item)) { data = $dataArmors; } else { return "" } return data[item.id].name; } let items = $gameParty.allItems(); let item = items[Math.randomInt(items.length)]; $gameVariables.setValue(50, GetItemName(item)); $gameParty.loseItem(item, 1);

Then your common event will look something like this (I recommend checking that the party has items and handling with different text):
1678725766298.png


Hope this helps!
Haha wait I completely forgot that normally the item container returned by $gameParty.allItems() already contains the name data
So you can just get rid of that helper function and your whole script block is
Code:
let items = $gameParty.allItems();
let item = items[Math.randomInt(items.length)];
$gameVariables.setValue(50, item.name);
$gameParty.loseItem(item, 1);
Oops
This was my adaptation:
Spoiler
JavaScript:
var items = $gameParty.allItems();
var count = items.length;
if (count) {
  var randomItem = items[Math.randomInt(count)];
  var numLost = 1;
  $gameParty.loseItem(randomItem, numLost);
  $gameMessage.add(
    `You lost \\I[${randomItem.iconIndex}] ${randomItem.name} x ${numLost}!`
  );
}


My solution doesn't save the item to a game variable though, which is definitely a good idea for eventing purposes. It does however give the message the ability to handle more than one item being removed, if that was ever the case.
You could try this (untested):
JavaScript:
var items = $gameParty.allItems();
var count = items.length;
// If the party has items in inventory
if (count) {
  // Get a random inventory item
  var item = items[Math.randomInt(count)];
  // Lose 1 of that item
  $gameParty.loseItem(item, 1);
  // Store item ID in variable 123
  $gameVariables.setValue(123, item.id);
  // Show message using that item's icon & name
  $gameMessage.add("We lost \\i[%1] %2!".format(item.iconIndex, item.name));
}
This stores the item ID in a variable (e.g. for reference in following commands in your event) and it also shows a message. I'm guessing you'll only want one of those. Feel free to remove/change any parts as you like~

Alternatively there are plugins that can automatically show non-interrupting messages when gaining/losing items & gold. E.g. TinyGetInfoWnd.js, which ships with RMMV - in the Steam version it can be found in the install folder, under dlc/KadokawaPlugins, standalone might be organised differently.
Thank you for this suggestion! I will keep in mind the plugin that you suggested and look into it.
Haha wait I completely forgot that normally the item container returned by $gameParty.allItems() already contains the name data
So you can just get rid of that helper function and your whole script block is
Code:
let items = $gameParty.allItems();
let item = items[Math.randomInt(items.length)];
$gameVariables.setValue(50, item.name);
$gameParty.loseItem(item, 1);
Oops
Thank you for providing such a short and concise script! (I don't know about others, but for me who have zero knowledge of Javascript and this being my first actual script call in the game, I feel that this is written very clearly.)

Just to clarify if I understood this script correctly,

//Find out what is in the party's inventory
let items = $gameParty.allItems();

//Determine the item that is going to be lost from the inventory
let item = items[Math.randomInt(items.length)];

//Store the item's name in a variable (in this case, it's Variable 50)
$gameVariables.setValue(50, item.name);

//Party loses the item, 1 of them.
$gameParty.loseItem(item, 1);

If I am right with this logic, technically I can use a similar formula for anything else I may want to do in the future, aka:

1. let (insert term here) = (whatever I want to set the term to be, as long as I can find the corresponding Javascript formula)

2. $gameVariables.setValue(X, Y), whereby X is the variable number it is stored, and Y is whatever I want to store (number, string of text like item name, and......maybe other things I've yet to explore?)

So for an instance, I want to make the weird scenario of a person robbing the party of an item at first because he urgently needs it, then returning back to compensate the party with gold relative to the item he robbed (75%~150% of the item's price). I can write a script like this:

let items = $gameParty.allItems();
let item = items[Math.randomInt(items.length)];
$gameVariables.setValue(50, item.name);
$gameVariables.setValue(51, item.price);
$gameParty.loseItem(item, 1);

Text: Oh no, that guy just robbed us of \v[50]! We spent \v[51] gold to buy it!

[Game progresses]
let compensate = Math.round(($gameVariables.value(51) * (75 + Math.randomInt(76)))/100)
$gameVariables.setValue(52, compensate)

Text: Let me compensate you now with \v[52] gold for the \v[50] I took from you earlier.

[I tested this part out in my game already and it works, but if you'd be kind enough to confirm how I think it works out, I'd greatly appreciate it!
=> item.price checks for the price of the item registered in the engine's database.
=> Math.randomInt(x) churns out a random number from 0 to x-1, so if I want my value to be 75~150, then I will have to add a 75 in front so the minimum value is 75, and then get a random value between 0-75 (which makes x = 76) to add on to the minimum value.
=> Multiply the value of the lost item by 75~150, then divide the value by 100 to get the actual value in applying percentages. *Because I didn't manage to find a way to multiply random percentages.*
=> Math.round rounds the value in the previous step to the nearest integer if there are any decimal points in the value
=> Set the term "compensate" to store the value of the previous step. *I'm not sure if this step is necessary, but when I tried plugging in the Math.round straight into Control Variable, setting Variable 52 via that exact script, it didn't work out.*
=> Set Variable 52 to what is stored in the term "compensate"

Did I understand how this script that you have kindly provided me with correctly?

Thank you for your time!
This was my adaptation:
Spoiler
JavaScript:
var items = $gameParty.allItems();
var count = items.length;
if (count) {
  var randomItem = items[Math.randomInt(count)];
  var numLost = 1;
  $gameParty.loseItem(randomItem, numLost);
  $gameMessage.add(
    `You lost \\I[${randomItem.iconIndex}] ${randomItem.name} x ${numLost}!`
  );
}


My solution doesn't save the item to a game variable though, which is definitely a good idea for eventing purposes. It does however give the message the ability to handle more than one item being removed, if that was ever the case.
I really appreciate this adaptation that I can return to at a further date to learn more about Javascript, especially if I want them to remove more than one item (which I think is highly likely). Thank you!
Did I understand how this script that you have kindly provided me with correctly?!
Yes your comments on my code are exactly correct, it seems like you're well on your way to having a strong grasp of js!

One small thing with your second example:
Code:
Math.round(($gameVariables.value(51) * (75 + Math.randomInt(76)))/100)

This can be more succinctly represented through use of the Math.random function which gives a decimal number between 0 and 1.

To multiply a number by a random value between 75% and 150% it can be written

Code:
Math.round($gameVariables.value(51) * (Math.random() * 0.75 + 0.75))

Which can also be simplified to
Code:
Math.round($gameVariables.value(51) * 0.75 * (Math.random() + 1))

Of course it's just a matter of what's easier for you to read and edit in the future.
Also a good resource for examples for script calls is this community maintained spreadsheet:

It also has links to additional learning resources for general javascript and lots of rpgmaker specifics.
Hopefully that's helpful, but feel free to ask any other questions
Thank you! That's all for now :3
Yes your comments on my code are exactly correct, it seems like you're well on your way to having a strong grasp of js!

One small thing with your second example:
Code:
Math.round(($gameVariables.value(51) * (75 + Math.randomInt(76)))/100)

This can be more succinctly represented through use of the Math.random function which gives a decimal number between 0 and 1.

To multiply a number by a random value between 75% and 150% it can be written

Code:
Math.round($gameVariables.value(51) * (Math.random() * 0.75 + 0.75))

Which can also be simplified to
Code:
Math.round($gameVariables.value(51) * 0.75 * (Math.random() + 1))

Of course it's just a matter of what's easier for you to read and edit in the future.

Original thread: https://forums.rpgmakerweb.com/threads/question-possible-to-remove-a-random-item-from-partys-inventory-and-inform-players-about-it.155776/