# 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="/files/t9JE9QXIAOgOHlbbYBMc" 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="/pages/yZmafMAFpiUZLaOmBbDC" %}
[Custom Loot Generation Algorithm](/magic-pig-games/game-modules-4/module-documentation/loot/custom-loot-generation-algorithm.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/game-modules-4/module-documentation/loot/generating-and-handling-loot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
