Tutorial-Blog Controlling RNG in Treasure Chests

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,914
Reaction score
2,969
First Language
EN
Primarily Uses
RMMZ
Nice! For the scripted part, I'd personally consider something like this:
JavaScript:
// Define IDs of eligible drops
const itemIds   = [7, 10, 11, 15];
const weaponIds = [];
const armorIds  = [];
// Get corresponding items
const drops = itemIds.map(id => $dataItems[id]).concat(
  weaponIds.map(id => $dataWeapons[id]),
  armorIds.map(id => $dataArmors[id])
);
// Count those items in inventory
const count = drops.map(item => $gameParty.numItems(item));
// Get drop rates (high count => low chance)
const maxCount = Math.max(...count);
const rates = count.map(n => 1 + maxCount - n);
// Random selection weighted by rates
const total = rates.reduce((a, c) => a + c, 0);
let r = 1 + Math.randomInt(total);
const index = rates.findIndex(a => (r -= a) <= 0);
const drop  = drops[index];
// Store item's name and icon index for message
$gameVariables.setValue(1, drop.iconIndex);
$gameVariables.setValue(2, drop.name);
// Gain item
$gameParty.gainItem(drop, 1);
[Edit: added +1 to r so it fits the correct range (1 ~ total).]
 
Last edited:

Pasteleptic

Costan's Sin
Veteran
Joined
Oct 4, 2014
Messages
46
Reaction score
69
First Language
English
Primarily Uses
RMMZ
I like the added scripting tutorial a lot, it's a great help!
 

SoftCloud

Aspiring Game Alchemist
Veteran
Joined
Nov 28, 2018
Messages
109
Reaction score
477
First Language
English
Primarily Uses
RMMV
Always need some good pointers are RNG. Thank you~! And @caethyril, thank you! A good script would help a lot. ( - u -~<3
 

Latest Threads

Latest Posts

Latest Profile Posts

DamageEvil_1.png added!
index.php

I have a very strong feeling my internet will be hurricane'd, forcing me to work exclusively on my projects. Wish me luck y'all.
Figured I should probably put my badly written RMMV plugins on GitHub or something, idk.
I've been working on plugins to make it easier to incorporate Persona 3's Once More batsys into MV games. One makes it so you can have states automatically applied to the target and/or user when their hit crits or targets a weakness... I could use testers for it, though. Maybe I'll make a thread in the JS Plugins In Development board about it?
Whenever I'm feeling down I like to watch Bardic Inspiration by Deerstalker Pictures
on youtube. Because 'my mockery is Vicious.' It's funny and catchy like the flu.

Forum statistics

Threads
125,617
Messages
1,172,297
Members
164,723
Latest member
Jobiia
Top