Road to Game Dev: Drop Tables and Loot

Rowan Mcmanus
5 min readAug 7, 2021
Gif from an earlier prototype

“Loot” is a staple of modern gaming, and has been since the days of the early RPG. Kill monsters, get treasure and powerful items as a reward for overcoming the challenge. Nowadays, it manifests as even random item drops mid-battle — like powerups. Today we’ll take a look at some options for creating a simple “drop table”.

What is a Drop Table?

A “Drop Table” in games is effectively a spreadsheet, or list, of every possible item that can drop from something, be it a treasure chest or a defeated enemy. This list comes with the rarity of those drops — put together, it basically creates a raffle system. Each time you drop an item, you run the raffle; items with more tickets are more common and more likely to win. We can use this to our advantage to gain a lot of control over randomness!

There are a couple of options for how this table is actually set up. One option is to have a total range you can roll on, and then assign a range to each item on the table. However, this gets super clunky when trying to make any adjustments, and can be pretty limiting. So instead, we’ll take option two: Giving each item a number of tickets, and then rolling based on the total number of tickets. There is also a third option: Divide items into different rarities, and then just roll to decide which rarity you want, before rolling within that rarity. However, this system is best when you have a lot of items to choose from, and we only want to look at a few right now.

You also may ask: “What if I don’t want an item to drop at all sometimes?” There’s two ways to do this! You can either make “nothing” one of the “drops” and give it tickets like anything else, or you can check if nothing drops before you roll on the table by giving the table only a chance to be used at all. Either way will work, depending on what you want to do. For now, we’ll use the latter, since it’s a bit easier to set up.

Creating and Using a Drop Table

As you can imagine, we’ll be using our friend the Random Number Generator pretty heavily here! First, we want to even know what will exist on the table, so let’s create an array of game objects and populate it:

Now we need to actually give tickets to each of these powerups. This is pretty simple; we just need a variable and a function to call it.

Now comes the hard part. We need to build a function we can call whenever we want to actually roll on a drop table, tell it what range to roll in, and then actually grab a powerup once you’ve rolled it. This is a lot harder than it sounds, so I’ll show the code first and then break it down. Important to note is that this will be a public function we call from an enemy (or treasure chest, etc) whenever we want to roll for a powerup. Here it is:

First off, the easy part: Passing in spawnlocation whenever we call this from an enemy just tells the function where the powerup actually should be. This is simple, but easy to gloss over. Same thing with canSpawn — while we shouldn’t ever be calling this function while spawning is disabled, it still needs to be checked.

Now, we have to actually create the drop table in the code. We’ll use a “for” loop for this; similar to a while loop, this will run continuously until the condition is met, but unlike while loops, we can specify conditions and how each iteration affects those conditions. In this case, we want to run through our entire list of powerups to look for their drop ratio; so we create a temporary integer “i” and increment it up each time, until we run out of powerups to check — which we can find out by asking for the length of the array. Each one we check adds their drop ratio to the maximum, until we have a grand total at the end. Now we need to roll within that range, and actually pull out that entry from the array.

For this, we’re going to do something that might seem a little strange. We’ll start by making the random roll, but then we’re actually going to rebuild the drop table again. The difference this time, however, is that each time we grab a drop ratio, we’re going to check against the roll value. Once we pass the value, then we know it rolled in the range of that powerup, so we can stop there and instantiate the one we were on!

Obviously, there are a lot of other ways to do a system like this — but this is a nice and simple way to start off with, and works pretty well! There are a couple of pitfalls, however; when creating your for loops, always remember that arrays start at 0, and an array with length 5 will end at 4. Therefore, you never want i to be equal to the length, or it will try to check an empty space on the array, and weird things begin to happen.

--

--