Let's talk loot

I’ve hit a wall in my logic and need some help figuring out the next piece dealing with different “types” of items and having an interesting way of dynamically generating loot for containers.

When a container is created I generate it’s contents, I had this originally separating the items in different structs like “Weapons”, “Consumables” etc… but I realized I can’t return an array of different type structs to my player after they “Open the container and get all the loot”

Am I stuck with having to put all items into one struct and manually creating individual loot tables for all the different combinations and attaching those to my containers (which seems really tedious) or can I programmatically define them somehow?

Currently I’m also leveraging a weight system which makes sense to me for picking one item, but what if I want to give the user X amount of items? Do I need to attach a total value to the container as well as a value to each individual item and then add up each item that gets picked from the weighted loot table then stop generating when the total is reached?

And then last thought is, if I’m using one struct to house all my items, I’ll need to leverage Enums to define Item type so I know which Items are “weapons” allowing my player to equip those, etc…

Would you then leverage a TMap<Enum Item Type : TArray for the player’s inventory?

I hope this makes sense, I’ve been spinning in my head while reading/watching a lot but the solutions I saw stop after generating randomly one item.

~Thanks for reading~

Here’s what I do for my inventory system. Every item only ever has the same struct so it can be stored in a array. You store the minimal information to find the item, not the whole item’s information.

struct FItemInfo
{
	FName ItemID;
	int32 MaxStack = 1;
	int32 Quantity = 1;
	TEnumAsByte<EItemType> ItemType;
};

Use the ItemID to find it in the DT_ItemType (DT_Weapon/DT_Armor/DT_Consumable). Your system has weight so you may want to do more calculation moving around containers beside quantity.

Container loot generation would use ItemID/Quantity/SpawnChance to generate loot array. I make several DataTable for different container with rows like

struct FRandomItem
{
	FName ItemID;
	int32 SpawnWeight = 1;
	int32 MinAmount = 1;
	int32 MaxAmount = 1;
};

1 Like

Thanks that helps me a lot to conceptualize how to organize it.

How do you use min and max amount? Is that how many times it can potentially spawn as loot in the container?

I use SpawnWeight to determine the chance of spawning. In this table, all rows have the same chance to spawn because their weights are the same. Once the row is determined, do a random amount in Min/Max.
image

How I generate from DT:
1.Get total amount of rows(items) and total weight of all items

2.A loop for how many times should the container generate items. Each time set the target row.
image

3.Loop through the DT_LootDrop to see which row is the target item.

4.How I set my item information, yours would be different. Add the target item to the output array.

5.Out put the array of structure to your container and store it.

If your loot logic has more things going on like not repeating loot, handle that within the function. I parse the out put struct array to another function that stacks any potential stackable items within the array.

1 Like

you can also use DataAssets to define you static item data, this allows you to use inheritance

also there is a new system called FInstancedStruct which allows you to have an array of different structs

1 Like

Thank you for that suggestion, that could be exactly what I use for returning an array of different loot from a container. Your question asking how to use it too is a great post if anyone else is looking to see how to use one. https://forums.unrealengine.com/t/can-someone-show-my-how-to-use-finstancedstruct-please/1898788

1 Like