Download

Different struct depending on item in container?

Hey guys!

So I’m working on containers in my game. Specifically, a box that holds stuff.
Ye.

So here’s my system so far. The container has an array of structs, called “ContainerItemStruct”. This is has two values: “Item” (Which is a blueprint) and “Amount”.
Huzzah. This works well!

Screenshot_4.png

HOWEVER… This is very limited. What if I want to add a sword of damage 12, or a pizza with defense 4? Basically, I want to add more struct vars… but only when the item needs those vars. Armor will have a defense var, but a sword will have damage, and so on.

So… any ideas? I’m thinking having the ability to select a category and that will provide a different struct depending on whether the category is “weapon” or “armor”, but I’m not quite sure how to do that.

Thanks! :smiley:

you best bet is to use a struct with all possibilities, and if the option is not needed for that item just keep it blank, i would also make sure to use the same struct layout for your player inventory this way it plugs right over when adding items. My struct currently has 13 members

Ehh alright, I’ll probably end up doing this. I was hoping that there would be another way, but alas I suppose not.

Thanks!

You can efficiently implement this using class hierarchy. If you are using blueprints then you could start with creating a base class “Item” which describes some most generic properties of items you want to store, like weight or how many inventory tiles it contains and etc. then for specific items you create them as child blueprints of basic Item blueprint and add all custom properties there. You can build it as a tree of classes starting from base Items create sub-base Weapons and Wearables from which you extend your hierarchy further. Nice thing about this is that your code will be nicely encapsulated and you can always store references to objects using their parent classes. For example array of Items can store valid reference to any object which is child of Items. To use its unique functions you would need to cast it first but that is easy and again can be done to any level of hierarchy.

I have this system already, the problem is that I cannot figure out how to add different classes of items in the little menubox thing

use byte for all logic and instaced blueprint with enumerator for frontend.

well, on normal C++ there is a struct called “union” IIRC… it serves that purpose, thought on the first element to know at runtime, you should add a identifier to know which type of thing you have.

Dont know if UE allows unions to be used instead of structs.

Also, if you have this hierachy of things as classes, you should create a array that can hold the parent class, you will be able to use them at runtime, thought dont know how the editor handle it automatically (ie show you the specific properties of each item).


Following Crierr says, I guess if you build components on creation, you could add as many subcomponents (or items) as the array enumerator of the available items.