Loot Box

Scripting with the LootBox class.

A class which can be used to receive a randomized selection of items.

Fields & Properties

Name
Description
Type
Access

id

An ID that can be used to determine if a loot box has already been obtained.

string

public

possibleDrops

A list of all possible item drops.

List<LootBoxDrop>

public

minItemDrop

The minimum amount of items that can be dropped from this loot box.

int

public

maxItemDrop

The max amount of items that can be dropped from this loot box.

int

public

Creating a Loot Box

Since loot boxes are scriptable objects, you can create them with ScriptableObject.CreateInstance.

LootBox lootBox = ScriptableObject.CreateInstance<LootBox>();

Working with a Loot Box

Setting Possible Drops

Creating a Drop List

The code below is an example of how you can create a drop list. In the example, 2 possible drops are added to a loot box, each with a 50% probability of dropping. The myItem and myOtherItem fields are instances of an Item.

lootBox.possibleDrops = new List<LootBoxDrop>()
{
    new LootBoxDrop()
    {
        item = myItem,
        amountRange = new Vector2(0, 64),
        dropRate = 1
    },

    new LootBoxDrop()
    {
        item = myOtherItem,
        amountRange = new Vector2(0, 64),
        dropRate = 1
    },
};

Randomized Drop Amount

A random number of items are dropped based on the minItemDrop and maxItemDrop fields.

// Random range between 1 - 5 items
lootBox.minItemDrop = 1;
lootBox.maxItemDrop = 5;

Getting Drop Chances

You can get the drop chance of each possible drop as a percentage out of 100 by it's index with the CalculateDropChance method.

// Get drop chance of an item drop at index 0
float dropChance = lootBox.CalculateDropChance(0);

Getting Loot

The GetLoot method can be used to get random loot. This can be considered as opening the loot box. This returns an ItemStack list.

var itemStacks = lootBox.GetLoot();

Last updated