Create Runtime Loot Tables - Chests
This is an update to the way Chest.cs works, and utilizes a random Scriptable Object LootTable. Minimal changes to basic Mythril, so it’s pretty much drop in, and will not break anything else that you’re in the middle of, or have already done with Chests in the game. What is does, is when a player opens a chest, it checks what level they currently are, and rolls loot based on the Loot Table you setup, with level ranges on the loot, so you can have more open world, as the monsters scale with player, so does the chest loot. Works well in procedural dungeons as well:
Here’s the steps to get this working, firstly, you need this script: LootTable.cs
This is the new randomizer - you can see in the screen shot what a demo one looks like.
The next part is super important, in Chest.cs, please make these two changes:
Add
[SerializeField] private LootTable m_lootTable;
I added it right under ChestLoot m_loot;
Secondly in TryOpen(), right inside if (!m_opened)
public async Task<bool> TryOpen()
{
if (!m_opened)
{
if (m_loot.IsEmpty() && m_lootTable != null)
{
m_loot = m_lootTable.RollLoot(GameManager.Player.level);
}
else if (m_lootTable != null)
{
var extraLoot = m_lootTable.RollLoot(GameManager.Player.level);
List<ChestLootEntry> combinedEntries = new List<ChestLootEntry>();
if (m_loot.entries != null)
combinedEntries.AddRange(m_loot.entries);
if (extraLoot.entries != null)
combinedEntries.AddRange(extraLoot.entries);
m_loot.entries = combinedEntries.ToArray();
m_loot.money += extraLoot.money;
}
//LEAVE EVERYTHING ELSE JUST ADD THE STUFF ABOVE - should come right before these two lines:
TryPlayOpeningAnimation(true);
TryPlayContentRevealAnimation();
So what this does:
- Normal Chest Loot Table intact - so put quest items, or guaranteed loot - or leave empty - doesn’t matter…
- Make a Loot Table SO - like the screen shot, with level ranged loot, and assign the loot table to the chest - or don’t - works either way
- What the player gets - ALL the loot you assigned - the static loot from the chest and the random loot from the table if one is assigned
How the Loot Table Works:
- Loot Groups:
- Groups of items you want, and the change for rolling those items, for instance, in my screenshot you see consumables… chance = 1.00 (100%) number of rolls = 2 min / max level
So you will have a 100% chance to get 2 items from the loot table in this group, if you are level 1-5
In the second group called Items - .5 (50% chance) - 2 rolls - level 1-20… so you get 2 chances at 50% - i.e., you could get 0, 1 or 2 items from this groups loot pool.
- Drops: If the player passes a roll % chance to get an item, it will check the combined weights of the loot, and then select and item - and assign a quantity based on the min/max
So in the items one, we have Iron Sword 10, Iron Plate 10, Lucky Sword, 1…. so each time you open the chest, you have 1: 50% chance, to get 10/21 Iron Sword, 10/21 Iron Plate, or 1/21 Lucky Sword 2: Second 50% chance to get the same…
Hope this is clear - and useful in your projects!


š¬ Comments (5)
š Scripts
Want to continue the conversation?