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,243
Reaction score
721
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,928
Reaction score
8,607
First Language
German
Primarily Uses
RMMV
like he said in script call event command
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,845
Reaction score
2,180
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,720
Reaction score
15,304
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,243
Reaction score
721
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,845
Reaction score
2,180
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,238
Reaction score
1,201
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,238
Reaction score
1,201
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 Posts

Latest Profile Posts

About the Deer, I had to cut some piece of video due the *swearing* Youtube copyrights issues...
A caveman pokes his head in...
I was listening to songs by Chuck E Cheese's voice actor (Jaret Reddick from Bowling for Soup) when the meds I was taking for my stomach virus apparently stopped working. I can't take my next dose until morning, but it's no use anyway. I'm officially sick again. Of course, I'll definitely take the next dose either way, but it's useless now.
New profile picture! This time, it's representative of my current project.
I suddenly feel like making a game for little kids: a pointless sandbox of cute animals, silly collectibles, and random mini games.

Forum statistics

Threads
116,002
Messages
1,094,770
Members
151,302
Latest member
jabajav01
Top