Generating & Handling Loot

v4.0

Generating Loot

If you would like to control when the loot is generated, make sure generateOnAwake == false. Then, you can call the GenerateLoot() method. There is an optional bool overwrite parameter, which is false by default. If true, the LootBox will overwrite the existing loot if it has already generated loot. If false, GenerateLoot() will do nothing at all.

gameLootBox.GenerateLoot();

Handling loot

View the Party Based RPG demo game to see these scripts in action, bringing the LootBox system together with our Inventory system.

The demo has UI buttons which mimic Loot Boxes, physical boxes you can interact with by pressing "Space", and enemies and quests, all of which make use of the LootBox module.

In the demo scene, you'll find two UI Buttons ("Open Small Box" & "Open 'Lucky' Treasure Box") which are representing treasure boxes -- these two buttons each have a LootBox which the player can open and collect ItemObjects from to put in their inventory using the Inventory module.

GameLootBoxDemo.cs inherits from GameLootBox.cs and is how we will override the method used to handle loot once it is created.

This is a step you will need to take. As there are countless inventory management systems, whether you use the Inventory module, your own system, or another invenetory asset from the Asset Store, you'll need to write a small amount of code to handle the loot once it is generated.

The HandleLoot() method is essentially empty in GameLootBox.cs, so I have written a small amount of code which will override HandleLoot(). The method does two things.

For each GameItemObject that was created, the script will attempt to add the item to the inventory of the box object, using the BoxManager. The drag-and-drop Inventory System includes a mechanic features limited space based on a grid, inside the "box", so there's a chance that if too many items are spawned, or if some are too big, some items won't fit.

The script will keep track of what does not fit, and add those to a list of items to delete. The second thing it does, after placing items, is to delete those items from the list of items on the loot box object.

In this version, items which can't fit are deleted. Perhaps in your project they're handled differently. That is something you may choose to code yourself.

Remember, Game Modules lets you create YOUR project, and does not force your project to act one specific way.

Configured properly with the Inventory System, this is the only step required to pass the generated loot into the system that allows players to move the items around.

GameLootBoxDemo.cs
public class GameLootBoxDemo : GameLootBox
{
    protected override void HandleLoot()
    {
        WriteToConsole("Handling Generated Loot", "GameLootBoxDemo"
            , writeToConsoleColor, writeToConsole, false, gameObject);
            
        var boxManager = GetComponent<BoxManager>();
        var deleteList = new GameItemObjectList();
        foreach (var itemObject in lootBoxInventory.list)
        {
            // Try to add it to the list, if so, continue
            if (boxManager.AddLootBoxItemToList(itemObject)) 
                continue;
                
            // Could not add to the list, so we will move it to the delete list.
            WriteToConsole($"Could not fit {itemObject.objectName} into the inventory box"
                , "GameLootBoxDemo", writeToConsoleColor, writeToConsole, false,
                gameObject);
            deleteList.Add(itemObject, false);
        }

        // Remove all the items in the Delete list
        foreach (var item in deleteList.list)
            lootBoxInventory.Remove(item);
    }
}

Last updated