MV Script to use dingk's Loot Tables for Shops

I am a big fan of dingk's loot tables for enemy drops, it works perfectly. It can be found here: https://forums.rpgmakerweb.com/index.php?threads/loot-tables-v1-0-1-2021-07-05.120860/

I am wondering if there is a way to populate a shop using the loot tables defined in dingk's plugin? If I could do the exact same thing the script is doing at the end of a battle but instead apply it to populating a shop, it would be absolutely perfect for my game.

Thanks in advance!
Hmmm, try to put the below as plugin and then in the shop event make a script box with prepareLootShop(X);where X is the enemyId you want to reference. It seemed to work when I put an enemy with 1 random drop out of 3, but I didn't really check the plugin in depth.

function prepareLootShop(enemyId) {

let poolsToDrop = [];
let items = [];

for (let table of $dataEnemies[enemyId].dropTables) {
if (table.rate < Math.random()) continue;
let pool = dingk.Loot.getDropCategory(table);
if (pool) poolsToDrop.push(pool);
}

if (poolsToDrop.length) {
items = dingk.Loot.getItemsFromPool(poolsToDrop);
}

let shopItems = [];
let itemType = 0;
items.forEach(item => {
if (item.atypeId) {itemType = 2; shopItems.push([itemType, item.id, 0])}
if (item.wtypeId) {itemType = 1; shopItems.push([itemType, item.id, 0])}
if (item.itypeId) {itemType = 0; shopItems.push([itemType, item.id, 0])}
})

if (shopItems.length) {
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(
shopItems, false
);
}
}
Hmmm, try to put the below as plugin and then in the shop event make a script box with prepareLootShop(X);where X is the enemyId you want to reference. It seemed to work when I put an enemy with 1 random drop out of 3, but I didn't really check the plugin in depth.

function prepareLootShop(enemyId) {

let poolsToDrop = [];
let items = [];

for (let table of $dataEnemies[enemyId].dropTables) {
if (table.rate < Math.random()) continue;
let pool = dingk.Loot.getDropCategory(table);
if (pool) poolsToDrop.push(pool);
}

if (poolsToDrop.length) {
items = dingk.Loot.getItemsFromPool(poolsToDrop);
}

let shopItems = [];
let itemType = 0;
items.forEach(item => {
if (item.atypeId) {itemType = 2; shopItems.push([itemType, item.id, 0])}
if (item.wtypeId) {itemType = 1; shopItems.push([itemType, item.id, 0])}
if (item.itypeId) {itemType = 0; shopItems.push([itemType, item.id, 0])}
})

if (shopItems.length) {
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(
shopItems, false
);
}
}

Holy moly, this is so good! Thanks so much! I'll have to change how some things in the game work but it'll be well worth it.

One thing I'm wondering, if it wouldn't be too hard to implement, is there a way to separate out the rolling of the shop and the opening of the shop into two different script calls?

I am eternally grateful for this btw, this will really take my game's shop system where I've wanted it to be the whole time, and I will be adding you to the credits!
So I take it that everything worked properly? 😀
And yeah, to separate them, I guess put in the plugin file just (change X to variable id you want):

function prepareLootShop(enemyId) {

let poolsToDrop = [];
let items = [];

for (let table of $dataEnemies[enemyId].dropTables) {
if (table.rate < Math.random()) continue;
let pool = dingk.Loot.getDropCategory(table);
if (pool) poolsToDrop.push(pool);
}

if (poolsToDrop.length) {
items = dingk.Loot.getItemsFromPool(poolsToDrop);
}

$gameVariables.setValue(X, items);

}

Use the script in the earlier post to make shop loot, then use another script to call the shop with the loot (change X to same variable id):

let shopItems = [];
let itemType = 0;
$gameVariables.value(X).forEach(item => {
if (item.atypeId) {itemType = 2; shopItems.push([itemType, item.id, 0])}
if (item.wtypeId) {itemType = 1; shopItems.push([itemType, item.id, 0])}
if (item.itypeId) {itemType = 0; shopItems.push([itemType, item.id, 0])}
})
if (shopItems.length) {
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(shopItems, false);
}
So I take it that everything worked properly? 😀
And yeah, to separate them, I guess put in the plugin file just (change X to variable id you want):

function prepareLootShop(enemyId) {

let poolsToDrop = [];
let items = [];

for (let table of $dataEnemies[enemyId].dropTables) {
if (table.rate < Math.random()) continue;
let pool = dingk.Loot.getDropCategory(table);
if (pool) poolsToDrop.push(pool);
}

if (poolsToDrop.length) {
items = dingk.Loot.getItemsFromPool(poolsToDrop);
}

$gameVariables.setValue(X, items);

}

Use the script in the earlier post to make shop loot, then use another script to call the shop with the loot (change X to same variable id):

let shopItems = [];
let itemType = 0;
$gameVariables.value(X).forEach(item => {
if (item.atypeId) {itemType = 2; shopItems.push([itemType, item.id, 0])}
if (item.wtypeId) {itemType = 1; shopItems.push([itemType, item.id, 0])}
if (item.itypeId) {itemType = 0; shopItems.push([itemType, item.id, 0])}
})
if (shopItems.length) {
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(shopItems, false);
}

Haha yes, everything worked properly, and this does as well! Thank you so much! You don't even know how much better this makes my game!

Original thread: https://forums.rpgmakerweb.com/threads/script-to-use-dingks-loot-tables-for-shops.179592/