Inventory System / Polymorphism

Hey all, I am trying to create an inventory system but wondering how is the best way to go about this, I was trying to structure blueprints but I can’t find a way to polymorph them. basically I want a blueprint ‘Inventory’ that has 40 slots, so will have an array of 40 ‘Inventory Slot’ blueprints which contain 2 variables, int quantity and 1 ‘Item’. this is where I am not sure what to do, I want ‘Item’ to be a class / struct / blueprint with variables and functions as an abstract that other items will implement. such as If I have 10 sticks in my inventory it will be structures like… Inventory location[0] will be an InventorySlot with Stick as the Item object and 10 as the qty, and Stick inherits Item.

what blueprint should I use for the Inventory and Inventory Slots, and how should I do the polymorphism relationship between Item and Stick?

I’ll see if I can find a youtube tutorial on this when I get home (unless someone beats me to it) but the base premise is that you have a base class which all other items are derived from so you can cast back and forth. Inventory would then be an array of struct f.ex. 1) int id 2) int amount 3) BP_MasterItem Item

I’ll try to make it clearer when I get home, if needed :slight_smile:

My Inventory System looks similar to this (snippet)

I recommend looking at how other games implement their inventory.It’s always a help to visualize class hierarchies with UML Diagrams. This can show problems with references etc.

video is unavalible, I have started working on it however my item ‘ItemStick’ is saying ‘wrong class’ when trying to create an instance of it in game, no idea what’s wrong here, why is there no simple Constructor to use? I am trying to add an instance of ItemStick class into the inventory… this should be so simple yet I can’t find anything that works.

Item stick is an actor that has the parent class Item which is also an actor.

ItemStick seems to be an Actor Derived class. Actor Derived Classes need to use “Spawn Actor Class” node and not “Construct Object”

I use a ray trace to detect objects that the player looks at, it is more accurate than a collisions box around the object. Plus when I look at it I can change the icon to a pickup hand which adds a nice touch to it like in the game Far cry 3. Also you will notice that arrays of stucts can be somewhat of an issue if you get complex with it.

I used this tutorial and expanded on it greatly, now my inventory system is legit haha…

these items aren’t items that are spawned in the world, these are created in your inventory when you start cutting a tree down. I’ll try spawn actor but I think I did try that and it didn’t work.

ok guys, so I’ve got a few ideas but still need help. should I change the blueprint type from actor to something else? (i would like to do this but no idea what to do since structs can’t have methods or polymorphism) or should I spawn them invisibly and use the UI to access the variables such as sprites etc from it?

No idea why this is happening… I have changed Inventory to be an actor component. inventory slot to be a structure. Item to be an actor, and ItemStick a child of Item. however when debugging it, it is never creating the actor it seems. Item is none, but quantity is correct at 10. the pics bellow show the flow in reverse order.

ok turns out this is because you can’t call ‘spawn actor’ from a construction script… there really needs to be a message or something telling people this.

however now it’s not getting added to the inventory at all, the data is being created and made into a struct correctly, but it wont add to the inventory… why is this?

this shows the data is correctly being passed into this function.

this is the inventory after the add is called… no change. I have also tried using ‘insert’ to overwrite one of the ‘none’ item slots but this doesn’t work either.

BUMP. Still can’t figure out what is wrong

Are you using Construction Script to add to the Inventory for testing?
If so I suggest using BeginPlay.