MV Random Reward

Ok, random rewards are a core part of my game an honestly... i can powerthrough the whole process, will be LONG and tedious, but i can do it! Yet... i would love to spare me some time here, does anyone knows how to do a Random Reward system via script? Or a plug in or something? Like a i said, i can powerthrought this, but... you know 🙂
Hi.
Is this a Plugin request, or do you just want to talk about this type of system?
it depends on what maker ou use, bxace, mv, mz etc,
for MV/MZ you can use a scriptcall?

let items = [1, 2, 3, 4, 5, 6] //item pool

the rest of the code no idea, not out of my head, but you can use an array
with different items, also, 12, 14, 17, 18 etc, than take 1 random item
on from the pool, the rest of this array can be destroyed if not used anymore.

there is also a tutorial how to make a random chest that draws item,
that code is basicly the same 🙂 (let me see if I can find that one)
but it still depends on the maker you use, because this can be different.
because the above tiny snippet wont work in vxace 🙂


here is the code for MV (might work for MZ)

control variables 6 = random 100...300
Script: let loot = $gameVariables.value(6);
$gameparty.gainItem($dataItems[loot], 1);
Show Animation player, x // optional if you want animation
text: you got \ii[\[v6]]

keep in mind that \ii = is a yanfly plugin (messagecore) to create
items and draw items from the variable Number.
it depends on what maker ou use, bxace, mv, mz etc,
for MV/MZ you can use a scriptcall?

let items = [1, 2, 3, 4, 5, 6] //item pool

the rest of the code no idea, not out of my head, but you can use an array
with different items, also, 12, 14, 17, 18 etc, than take 1 random item
on from the pool, the rest of this array can be destroyed if not used anymore.

there is also a tutorial how to make a random chest that draws item,
that code is basicly the same 🙂 (let me see if I can find that one)
but it still depends on the maker you use, because this can be different.
because the above tiny snippet wont work in vxace 🙂

THIS! I am using MV i know i had see that what your are talking about, this will helpme a lot, otherwise.... conditional branch my old friend... 😅
Moved to: Plugin Requests
Without any scripting knowledge, here's what you'd do:
Code:
Control Variable -> Single -> Set -> Random
Conditional Branch -> Variable = Constant
    Change Items -> Increase -> Constant
Else [...]
You can also use a random variable for the item count (such as if you want to randomly award 1-3 potions or similar)

This is likely going to be a huge branch, so if you want to use it in many events, it would be smarter to make it into a Common Event with no trigger, and call that Common Event whenever needed.

With JavaScript, things get much easier (as in less work):
Code:
Control Variable -> Single -> Set -> Random
Script: $gameParty.gainItem($dataItems[$gameVariables.value(123)], $gameVariables.value(124))
This is assuming the item ID is stored in variable #123 and the item count is stored in variable #124. Obviously, you'll have to change the numbers 123 and 124 to whatever your actual variable ID is.

And if you want a one-liner that doesn't require a variable, you can use this script:
Code:
var possibleRewards = [ 1, 2, 5, 8, 9 ];
var minDrop = 1;
var maxDrop = 4;

var reward = possibleRewards[Math.randomInt(possibleRewards.length)];
$gameParty.gainItem(reward, minDrop + Math.randomInt(maxDrop - minDrop + 1));
This will randomly choose from the items with ID 1, 2, 5, 8, or 9, and will give you between 1 and 4 of the item.

You can increase the probability of a specific item dropping by simply increasing how often the item appears in possibleRewards, for example:
Code:
var possibleRewards = [ 1, 1, 2, 5, 8, 9 ];
Will be twice as likely to give you item #1 compared to any other item.

If you also want to control how many of which item the system can award (like 1-5 potions, but only 1 swords), you can do this:
Code:
var possibleRewards = {
    1: { min: 1, max: 3 },
    2: { min: 1, max: 1 },
    9: { min: 3, max: 5 }
};
var possibleItemIDs = Object.keys(possibleRewards);
var reward = possibleItemIDs[Math.randomInt(possibleItemIDs.length)];
var minDrop = possibleRewards[reward].min;
var maxDrop = possibleRewards[reward].max;
$gameParty.gainItem(reward, minDrop + Math.randomInt(maxDrop - minDrop + 1));
Here, you'll randomly get 1-3 of item #1, only 1 of item #2, and 3-5 of item #9.

Note that for this last solution, you cannot add duplicate items to make them more likely to drop. For example:
Code:
var possibleRewards = {
    1: { min: 1, max: 3 },
    1: { min: 1, max: 3 },
    2: { min: 1, max: 1 },
    9: { min: 3, max: 5 }
};
will not behave any differently (each item has the same probability to drop).
some ways I didn't off, I save this as well, because it can come in handy 🙂
Sorry, looks like I made a mistake. You can't simply do $gameParty.gainItem(reward, amount), you have to do $gameParty.gainItem($dataItems[reward], amount) instead.

Also, I forgot to say this in my previous comment:
Even if you use the JavaScript-only solutions, you might still want to work with variables, so that you can inform the player with a Show Text event which item they got and how many of it.

You can set a variable like this:
Code:
$gameVariables.setValue(124, amount)
which will save the content of "amount" into variable #124.

To display those variables:
Code:
Show Text -> You found \v[123] x \v[124]
Assuming you wrote "Potion" in variable #123 and "3" in variable #124, this will create a message window that says: "You found Potion x 3".

Here's an improved (and fixed) version of the final JavaScript solution:
Code:
var possibleRewards = [
    { itemId: 1, min: 1, max: 3 },
    { itemId: 2, min: 1, max: 1 },
    { itemId: 9, min: 3, max: 5 }
];
var reward = possibleRewards[Math.randomInt(possibleRewards.length)];
var item = $dataItems[reward.itemId];
var amount = reward.min + Math.randomInt(reward.max - reward.min + 1);
$gameParty.gainItem(item, amount);
$gameVariables.setValue(123, item.name);
$gameVariables.setValue(124, amount);
This code allows you to duplicate items in the possibleRewards list to increase their probability of dropping, and writes the item's name in variable #123 and the awarded amount in variable #124 so you can write those into a message window.

An example of how to duplicate items:
Code:
var possibleRewards = [
    { itemId: 1, min: 1, max: 3 },
    { itemId: 2, min: 1, max: 1 },
    { itemId: 2, min: 1, max: 1 },
    { itemId: 9, min: 3, max: 5 }
];
This one doubles the probability of getting item #2.

I hope this helps. If there are still bugs in the code (I haven't had the time to test it), just let me know and I'll fix it.
@Nolonar while the part is a random pool, which works nicely,
and take oout a random number (1 from the pool) even if a duplicate
is something like this:

var possibleRewards = [ { itemId: 1, min: 1, max: 3 }, { itemId: 2, min: 1, max: 1 }, { itemId: 2, min: 1, max: 4 }, { itemId: 9, min: 3, max: 5 } ];

is Item ID only from the item list, or how to put weapon/armor to it or
itemID with % chance to get draw, if itemID 1= 10%, and itemID 3 = 30%
nothing is 60%, for example, or does this require a plugin?

but I love this working one, bit more extended than my basic one ;P
is Item ID only from the item list, or how to put weapon/armor to it

In my above code, I took the rewards from the items list. For weapons and armor, you'll need to change the var item = $dataItems[reward.itemId] part to fetch from $dataWeapons or $dataArmors instead. You'd also need to expand the possibleRewards list, so it knows that the reward is a weapon or a piece of armor.

This code should handle items, weapons, and armor alike:
Code:
var items = $dataItems;
var weapons = $dataWeapons;
var armor = $dataArmor;

var possibleRewards = [
    { object: items[1], min: 1, max: 3 },
    { object: items[9], min: 3, max: 5 },
    { object: weapon[2], min: 1, max: 1 },
    { object: armor[1], min: 1, max: 1 }
];

var reward = possibleRewards[Math.randomInt(possibleRewards.length)];
var amount = reward.min + Math.randomInt(reward.max - reward.min + 1);
$gameParty.gainItem(reward.object, amount);

$gameVariables.setValue(123, reward.object.name);
$gameVariables.setValue(124, amount);
In this example, the rewards can be either 1-3 of item #1, 3-5 of item #9, 1 of weapon #2, or 1 of armor #1.

or itemID with % chance to get draw, if itemID 1= 10%, and itemID 3 = 30%
nothing is 60%, for example, or does this require a plugin?

The simplest way to do that using my existing above code, would be to have { object: items[1] } once, { object: items[3] } 3 times and null 6 times, then check for null (you can also replace null with 0, if you like):
Code:
var items = $dataItems;
var weapons = $dataWeapons;
var armor = $dataArmor;

var possibleRewards = [
    { object: items[1], min: 1, max: 1 },
    { object: items[3], min: 1, max: 1 },
    { object: items[3], min: 1, max: 1 },
    { object: items[3], min: 1, max: 1 },
    null,
    null,
    0,    // same as null, but shorter to write
    null,
    null,
    null
];

var reward = possibleRewards[Math.randomInt(possibleRewards.length)];
if (reward) {
    var amount = reward.min + Math.randomInt(reward.max - reward.min + 1);
    $gameParty.gainItem(reward.object, amount);

    $gameVariables.setValue(123, reward.object.name);
    $gameVariables.setValue(124, amount);
} else {
    $gameVariables.setValue(123, "nothing");
    $gameVariables.setValue(124, 0);
}
This way, there are 10 "rewards" to choose from, and you have a 1/10 (10%) chance to get item #1, a 3/10 (30%) chance to get item #3, and a 6/10 (60%) to get nothing.

You'll also have to put your Show Text event command inside a Conditional Branch to check if you got a reward. That way you can show "You found nothing" or "It's empty" if the amount is 0 (in this example stored in variable #124).

If you want possibleRewards to be as short as possible, you can do it like in MMOs:
Code:
var items = $dataItems;
var weapons = $dataWeapons;
var armor = $dataArmor;

var possibleRewards = [
    { object: items[1], p: 0.1 },
    { object: items[3], p: 0.2 },
    { object: items[13], p: 0.0001 }
];

var rewards = possibleRewards.map(function(i) { i.p >= Math.random() });
for (var reward of rewards) {
    $gameParty.gainItem(reward.object, 1);
}
In this case, you have a 10% chance to get item #1, a 20% chance to get item #3, and a 0.01% to get item #13.

You also have a 2% chance to get items #1 and #3, a 0.001% chance to get items #1 and #13, and a 0.002% chance to get items #3 and #13.

Finally, you have a 0.0002% chance to get all 3 items, and a 71.9928% chance to get nothing at all.

If you want to notify the player what items they got, you should use a plugin for that, because the code would otherwise get really long and complex. I believe MV ships with the TinyGetInfoWnd plugin, which should cover that use case.

For items that should always drop, you can set p: 1 (100% drop rate).
for the last part, its probably a bit complicated to understand, but for
the previous wit null or 0 to adjust the 10% or more, looks easier to control,
as well, I think that pool to give, armor, weapon as well when possible, and really sweet.

ty for the huge explaination, which can help me with some pools as well,
to understand them, but I save both 🙂.
how do you make this a plugin and not just a script? I'm making booster packs [10 types of booster packs]. I'd like to make the booster packs in a script rather than event it in the limited lines in the event script call..
Looks like the code seems to be more convenient, i have 100 cards to place in a booster pack that pulls 8 cards, then each card has rarity that can be set..

JavaScript:
var items = $dataItems;
var weapons = $dataWeapons;
var armor = $dataArmor;

var possibleRewards = [
    { object: items[1], p: 0.1 },
    { object: items[3], p: 0.2 },
    { object: items[13], p: 0.0001 }
];

var rewards = possibleRewards.map(function(i) { i.p >= Math.random() });
for (var reward of rewards) {
    $gameParty.gainItem(reward.object, 1);
}
how do you make this a plugin and not just a script? I'm making booster packs [10 types of booster packs]. I'd like to make the booster packs in a script rather than event it in the limited lines in the event script call..

To make this into a plugin, you need to save the script in a .js file (for example, RandomDrop.js).
But there are a number of things to pay attention to when making a plugin.

First of all, you need to put the code into a function, so you can call it whenever needed:
Code:
function gainRandomReward() {
    // code here
}
To actually use it, you'll need to do (in the RPG Maker editor):
Code:
gainRandomReward();

But if you have another plugin that defines a "gainRandomReward" function, you'll have a conflict that causes incompatibility. To avoid that, you can rename the function to something different, or we can create a context for our plugin. The function would only be valid within that context:
Code:
(function() {
   // Plugin code here
})();
But now we can't use the function anymore, because it's in a separate (anonymous) context. Fortunately, we can use Plugin Commands to still have access to the function.

Before we proceed, I'd like to mention that some plugin developers don't use an anonymous context like I do. For example, Yanfly's AutoPassiveStates plugin:
Code:
var Imported = Imported || {};
Imported.YEP_AutoPassiveStates = true;

var Yanfly = Yanfly || {};
Yanfly.APS = Yanfly.APS || {};

Yanfly.APS.myFunction = function() {
    // function code here
}
They do that, so their plugins can be detected by other plugins. This can serve multiple purposes. I personally only use anonymous context, because it's simpler.

Back to the topic of Plugin Commands, how to do it is different depending on whether you use MV or MZ. For MV:
Code:
let Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    if (command === "getRandomDrop") {
        gainRandomReward();
    } else {
        Game_Interpreter_pluginCommand.call(this, command, args)
    }
};
Finally, in the MV editor you'll use a Plugin Command and write getRandomDrop in it.

For MZ, you'll need to work with plugin metadata:
Code:
/*:
* @target MZ
* @plugindesc Drops random rewards.
* @author name_of_author
* @url https://your-website-if-you-have-one.com
*
* @command getRandomDrop
* @text Get Random Drop
* @desc Player gets a random reward from a predefined list of rewards.
*/
With this, RPG Maker MZ knows that your plugin has a "getRandomDrop" Plugin Command, which you'll implement like this:
Code:
PluginManager.registerCommand("RandomDrop", "getRandomDrop", args => {
    gainRandomReward();
});
This is assuming you named the file RandomDrop.js and not something else. If you named it differently, you'll have to change "RandomDrop" to match the filename.

Looks like the code seems to be more convenient, i have 100 cards to place in a booster pack that pulls 8 cards, then each card has rarity that can be set..

For a booster pack system, I wouldn't use the MMO-style drop system, because it's very hard to control. But since you're going to have a lot of cards of different rarities, creating a list with thousands of entries is not going to be fun.

Fortunately, we can use a little trick:
Code:
var possibleRewards = [];
for (var i = 0; i < 10; i++) possibleRewards.push($dataWeapons[5]);
for (var i = 0; i < 30; i++) possibleRewards.push($dataArmor[8]);
for (var i = 0; i < 60; i++) possibleRewards.push($dataItems[14]);
This will give you a pool of 10+30+60=100 rewards, where you have a 10% chance of getting weapon #5, a 30% chance to get armor #8, and a 60% chance to get item #14.

This is a better solution for your use case, because you are guaranteed to get a drop. Since your booster pack contains 8 cards, you wouldn't want the player to have a chance to get any more or less than 8 cards.

So your plugin would look like this:
Code:
/*:
* @target MV MZ
*
* @command getBoosterA
* @text Get Booster A
*
* @command getBoosterB
* @text Get Booster B
*
* @command getBoosterC
* @text Get Booster C
*/

(function() {
    const PLUGIN_NAME = "RandomRewards"; // MZ only. This must match the filename.

    const BOOSTER_SIZE = 8;

    var items = $dataItems;
    var weapons = $dataWeapons;
    var armor = $dataArmor;

    var boosterA = [];
    for (var i = 0; i < 10; i++) boosterA.push(weapons[5]);
    for (var i = 0; i < 30; i++) boosterA.push(armor[8]);
    for (var i = 0; i < 60; i++) boosterA.push(items[14]);

    var boosterB = [];
    for (var i = 0; i < 10; i++) boosterB.push(weapons[3]);
    for (var i = 0; i < 60; i++) boosterB.push(items[42]);

    var boosterC = [];
    for (var i = 0; i < 10; i++) boosterC.push(weapons[13]);
    for (var i = 0; i < 60; i++) boosterC.push(items[25]);

    function awardBooster(booster, rewardCount) {
        for (var i = 0; i < rewardCount; i++) {
            var reward = booster[Math.randomInt(booster.length)];
            $gameParty.gainItem(reward, 1);
        }
    }

    const COMMANDS = {
        getBoosterA: function() { awardBooster(boosterA, BOOSTER_SIZE); },
        getBoosterB: function() { awardBooster(boosterB, BOOSTER_SIZE); },
        getBoosterC: function() { awardBooster(boosterC, BOOSTER_SIZE); }
    };

    // RPG Maker MV code
    let Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function (command, args) {
        if (command in COMMANDS) {
            COMMANDS[command]();
        } else {
            Game_Interpreter_pluginCommand.call(this, command, args)
        }
    };

    // RPG Maker MZ code
    if (PluginManager.registerCommand) {
        for (const command in COMMANDS) {
            PluginManager.registerCommand(PLUGIN_NAME, command, COMMANDS[command]);
        }
    }
})();
This plugin gives you 8 rewards (BOOSTER_SIZE = 8) from one of 3 different booster packs (boosterA, boosterB, and boosterC). Make sure your boosters don't remain empty, the game will crash if you try to fetch rewards from an empty booster.

This plugin should also be compatible to MV and MZ (if I didn't make any mistakes writing this).
Ok.... the part where you dont need programing knowledge to use RPG Maker just went out of the windon 😅

Anyways, thanks guys! I´ll dig into this and try to use whatever i can 🙏
if you understand my very basic one and the extended version Nolonar
explained every single code of the script, you should probably come out
how to create them 🙂

if you dont nkow any JS, I can understand it would be harder to know what each does,
even with explaination given.

but if you follow it nicely, I'm sure you can get it to work 🙂

Original thread: https://forums.rpgmakerweb.com/threads/random-reward.135879/