UMG inventory question

I followed the Unreal Engine tutorial on setting up a basic inventory system. The system goes over how to add items to your inventory/inventory HUD to have your player character use. I am trying to modify this to also be able to add items via an in-game merchant. I set up a store interface with each item for purchase set as a button. If you click on any button, the quantity increases, and that number is stored in an array (integer). Below these item/quantiy buttons is the “Buy” button. Does anyone know how I could take the “item quantity” array, have it convert to the inventory array, and thus appear in my inventory HUD?

I am a novice to blueprints and even mores o to umg.

Thanks,

You will want to save the ItemClass with the Item that you want to buy. If i remember correctly, Epic is just making the Item Actor invisible and stores a reference in an array.
So if you want to buy an item, you need to either have the item already spawned and just remove it from the merchant array (if this is wanted) and add it to the Inventory, like you
would do with a picked up item. Your inventory should have a function to add an Item to the Inventory. I guess it gets an ItemActor as a reference.

So like i said, if the Item of the merchant is already spawned, use this to add the item to the inventory. If you only have the ItemClass and the quantity, then just spawn the item
and THEN call the add item to inventory function. I don’t know how you save the stacks or if you are even using stacks. So you might want to either spawn x (quantity) items, or
spawn 1 and set its quantity to x.

I ended up changing the inventory system from a slotted system to a more dynamic one. But, I still can’t figure this out on how to add the store purchase to the player inventory. The store is not a traditional walk in and pick up an item store. It is all through UMG, like you would see in games like Dragon Age and White Knight Chronicles. I used the same structure to populate both the player inventory and the store items available, saved under two separate arrays (The store is auto-populated via elements added to the store array at begin play, while the player inventory is set to populate as you acquire items). To purchase an item, you click on it within the store interface, and hit the “Buy” button. This is where I have hit a wall…

Say, for instance, the player clicks on “item 1” in the store, hits purchase…What would I need to do to send “item 1” from the store array to the inventory array, while still leaving an “item 1” in the store inventory?

How do you save the items in your Inventory?
Normally you should only need to either duplicate the item info or spawn a new one. Then call the AddItemToInventorY
Function on it like you would with a oicked up item.

This is the add to inventory function. I put all “pick ups” into a separate BP, and call the function out of the Character blueprint. I am using the same struct for the store’s inventory, so I would just call this same function when I hit the “Buy” button?

Yes, but it would be a better setup if you would use an Input Parameter. Because right now the actor you want to pickup is always the traced one. So you would need to set that variable from the shop and that is not ideal.

Give that function an Input and use that for the cast etc. Then, where you call that function for you trace, you just pass the Actor traced. With this you can reuse the function for your shop.

You should also think about not saving a struct, but the whole actor and just disabling his model and collision. That is way easier (:

Do you mean not to use a struct at all, or just for the store? I found it easier to use the struct on the inventory itself, as I could set the item details and such for each item on the fly. I tried duplicating the function with an input parameter within the shop, but it is still not outputting to my player inventory. Not sure if I did it right, though. Any chance you could show an example of what you described?

Yes, there is a chance, but i guess tomorrow. It is way too late now in germany. I will get to you tomorrow if it is ok? (:

That would be great! Thank you!

So, i can’t give you a direct example with images, but i will try to explain it (you could watch the Epic Inventory Tutorial to see how they save the actors).

Instead of saving your items as structs (which is ok, but i find it rather complicated), you can make a Array that has your Item Base Class as type. So an array
of pointers (you might want to look up what a pointer is if you don’t know yet). With this, you can directly add the whole actor to the array and access the details
of each item the same way like you already do. Only that you are getting the actor and then the struct from him.

(Note: You don’t need to link every single entry of your Structs with the “Make struct” node. You could directly plug in the struct into the “Add” for your struct array setup.)

Now let’s say you have this Array of Pointers for you Item Base Class. You can save every Child BP, like Food, Weapons etc in this array without changing anything, because
pointers to base classes can also point to the children. You may see why this is an easier setup regarding different item types. Because you don’t need to create one big
struct with all different item information. You just need to create one Base Class with information they all share and then create children of that with the additional attributes.
Good thing here is to create an ENUM with “ItemTypes” for the baseclass. So that you know which item type you have and might want to cast to the child class.

(Note: If you save a child class, like food with additional details, in this array, you can only access the base class attributes. You need to cast it first to the child class to get the
variables and functions that this BP has. To know which item type you have, you can use the ENUM i talked about.)

Ok, so you have your array and now you know how you can use it to save your Items. What you want to do is: If you add an item to the inventory, you might want to set the
visibility of the ItemMesh to false and also disable collision, so that you can’t pick it up anymore. If you want to drop the item, you just need to enable collision, set the visibility
to true and may set its location to the drop point or something.

The function for adding should have the parameter i talked about. Just the BaseClass again. So you can reuse the function for all your adding you want to do at some point.

This point is, for example, your Shop. So how can you now get the shop to work?

You can also create variables with the real class (the purple colored one). Here you can set classes before starting the game, that can be used for spawning items.
Now, you could create an array of these classes. You can selected all the items that the shop should have. When selecting the item and buying it, you just take the
selected slot of the class array and spawn the Item. Then you can just use your Add To Inventory function with the new spawned/bought item and add it.

That’s all. :X

So something like that for your Add Item Function (just a small example. You will need more logic):

http://puu.sh/j73JR/d91f270102.png

And this is the Classes Array, but you can also choose the Item Base class, then you will only get the base class
and childclasses to select and not all actor classes:

http://puu.sh/j73Mg/9999f23e47.png

When the player selects an item, you can save the selected one and do something like this:

http://puu.sh/j73TS/32746b170c.png

You may understand that this is a high complex system and it takes time to create it, so i can’t show you more than this.
As i already said, you can watch the Epic Games Inventory Tutorial to get some ideas about the saving logic.

Hi guys,
I’m posting in this thread because I’m writing a similar in-game trade system :slight_smile: I started working with structures only but now I can’t figure out how to spawn an item in this way. eXi, your approach seems much better, but how do you keep the information about each item (e.g. price, number, icon, item ID)? Do you need another array of structs in this case? If so, how do you make the mapping between the two arrays?

Many thanks.

I just save the complete item actor. It has all the information. No need for an extra array of some kind of structure.

I have tried setting the shop to work in this way, but I am lost on the way to set up the item’s id, details, price, etc. Basically, in the store, if the player clicks on a gun in the list of items available for purchase, they will then be able to see the details of the item (description, thumbnail, etc). With the struct version I was using, I could bind the text blocks. How do I do this with the item class instead? Also, I am wanting the items to be directly added to the inventory, not spawned and then picked up. That just gets messy if you have to drop everything you purchase and then pick each item up. Is your item class way of setting up the shop going to allow me to do that?

Hi i m also new. I like to know how to save all the item, Is anyway you can show on next video ^^’

For the part of “How to save”, you can watch Epics basic Inventory Video Series. (: