Comment on page
LootBoxis a container for
ItemObjectswhich 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
LootBoxhas 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
ItemAttributeson them when they are spawned in a
I will be creating my own version of the method which creates the loot, in order to insert some code that augments the
ItemAttributechances by the players Luck
Decide where you'd like to store your
LootBoxobject, and right-click inside to select the menu option:
/Create/Game Modules/Create/Loot Box
This will create a
LootBoxScriptable Object in your project. Select that and view the Inspector to begin setting up your data.
Toggle on "Show Instructions" to show...instructions!
LootItemsobject 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.
LootItemsobject that is added to a
LootBoxis represented as a
LootBoxItemsSettingsobject, which holds the data about what and in what way
ItemAttributeswill be spawned.
Click the "Eye" icon to expand the
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
ItemAttributetypes which have been toggled on as "Allowed" for the items in the
LootItemsobject. 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
ItemObjectsdo get attributes of the same type.
It is possible that different
ItemObjectsin your LootItems object can hold different types of
ItemAttributes. The script will not apply an
ItemObjectif it can not hold that
However, I suggest that
LootItemsshould generally contain similar types of
ItemObjects, to minimize this occurance.
ItemAttributedrop downs will let you choose "None", "All", or any number of
ItemAttributesfrom the list. If more than one is selected, when an
ItemAttributeof this type is successfully added to the
ItemObjectbeing 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
ItemObjectitself 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
ItemObjectin the list will spawn. If "Stop after first failure" is toggled on, then the system will stop attempting to spawn
ItemObjectsas 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
ItemAttributeswill 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
ItemAttributesare attempted. This may be useful in creating a bit more randomization, especially if there are a lot of
If the order is randomized, it will be randomized for every
ItemObjectcreated. Forced attributes are always added, so long as at least one is selected in the upper section, and the
ItemObjectspawned is allowed to have the attribute.
The spawning code will run through each
LootBoxItemsSettingobject 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
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.