Generating & Handling Loot
v4.0
Last updated
v4.0
Last updated
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.
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.