Comment on page
LootBox.cs
v4.0
A
LootBox
is a container for ItemObjects
which you can place on anything that has to hold ItemObjects
-- a box, treasure, bucket, drawers, enemies, whatever you'd like. You could even use them to showcase items on a countertop or other surface.The best part is, the
LootBox
has built in randomization which you can set up, to provide a good amount of control yet still give players a random experience.As there are many ways to deal with items once they are spawned, the script is ready to be modified and expanded upon, so that you can customize the algorithms and handle the loot once it is created.
Why would you want to customize the algorithms? In my game, "Legend of the Stones", players will have a "Luck"
Stat
, and this will increase the chance of items having ItemAttributes
on them when they are spawned in a LootBox
.I will be creating my own version of the method which creates the loot, in order to insert some code that augments the
ItemAttribute
chances by the players Luck Stat
.Decide where you'd like to store your
LootBox
object, and right-click inside to select the menu option:/Create/Game Modules/Create/Loot Box
This will create a
LootBox
Scriptable Object in your project. Select that and view the Inspector to begin setting up your data.Toggle on "Show Instructions" to show...instructions!

Select a
LootItems
object from the drop down, and click "Add" to add it to the list. You can add any number of LootItems
, including duplicates, in any order. In general, if you want to guarantee an item is spawned, like a quest item, it should be higher up in the list...or first!When you have added multiple
LootItems
, each one can be moved up and down in the order using the up and down arrow buttons.
Each
LootItems
object that is added to a LootBox
is represented as a LootBoxItemsSettings
object, which holds the data about what and in what way ItemObjects
and ItemAttributes
will be spawned.Click the "Eye" icon to expand the
LootBoxItemsSetting
.
At the start, no items have been added, so nothing will spawn. Click "Add" to add an item. Go ahead and add a few more, and you should see something like this...

Each Item, and also Attributes, always start with all selected. The system will choose randomly which specific items and attributes are spawned, unless you select only one option.
On the left of the item list, you can modify the order items will attempt to spawn. The percentage shown are the chances that each item will spawn, based on the "Spawn Chances" curve. The "Force" toggle will set the row to successfully spawn.
Remember, depending on the Inventory system you're using, if a spawn item "can't fit", then it may not show even if
force == true
!The yellow popup menu shows all of the
ItemAttribute
types which have been toggled on as "Allowed" for the items in the LootItems
object. Click "Add" to enable these attributes in the LootBox. Like the Item Object, you can select some or all, and let the system chose randomly, or select just one. You can also select "None", which will ensure the system will not add any attribute of this type, even if other spawned
ItemObjects
do get attributes of the same type.
It is possible that different
ItemObjects
in your LootItems object can hold different types of ItemAttributes
. The script will not apply an ItemAttribute
to an ItemObject
if it can not hold that ItemAttribute
.However, I suggest that
LootItems
should generally contain similar types of ItemObjects
, to minimize this occurance.The
ItemAttribute
drop downs will let you choose "None", "All", or any number of ItemAttributes
from the list. If more than one is selected, when an ItemAttribute
of this type is successfully added to the ItemObject
being spawned, the script will randomly select one from those selected.
Here, I've unselected the "Broken" attribute, so that no spawned items will spawned in a "Broken" state. Though they might be spawned "Damaged" or "Worn"!
The column with the
ItemObject
itself can have "All" or any number of ItemObjects selected. If the item is successfully spawned, a random ItemObject from those selected will be spawned.The Spawn Chances curve determines the odds that any
ItemObject
in the list will spawn. If "Stop after first failure" is toggled on, then the system will stop attempting to spawn ItemObjects
as soon as one attempt fails. Once you have a few items listed, toggle this on and off and you should see the percentages change, indicating the odds that any given row will spawn.
The Attribute Chances curve determines if one or more
ItemAttributes
will be added to the ItemObject
. There is a "Force" toggle here as well, and the odds are displayed. "Stop after first failure" works the same as with Spawn Chances, and the "Randomize order" toggle will, if true
, randomize the order in which ItemAttributes
are attempted. This may be useful in creating a bit more randomization, especially if there are a lot of ItemAttribute
objectTypes
available.If the order is randomized, it will be randomized for every
ItemObject
created. Forced attributes are always added, so long as at least one is selected in the upper section, and the ItemObject
spawned is allowed to have the attribute.The spawning code will run through each
LootBoxItemsSetting
object one after another, regardless of the outcome of the earlier groups. This will allow you to mix and match various groups if items in different and perhaps interesting ways for different types and levels of LootBox
objects.Click the "Test Generate Loot" button to see what kind of loot may be spawned. Do it again and again to get a quick sense of what your players may discover.

Last modified 5mo ago