Chest Placement Randomizer
So continuing with my Random Loot project - this add-on is also stand-alone - or can be used in combination with <#1394477177721979010> . So what this does is allow you to make a single parent object that contains the ChestRandomizer.cs on it.
ChestRandomizer, allows you to assign chests to it, and a % chance of which chests should show up, and how many min/max chests should show up.
Said another way, if you have 6 chest ’locations’ on the map, you want 1 chest it ALWAYS be there, do not include it in the Randomizer, just put it where you want it. the remaining 5 chests, you want 1-2 of them to show up, so set min = 1, max = 2. Then you can individually give % to the chest locations, i.e., chest 1 is prominent in a pile of gold, it has a 75% chance of being picked, all the other chests of a 15% chance…
The script will then cycle through, in a random order, and see if any chest should spawn against it’s individual chance % - once it has spawned enough chests that it has created at least the min, or it hits the max… it stops. There are safe guards in place to clamp the chests if you don’t assign enough chests for the min spawn count.
This let’s you build rooms or maps with 10-20 chest locations, but at runtime only 5-6 actually show up, so people can play through your game multiple times and chests are in different locations.
Also it is fully setup through the persistable save system, so if you have 3 chests in a room, and only 1-2 are supposed to spawn, that will only run 1 time, and it will remember which 1-2 chests were in that room, and obviously the chest itself will continue to check if it was already opened. Works when you leave/enter rooms, as well as save inside/outside the room with the chests.
How to implement, first download ChestRandomizer.cs
Secondly, change this in Chest.cs:
public class ChestDataBlock : EntityDataBlock
{
public bool opened;
public bool isActive;
}
And this part:
protected override void OnSave(PersistableDataBlock block)
{
base.OnSave(block);
var data = block.As<ChestDataBlock>();
data.opened = m_opened;
data.isActive = gameObject.activeSelf;
}
protected override void OnLoad(PersistableDataBlock block)
{
base.OnLoad(block);
var data = block.As<ChestDataBlock>();
m_opened = data.opened;
gameObject.SetActive(data.isActive);
}
I’ve also done up a quick video showcasing this in action - along with the Loot Table system.

💬 Comments (1)
Want to continue the conversation?