How to make an inventory that accept multiples types of structs?

I don’t know if the question exemplifies it well, but i will try to explain it the better i can.

I am trying to make an inventory system, just to learn how to do it. But in a game is commom to have different types of items, with different data. A weapon for example, must have ammo, that must be stored for each weapon instance in your inventory, so if it had 30 initial ammo, and you shot 15 bullets, it needs to update and store 15 ammo instead of 30.

So i tried doing it with structs, and i made this struct:

ST_Item*

All items shoud have these information, but a weapon, for example, should also have ammo . But if i make a map variable to store the item structures, like this one:

It will only accept ST_Item structs. So i don’t know how to store different types of items with different data in this inventory.

If there was a way to store a generic struct reference, like you can make with class, that will accept any class, it would solve my problem, but i don’t know how to do it

the traditional way is to either have one giant struct with everything, or maybe a struct with nested structs of different types.

the new way is to use FInstancedStruct which is included in the StructUtils plugin, however this is technically experimental

an alternative is to use UObjects instead of structs

1 Like

So the tradicional way would be like this?

If it is a cube item, i just define the CubeItem variable and ignore the rest and vice versa?

And the InstancedStruct only works in c++ or is there a way to do it in blueprint? I have only found c++ tutorials

yep basically like you’ve posted, i should warn you massive structs are quite buggy in BP when you try to add/remove new elements. just backup your project and exit/reload if you do.

otherwise InstancedStruct works in BP, thats basically what its for since in C++ you can have struct inheritance already

1 Like

Thanks. I have made somethin like this before, but i was not sure if that was the best way. I’ve been racking my brains trying to solve it

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.