How can i spawn an actor out of an array (inventory)

So hey there,

I need your help at this point cause im not so familiar with c++ or blueprints.

What ive done until now is creating a blueprint called BP_Item which represends items that can be picked up and droped down.

I manage the pick up by creating an array of that blueprint and adding it to it. Than i delete the actor out of the scene.

My problem comes up when i want to spawn this actor now because i can only find the spawn actor from class function. Since the array provides BP it cant be combined by it.

Also i want to create children of that item blueprint which also should be added to the array and be spawned out of it again.

I found a solution where i created an inventory blueprint which contained an actor variable with the blueprint as value. And spawnt it from this with using the inventory class. I got that from another user an i dont get that at all.

I just want to save the different bp_item_childs in an array and spawn them out of it. I hope you have a simple way to explain me what im doing wrong.

Since im posting from my smartphone i can provide pics of the blueprints tomorrow if you need them.

Thanks for your answers (:

Ask yourself first if there is anything you need to save with the actual blueprint instance which is picked up. Does it contain values that is unique and must be stored? If not, you only want to keep track of which items you have in the inventory. For example, why not store arrays of identifiers? (numbers, strings or class’) For the player it will not look any different if you delete the item and then recreate it again when you want to spawn.

Just as an example, you could create a class array for your items:

Items: (array)

  • 0: class’BP_Potion’
  • 1: class’BP_Sword’
  • 2: class’BP_Shield’

When you pick up an item, you only store the ID of that item. So your inventory array could be:

Inventory: (int array)

  • 0: 0
  • 1: 0
  • 2: 1

(which is potion, potion, sword)

So when you want to spawn that item you use your inventory ID to get an index in the class array, then spawn that class. Open the image below in a new tab and you will see what I mean.

There is no need to store the actual object unless there is something unique about it. Using identifiers is a common way to simplify data flow, very common in databases to mention one example.

First, thanks for that nice answer!

So the item class contains all my items, which i made for the game. From 0 - n-1 it would hold different items.
If i now pick up a potion, i would save the array index of Item class to the Item Inventory array.

What if i want to save things like ammo and save the specific amount of bullets it got? There i would need to save the actual actor with its values or?
The default values, like a picture for the inventory UI or the ability to use the item, can be stored with the array by just connecting the 2 arrays like you did in the picture right? I would just check if the item at place e.g. 5 in the Item Class array can be used and than do what i has under the “OnUse” function. That sounds simpler than i tried to archiev it.

But still, if the item that i pick up has a dynamic value (dont know if its called like that), so e.g. i pick up an ammo clip with 6 bullets in it and if you shoot with the weapon, it checks if there is ammo in the inventory and would need to check and change the bullet value.

How would i save this type of item? Saving this in a different array which really holds the actor?
If i want to drop it than, could i store the actual index which the item has in the Item Class array to it and spawn it from getting the index from that item? After that i would link the actual bullet value to the output object of “Spawn item from class”.

I will test it, but await your answer, cause i normaly think to complicated.

Thanks again for upcomming answers (: And sorry if my english is hard to read, cause im not a native speaker ):

It is hard to give more answers for your inventory system as we have not set a total baseline for features. Do you have another game you can compare to so we can know everything you want to implement? For example, is it like Diablo where items are stored in slots and where you can divide the same type of item into groups? (think potions and gold) Is it simpler than that, where you only need to know if you have a type of item, and how many?

If the last piece of the puzzle is to know the number of each item, you can just add a third array containing the amount for its id.

  • Items (class Array)
  • Inventory (int Array)
  • Number of Items (int Array)

Let’s say you have in Inventory: (same as before)

  • 0: 0 (potion)
  • 1: 0 (potion)
  • 2: 1 (sword)

You can from Number of Items get the array index from Inventory OR Items. It depends on if you want to just store one shared value for all of the same items. If you want to have split groups of one items, for the sword as example, it is number 2 in the list. NumberOfItems(2) would then contain how many swords in that group that you have.

Going further than this may need a redesign of the system, as all pieces need to be taken into consideration. For a smaller inventory, a setup like this is enough. Going Diablo style however will need something different.

In my example in the previous post, I had an InventoryManager blueprint. I guess a more advanced version could utilize sub-managers. Also, instead of having a class-array for the items, you could instead have an array of item managers. That way you could access item specific behaviours, for example cool downs and other neat tricks.

To be honest I have not made an inventory manager before, I’m just referencing previous work I have done with relational databases. To me that feels most natural to do, but it may not be how game developers usually tackle this problem.

Thanks for the answer. My Inventory system should do the following:

store item and its individual values (like name, functions, stackable or not, intern amount like ammo from a magazin)

Its not that much i want to do with it. I just want the player to collect several different items which either can be consumed, placed or combined.

Its like minecraft mixed with dayz if you want an explanation.

By now i managed to pickup and spawn the items as well as track the name of them in a textbased inventory showing me what items i got at the moment.

I did this with a class array, which contains the BP_Classes, an int array which stores the index of the item in the class array and an array of the parent type of all items to store them and their variables.

So for picking up my items, i compare the hit actor with the ones in the class array and save the matching index in the int array. I also save the whole actor in my 3rd array to save the values. To show the name of the item in my inventory i go through the 3rd array which contains the whole object and draw the name as string.
To drop i look at the current selected index in the int array and take the number to get the matching class out of the class array. After that i spawn from the class array and delete the number and the object out of the int and the 3rd array.

That works at the moment. I dont know if i can improve that.

To track the amount of items i want to stack, i would just check if my item is already in the inventory and if its stackable. If both things are true, i would not add it, but increase the amount in a 4th array.

Is this to much array thinking of me there?

I guess, that if it works, use it. There may be better solutions out there, but the best I assume is the one that does what you want. :slight_smile: