(Solved)Random item common event.

So, this is something I thought would be easy, but ran into some problems with eventing.
I want to make a common event, that gives the player a random item within a range. (In this case between items 8-17)
Thought this would be easy enough, set variable X to a random number between 8-17, give player that item.
Well, first problem I'm having is I want to actually tell the player what item they received. But from what I can tell
the default Show Message doesn't have a slash command to display an item name through variables.
Second problem is the "Change Item" event doesn't have the option to give an item as a variable. Only to give an
amount of a specific item as a variable. Anyone know a way around this?
Your Variable X set to random 8..17
Conditional Branch VariableX equal 8
Add item XYZ , Show Message XYZ obtained
end
Conditional Branch VariableX equal 9
Add item ABC , Show Message ABC obtained
end
and so forth...
If you don't mind to use plugins, Yanfly's message core adds a slash that displays items names by their id's, might be helpfull, plus Bex's suggestion of conditional branches.
To add an item for which you stored the id into your variable X, you can use the following script line :
Code:
$gameParty.gainItem($dataItems[$gameVariables.value(X)], 1)
(don't forget to replace X by your actual variable number)
Add a script call to your common event, then paste this into it:

Code:
var a = {"min":8,"max":17};
var i = Math.floor(Math.random() * (1 + a.max - a.min)) + a.min;
var m = "Obtained %i!";
$gameParty.gainItem($dataItems[i], 1);
$gameMessage.add(m.replace(/%i/gi, $dataItems[i].name));

This randomly picks an item between 8 and 17 (including 8 and 17), gives it to the player, and displays its name.

Edit: You can customize the message (var m) to whatever you want, with %i representing the name of the item. So, "You found a %i inside the gift box!" or whatever.
Code:
var a = {"min":8,"max":17};
var i = Math.floor(Math.random() * (1 + a.max - a.min)) + a.min;
var m = "Obtained %i!";
$gameParty.gainItem($dataItems[i], 1);
$gameMessage.add(m.replace(/%i/gi, $dataItems[i].name));
If you do it that way, you need to add
Code:
this.setWaitMode('message');
at the end, or the game won't wait for the player's input to proceed to the next instructions.
Oh right, thanks. I just sort of threw that together on the fly. 🙂
That worked, thanks for the help!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-random-item-common-event.106097/