How to Build a Professional Weighted Loot System in Unity

Hello, developers!
Welcome to the first devlog for Endless Encounters. Today, I want to share a core piece of the engine with you: a professional, weighted loot table system.
Every developer working on an RPG or survival game has faced this problem. You need an enemy to drop loot, so you write a quick script with Random.Range(0, 100)
. If the number is 99, it drops the rare sword! But this approach quickly becomes difficult to manage and balance. What if you have hundreds of items? How do you give one item a 7.5% chance and another a 12% chance?
The solution is to use a data-driven approach with ScriptableObjects
to create powerful, reusable, and designer-friendly loot tables. I'm providing the complete script for the system I built below—feel free to use it in your own projects!
The Code: LootTable.cs
This script allows you to create any number of LootTable
assets in your project. Each table contains a list of items, and each item has a "weight." The higher the weight, the more common the drop.
C#
using System.Collections.Generic; using UnityEngine; namespace EndlessEncounters { /// <summary> /// A ScriptableObject that defines a collection of items with weighted chances for dropping. /// This allows designers to create and manage loot tables easily in the editor. /// </summary> [CreateAssetMenu(fileName = "NewLootTable", menuName = "Endless Encounters/Loot Table")] public class LootTable : ScriptableObject { [System.Serializable] public class LootItem { public string itemID; [Min(0f)] public float weight; } public List<LootItem> lootItems = new List<LootItem>(); private float totalWeight; private bool isInitialized = false; private void Initialize() { if (isInitialized) return; totalWeight = 0f; foreach (var item in lootItems) { if (item.weight > 0) totalWeight += item.weight; } isInitialized = true; } public string GetLoot() { Initialize(); if (totalWeight <= 0 || lootItems.Count == 0) return null; float randomValue = Random.Range(0f, totalWeight); foreach (var item in lootItems) { if (item.weight <= 0) continue; if (randomValue <= item.weight) return item.itemID; randomValue -= item.weight; } return null; } private void OnValidate() { isInitialized = false; } } }
Key Design Choices
- Weighted Logic: Instead of dealing with fragile percentages that must add up to 100, this system uses weights. An item with a weight of
50
will appear five times more often than an item with a weight of 10
. It's intuitive and easy to balance.
OnValidate
for Designers: This script includes a special OnValidate
function. This is a Unity Editor trick that automatically detects when a designer changes a weight value in the Inspector and tells the system to recalculate its totals. It makes the tool foolproof.
- Clean API: The simple
GetLoot()
method is all you need to call from your enemy death script or treasure chest script to get a valid item ID.
The Bigger Picture
This robust loot table system is just one of the core modules inside the Endless Encounters: Dynamic Mission & Loot Generator. The main engine uses these tables to automatically generate meaningful rewards for the infinite procedural quests it creates.
If you found this script helpful and want a complete engine that handles not just loot, but also dynamic mission generation, objective tracking, and XP scaling, please check out the full asset on our main page.
<a href="https://rottencone83.itch.io/endless-encounters-dynamic-mission-loot-generator" target="_blank" rel="noopener" externallink="" _nghost-ng-c889489730="" jslog="197247;track:generic_click,impression;BardVeMetadataKey:[[" r_39f64fac98552e78","c_a9bbb297a90114eb",null,"rc_01f0cd05d3f218e8",null,null,"en",null,1,null,null,1,0]]"="" class="ng-star-inserted">Click here to view Endless Encounters on Itch.io</a>
Thanks for reading, and happy developing!
Files
Get Endless Encounters: Dynamic Mission & Loot Generator
Endless Encounters: Dynamic Mission & Loot Generator
Dynamic missions, smart loot, and XP systems for Unity. Modular, mobile-ready, and fully customizable.
Status | Released |
Category | Tool |
Author | Rottencone83 Builds |
Genre | Role Playing |
Tags | game-assets, gamedev, loot-system, Procedural Generation, quest-system, scriptableobjects, source-code, tool, Unity |
Leave a comment
Log in with itch.io to leave a comment.