How to use script calls and in-game variables to create a slightly advanced treasure chest containing random items. Great for repeatable dungeons with randomized loot!

I've seen a ton of scripts out there that allow people to create random loot tables for chests or drops from enemies. Those are pretty cool, but the same effect can be achieved via a Script Call, and two in-game variables. Granted, the scripts dedicated to this probably have more options, but if you want a simple chest that grants a random item based on ID ranges that you specify (and they don't have to be sequential the way using a variable would force!), then take a look here.

Before I get started, special thanks goes out to KilloZapIt, who helped to educate me (for the millionth time) on how to script this properly!

So, the first thing you want to do is set aside two in-game variables. Take note of their IDs and give them names, let's say "Random Treasure ID" and "Random Treasure Amount," for simplicity's sake.

Create an event and place the following script call inside.

random_item_ids = [1..22, 24..100]

random_item_amt = [1..3]
collect_ids = random_item_ids.collect_concat {|n| n.is_a?(Enumerable) ? n.to_a : [n]}
collect_amt = random_item_amt.collect_concat {|d| d.is_a?(Enumerable) ? d.to_a : [d]}
random_item_id = collect_ids.sample
random_amt = collect_amt.sample
$game_party.gain_item($data_items[random_item_id], random_amt)
$game_variables[268] = $data_items[random_item_id].name
$game_variables[269] = random_amt

Now, allow me to explain what the code does. The first variable contains an array of every item ID you want the chest to be able to contain. You can set specific IDs, separated by commas, or use ranges like '2..100' which would include every item ID from 2 through 100. There is no limit to the amount of IDs you can include. Just to be safe, do not include IDs that do not exist.

The next variable handles the amount of the item the player will receive. It works the same way as the IDs, and can contain simply '1' to award only one item, or a range like '1..10' to award anywhere from 1 to 10 of the item in the chest.

You won't have to touch anything else except the last two lines, where you'd need to put in the IDs of the in-game variables you've set aside. The interesting thing about in-game variables is that we are led to believe they can only be set to a numerical value, but that isn't true. In-game variables can be set to a string, array, boolean, or a key!

That is where this line comes into play!

$game_variables[268] = $data_items[random_item_id].name

What this line is doing is taking an in-game variable, and storing the name of the randomly chosen item as its value. This will be useful in displaying the name of the item you've received in the next part of this tutorial.

Create a "Show Message" event underneath the script call (you can put a fancy sound effect beforehand if that suits you) and use something like what is shown below. Be sure to use your in-game variable IDs in the message, using the built-in variable display control code, "\v."

Now, when you activate this event, not only will it award a random reward defined by you, in a random amount defined by you, but you will not have to resort to crazy workarounds to display the item you've gotten.

This script call is in use in my own project, in a randomly generated dungeon (Via Infinito) that can be visited repeatedly with resetting random chests each time. :D

P.S. This can work just as easily for weapons or armor, too! Just replace the parts of the code above referencing $data_items to $data_weapons or $data_armors!