How to randomly remove an item from a player's inventory in a common event?!

ThePyroMan8

Warper
Member
Joined
Sep 20, 2021
Messages
3
Reaction score
1
First Language
English
Primarily Uses
RMMV
Okay so I'm trying to make a skill that ejects a random item from the player's inventory.
I'm relatively new to this and I have no idea on how scripting even works so if someone could explain to me a way on doing it that would be great!
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
1,253
Reaction score
727
First Language
Poland
Primarily Uses
RMMV
use this in script call event:
$gameParty.loseItem($dataItems[Math.randomInt($dataItems.length)], 1)
 

ShiningPhoibe

Veteran
Veteran
Joined
May 12, 2020
Messages
131
Reaction score
44
First Language
French
Primarily Uses
RMMZ
Event command > Party (upper right corner) > change items. You might need to look up tutorials before you tackle making a game though.
 

ThePyroMan8

Warper
Member
Joined
Sep 20, 2021
Messages
3
Reaction score
1
First Language
English
Primarily Uses
RMMV
use this in script call event:
$gameParty.loseItem($dataItems[Math.randomInt($dataItems.length)], 1)
where do i type it though
sorry for asking basic questions btw
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
33,993
Reaction score
8,625
First Language
German
Primarily Uses
RMMV
like he said in script call event command
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,861
Reaction score
2,196
First Language
EN
Primarily Uses
RMMZ
I'd suggest this instead (e.g. in a Script command):
JavaScript:
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".
 

ThePyroMan8

Warper
Member
Joined
Sep 20, 2021
Messages
3
Reaction score
1
First Language
English
Primarily Uses
RMMV
Thank you so much for everyone's help, it works!!!
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
43,758
Reaction score
15,316
First Language
English
Primarily Uses
RMMV
$gameParty.loseItem($dataItems[Math.randomInt($dataItems.length)], 1)
Could that end up trying to lose $dataItems[0]? Does it need to be +1?
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
1,253
Reaction score
727
First Language
Poland
Primarily Uses
RMMV
@Shaz In this form, it might, but Math.randomInt() has a nice trick to avoid it:

Math.randomInt(size, min, max)
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,861
Reaction score
2,196
First Language
EN
Primarily Uses
RMMZ
Math.randomInt(size, min, max)
This is not something that is defined in the core scripts. Instead you can use:
  • min + Math.randomInt(1 + max - min)
This should return a random integer in the range [min, max].
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,283
Reaction score
1,234
First Language
English
Primarily Uses
RMMV
use this in script call event:
$gameParty.loseItem($dataItems[Math.randomInt($dataItems.length)], 1)
The other problem with this method is that you're selecting a random item from the database without any reason to believe the party has one to lose. Unless you're late in the game and a hoarder, there's every chance that this will get called and simply do nothing. Caethyril's method of using the $gameParty containers assures that something will be lost when the function is called.

Math.randomInt(size, min, max)
What is "size" supposed to represent in that? In your own example, you call it with the supposed min and max being 2-20, and you get a result of 157. So...how is it "working quite well"? :stickytongue:

You can see the code in rpg_core.js, and it only takes one argument. In JavaScript you can overload a function call with additional arguments and not get an error, but neither does it do anything with them.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,283
Reaction score
1,234
First Language
English
Primarily Uses
RMMV
RPG Maker MV doesn't use that library. Notice how all of its references and examples are written with "math," lower case? If you type anything from that page into your MV console, you'll get an error.

Heck, if it did work according to that page, the examples you typed into your console would not have shown those results, because you were asking for a 255-element-long array every time!

In MV, you have access to the Math object that's built into JavaScript, which provides some functions like random and floor; but the randomInt function was written by the MV programmers, it takes one argument, and you can see the code exactly where I told you it was.
 

Latest Threads

Latest Profile Posts

unintentionally made a track that sounds straight out of Yume Nikki
Revisiting one of my older projects in a long time to work some more on the mechanics. I've been working on- and off-again for about a year now on recreating the Force and Mystic systems from Wild ARMs. And I think I've finally made some good breakthroughs on that!

Still, it is not perfect and I may post my thoughts at a later time to see if there are any other suggestions or alternatives I've missed.
actor name string, why you gotta be case sensitive like that?
Doing RPG Maker News for 26th October 2021

My entire world, full of monsters and treasures, will likely be done by the end of the year. After that it's all NPCs and side quests... then done. I'm never making an open world game again xD

Forum statistics

Threads
116,106
Messages
1,095,848
Members
151,505
Latest member
rensky
Top