Random Loot Boxes

Do you want your powerful bosses or event enemies to give your player a little something extra?
Maybe a little item that will distribute a random reward to the player upon opening from their inventory?
Then uh, this might be the guide for you.

In many MMORPG's, many bosses drop boxes that contain random gear and items. In RPGMaker (I've only managed to do this in MV, I don't know if this works as well in the other engines), it is very possible to replicate this effect using Common Events.

First, enter your database.
Spoiler
KpU4ACL.png

Next, go into your Items tab and create the item you wish to give the random reward. For the sake of this tutorial, I shall call mine Reward Box, though it can be called whatever you like.
Spoiler
nzRECBx.png

Next, set the item type and etc to how you see fit. You likely don't want this item to be used in battle, so I recommend setting it to Menu only. If you want the player to have the ability to sell the box, give it a price. Set the scope to none if you wouldn't like to play an animation when they open your box.

Spoiler
8vhxtLM.png

Next, we will need to make a Common Event for the box to use when the player opens it. Locate the Common Event tab in your Database and make a new event.

Spoiler
GKg1J6c.png

Now for the fun part. We are going to assign a variable to our box. Click the empty common event field as if you were making any normal event, and select "Control Variables." Next make a new variable. For the sake of maintaining organization, I like to name my variables after their trigger. So I will name mine Reward Box.
Spoiler: Selecting Control Variables
p1tHmKy.png
6fa0af4e-9f61-457a-8bbd-8fe46a15ae99
Spoiler: Naming the Variable
fzRi70e.png

Once you have your variable made and selected, click okay. Next, go under Operation and make sure the operation is on "set."
Then go to Operand and select random. This will be what generates the item given randomly. Lets say we want to give out 4 items, since in cpu language 0 is still counted as a number, we would select 0-3 if we wish to distribute 4 items. Alternatively you could select 1-4 but for the sake of this tutorial, we will use 0-3.

Spoiler
3B3GnRE.png

Click "Ok" and return to the event page. On the next line, add a Conditional Branch. Now set the condition for the branch to be your variable. Be sure that "Variable = x" where X represents what item will correspond to the number stored in the variable. For the very first one, we will select 0 as this will be the first item distributed. Copy and paste this branch adding 1 to the constant each time until you have 0-3 as shown below.
Spoiler
HCGWDdf.png
Spoiler: When the Conditional Branches are set up properly:
5uedcM4.png

Now for the final part of the common event. In each If: branch, add some text saying something like "You've obtained (Item name)!" and then have it give the player the item!
Spoiler
viXM18t.png

Next, we need to go back to our item in the Items tab. Select "Effects", other and Common Event. Select the common event we just made.
Spoiler
frd7Q1b.png
Next, just make the enemy drop the box and you're done 🙂
Spoiler
2x4B9J8.png
This is a great tutorial! If you don't mind, I'd like to point out that you can achieve the same effect using a script command in the common event and it's much easier to add/remove items from it:

Code:
var items = [1, 2, 3, 0];
var index = Math.randomInt(items.length);
if (items[index] > 0) {
  var reward = $dataItems[items[index]];
  $gameMessage.add("Obtained " + reward.name + "!");
  $gameParty.gainItem(reward, 1);
} else {
  $gameMessage.add("You get nothing, you lose, GOOD DAY SIR!");
}

Just populate the initial array with the ID numbers of all the possible items the box can give. You can have a couple or hundreds but you only have to change that one line rather than do conditional branches for every item.

If this goes beyond the scope of your tutorial or you weren't looking for viable alternative methods, please let me know and I'll delete my post.
While that is an easier method, I was going to add that into an intermediate version as this was for those who didn't want to familiarize themselves with coding just yet as they were still learning the basics of the engine and how it works. Don't delete that though, it looks elegant as hell XD
Code:
var items = [1, 2, 3, 0];
var index = Math.randomInt(items.length);
if (items[index] > 0) {
  var reward = $dataItems[items[index]];
  $gameMessage.add("Obtained " + reward.name + "!");
  $gameParty.gainItem(reward, 1);
} else {
  $gameMessage.add("You get nothing, you lose, GOOD DAY SIR!");
}

This is elegant and awesome! I've noticed you have supplied great code solutions for a number of posts, are you available for hire? You are a fine coder. 🙂
I do commissions now and again, @SimProse. I work a 9-5 as a .NET Developer so I don't have the time to develop scripts and plugins as a career like some of the better known community members do, but depending on the job and the pay I can generally work something out.

I'm not sure if you've played or are aware of Profit Motive, but I coded a few of the systems in that, like the stock market and outfit changing.

If you'd like to discuss any hiring requests with me, please do so via PM so we don't hijack poor Damian's thread. 😛
This is a great tutorial! If you don't mind, I'd like to point out that you can achieve the same effect using a script command in the common event and it's much easier to add/remove items from it:

Code:
var items = [1, 2, 3, 0];
var index = Math.randomInt(items.length);
if (items[index] > 0) {
  var reward = $dataItems[items[index]];
  $gameMessage.add("Obtained " + reward.name + "!");
  $gameParty.gainItem(reward, 1);
} else {
  $gameMessage.add("You get nothing, you lose, GOOD DAY SIR!");
}

Just populate the initial array with the ID numbers of all the possible items the box can give. You can have a couple or hundreds but you only have to change that one line rather than do conditional branches for every item.

If this goes beyond the scope of your tutorial or you weren't looking for viable alternative methods, please let me know and I'll delete my post.
How do i set percentage rates into this?
I mean, i want one item have 50% cha ce to drop, another one 10%, and go on.
It's a really great tutorial. The problem is, the player can save the game and then open the loot box over and over again until he gets what he wants.
Sure the player can savescum, but I wonder why you'd want to prevent someone from cheating in a single player game. Let people enjoy your game however they want.
I'm assuming you're making a sp game, because let's face it if you're making a multiplayer game in mv, you're js savvy enough to do this on your own.

If you really feel it's mandatory, the alternative is requesting a plugin that creates a loot box's content when obtained. Not easy to do as it'd necessitate making items independent and not just getting the info from the database template.

Also a shameless plug: I made a random loot plugin a while ago, the link is in my signature.
@DamianCastaway Nice! I saw what you did there. Beautiful reference, Mr. Wonka! 😉

@SolonWise @Astfgl66 Two things about this. 1) The player may not realize it's random right away, so they may just take what they get until they figure it out, and 2) you could occasionally make loot drops that aren't random so that players who try to "savescum" keep loading the same result until they realize the :troll: is back at them. :mrsatan:
Okay, but that doesn't address my point at all.
In a game that's not competitive, why would you take the time to prevent save scumming ? If your players enjoys the game more by being overpowered with save scumming, isn't it just fine?
There's no need to penalize players for not playing the game as you intended, let people enjoy singleplayer games the way they want to.

Your second point is also even more complicated to do than writing a plugin to make items independent and having loot boxes save contents upon being acquired not opened. Unless you're talking about making fake loot boxes as items, I guess you could, but I fail to see the point.

It's just a battle you can't win. People will find a way to cheat if they want to. If you autosave, they'll back up their save outside the directory. If your contents are random and fixed upon being acquired, and a player really wants an item, he'll just reload and keep trying. And then you'll even have the few who will simply open your published game with a cheat program and give themselves the object directly, and you can't do anything about that.
The only solution is to completely block access to the savefile by having your own server online that handles it, but that's its own can of worms.

It'll take you time to do something that will do nothing but prevent enjoyment for some of your playerbase and nothing for the rest. It's not worth it.
You'd better spend that time to add content that will bring enjoyment for everyone.

Let's face it: if you're making a gacha game and you intend to sell loot boxes as microtransaction, the only way to go is to make online profiles and profiles being on your server only, with players never having access to them. If you can do that, then you're not reading a tutorial about random loot here.
I mean if you were really *that* concerned about save scumming, what you could do is add a hashmap for opened treasures which is saved in a file independently of the game saves, then each time a chest is opened add the event ID as the key and the item ID which was randomly generated as the value. Then modify the code slightly so that if the hashmap contains the event ID of the chest being opened, acquire the item with the ID in the map rather than randomly generating one.

You'd need a separate file for each playthrough though, so it's probably more work than it's worth.
Thank you ^^
Really helpful for me , who have no knowledge about java.
I used the above script, and everything seems to work. Until I check my inventory, and the
items i just received are not there.

Everything seems okay, even says I received the item, but when I try to use/equip it, it does not
appear in my inventory.
I created something very similar to this using an almost identical method to what Damian stated above, but I did it in the manner of a huge loot table similar to Diablo's 😛
I always found it super fun to get random things with chances of something awesome.
Cheers to you Damian for this.

Original thread: https://forums.rpgmakerweb.com/threads/random-loot-boxes.75178/