---
description: v4.0
---

# Generating & Handling Loot

## 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.

```csharp
gameLootBox.GenerateLoot();
```

## Handling loot

{% hint style="success" %}
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**.
{% endhint %}

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.

<figure><img src="https://2431624982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MY3N_li2jPq7az6mYfq%2Fuploads%2FxZWIxlIBVn4aHjUKbkhf%2FScreenshot%202023-06-18%20at%2011.30.17%20PM.png?alt=media&#x26;token=98a7ffcd-4588-4050-a872-b1020d931c53" alt=""><figcaption><p>Don't forget to add Saveable! The "Box Manager" is from the  drag-and-drop Inventory System, and the "Treasure Box" component handles player interaction in the demo scene, including opening the box when the player interacts.</p></figcaption></figure>

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

{% hint style="warning" %}
_**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.
{% endhint %}

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.

{% hint style="info" %}
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.
{% endhint %}

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.

{% code title="GameLootBoxDemo.cs" %}
```csharp
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);
    }
}
```
{% endcode %}

{% content-ref url="custom-loot-generation-algorithm" %}
[custom-loot-generation-algorithm](custom-loot-generation-algorithm)
{% endcontent-ref %}
