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
718
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,879
Reaction score
8,587
First Language
German
Primarily Uses
RMMV
like he said in script call event command
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,828
Reaction score
2,164
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,676
Reaction score
15,291
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
718
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,828
Reaction score
2,164
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,173
Reaction score
1,164
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,173
Reaction score
1,164
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

I want to learn Javascript to create a custom menu, but I don't really know where to start.
One uncomfortable truth that has withstood the test of time, is how most people will stand by while they see a crime happening, unless it's illegal to stand by. We probably wouldn't have stories about "special heroes" or games about heroes if the "everyday common folk" were largely capable of being heroes. Heroes face dangers that most people are not willing to.
I'm having toooo much fun designing the new MP (Mental Psyche) system. I've only worked it out for enemies so far, but it's fun to stun them, then go after their MP and delete them instead to farm for better item drops.
anyone know of a place I can promote my game? sorta like game awards but for indie devs, like a place where I can drop a trailer and like stir up some hype
Doing RPG Maker News for 17th October 2021

Forum statistics

Threads
115,882
Messages
1,093,696
Members
151,126
Latest member
LAB00M
Top