Galaxy Shooter 2D — Loot Tables for Powerup Drops #26
Balancing the Powerup spawns by adding rarity!
Do you wonder how RPGs, Looter Shooters, or lootboxes handle which weapon or reward you’ll get after killing an enemy or opening a box? Then, lucky for you, I recently implemented Loot Tables in my game, which is precisely what these games / boxes use, and that’s today’s topic!
What is a Loot Table
A loot table is a list of items that can be randomly selected, and the drop percentage of each item is determined by weights, meaning that every item drop weight is added to a total amount of weight and the percentage from each item is calculated, and finally, a random number between 0 and that total weight is generated and it is used to get an item from that table.
For example:
- Total Weight: 18
- Ammo Powerup: 8 (8 / 18 ≈ 44,4%)
- Speed Powerup: 6 (6 / 18 ≈ 33,3%)
- Triple Powerup:4 (4 / 18 ≈ 22,2%)
You can interpret a loot table as a little container where you put an amount of items inside it (represented as the weight), and the probability that an item will be taken out of that box is determined by that very same amount (if you have 9 items of type X, and only 1 of type 1, you are most likely to pick up an item of type X (90%)).
If you want to read more about Loot Tables, you can read two great articles about them here:
Defining Loot Tables in ARPG Game Design
Loot drop best practices
How did I implement it
I’ve created a new class name PowerupSpawn, which has the Powerup Type to spawn (item to drop), and a weight (drop percentage).
Next, I created an array of that class and filled it with all the powerup types in the Editor’s Inspector. Because loot tables are static, it is preferable to use an array over a list (arrays are faster to access).
Knowing that the loot table will always be the same, I can calculate the total weight of every item in the Start method.
Now, in my method where I’ll want to spawn an item, I ask for a random number between 0 and the total weight amount which is our rolled “percentage”.
And finally, with that generated weight, it checks all the items and sees if the weight matches the powerup weight. If it does, it sets that powerup type, and if it doesn’t, subtract the weight and continue checking the other powerups.
To understand what this does, I’ll provide two examples with the loot table I created in What is a Loot Table:
Example 1
The new generated weight is 3 (≈ 16,7%).
- The first item in the array is the Ammo Powerup, which has a weight of 8. The weight is lower than the Ammo Weight.
- AmmoPowerup is selected.
Example 2
The new generated weight is 14 (≈ 77,8%).
- The first item in the array is the Ammo Powerup, which has a weight of 8. The weight is obviously higher than this powerup weight.
- We subtract the new weight by the Ammo Weight, and now, the new weight is 6 (≈ 33,3%).
- The next item in the array is the Speed Powerup, which has a weight of 6. Both weights are equal.
- Speed Powerup is selected.
Conclusion
With loot tables, the game balance feels nicer, and the OP weapons such as the Heat Seeking Lasers or the Laser Beam don’t drop as often as health, ammo or speed.