Can't spawn actor component at runtime

My game uses a self made inventory system, designed as an actor component, so I can easily attack it to other actors and keep the number of actual actor instances as low as possible.

In a function I use, I need to make a temporary instance of the inventory component to make changes there and later copy it back.
As far as I know I have to spawn actor components via the Create Object From Class function. However, when I do that, I get a wrong class to spawn compilation error.
Am I missing something?

if the engine gives you that error, that means your class must be based on an actor, and actors must be spawned /exist in the world.

you could spawn from class and hide the object, or you could make a local variable that is an array to use temporarily, as I think that is what you are trying to do.

now if I could offer a different way of doing things, you could make one blueprint object that has an inventory, and functions to work with the inventory, and just make a child blueprint from that for each different kind of thing with an inventory. that way, everything uses the same functions and arrays easily.

No, that’s not what I’m trying to do.
I have to make a copy of the Inventory itself, to make non destructive changes to it (reordering the items in the inventory), and then replace the used inventory with the temp copy.

And I can’t spawn fro class, since an actor component does not allow that, since it isn’t an actor.

well then, in that case, you should make a local array, and make a macro to copy your custom struct across to the other temporary array, since unreal engine tends to move things rather than copy them.

in short, I think you have to work around the construct from class node, instead of using it, for this situation you’re in.

But then again, that is also not a good solution. The custom struct is only there to give me a list of all item types in the array, over which I iterate and then add them

Making a copy of the array and then doing stuff, is certainly doable, but I’d have to write the same code I have written in other places again, which totally violates any OOP principle out there.
If I could create a new blank instance of the actor component I could reuse 95% of the code I need to get the logic to work.

Doing it the way you suggested is really the last thing I want to try. If i have learned anything in over a decade of programming work: It’s reuse everything, which I can’t do here.

I don’t know the requirements of your inventory, but here is what I did to achieve the same thing (an inventory that can do whatever I needed) :

I made an inventory struct,and a blueprint object that implements functions for handling it’s inventory. I then made objects that are children of this. that way, if I made a function on the inventory blueprint parent, all inventory objects get that function.

nothing has to be done twice. it’s as simple as adding a new child blueprint. your array should be entirely of structures. they are “figments” of the items. then when you want to display an inventory, you can have a widget display the items based on the structure. this is very object oriented, as you mention.

It seems, you are not getting my problem here.
What you are describing is nice and all, but it’s totally beside the point. I don’t need multiple inventory types (even if I do, this function has to be present in all inventories), I need the reordering function (i.e. merge all items into the least possible amount of items and reorder them based on a certain criteria) to work.

And my inventory already works with structs (you can see it, it’s called S_InventoryStructure).

I understand your problem. I’m sorry it does not seem that way, perhaps I can elaborate better. A parent/child blueprint system would achieve all of this.

You create one parent blueprint, which is an inventory blueprint, which implements all of your functions. (you could even use an function library if you wish, although I personally do not use that route.)

This would be one type of inventory, not multiple inventory types as you mention. The only reason for the parent is so that you don’t have to make the same functions over and over. You would only do your reorder function on the parent, and use it everywhere you need an inventory.

As for your reordering function, your inventory structs could contain the quantity, and the reordering function would look at each struct quantity, and match them up. If you do not want to do this on the original inventory array as you’ve said previously, you could make an extra array in the reordering function as a local variable, and copy everything to it and do it from there in the same function, and then just return the new array, or move the new array to the place of the old one.

You already have the parent blueprint in your pictures, “BP_Inventory”. You just need the items it contains to actually be an array contained within the blueprint. This should be an array of structs, and another “inventory” would be another array of structs. You should not have the inventory constructing/spawning itself. “Temporary inventory” should be an array variable, the same as the array in the top half of your picture. It should be doing all the work on its own blueprint.

I’d also suggest a way to reorder the inventory/help with a function for that, but without seeing more of the inventory system, I cannot suggest a way that would work with what you already have.

No, you don’t get what I need to do. Everything you describe I already have and had all along.

And I can’t simply make a temp array, since the way items are stored in the actual inventory array. The array I have in my screen is only to get the item types nothing more. But here are some consideratinos:
a) There might be two full stacks of an item, but on the end of the inventory. The reordering function would move them to the beginning.
b) I have to merge partial stacks.
c) I need to change the index based on a criteria (e.g. value)

If I could create a new inventory altogether I could simply do a foreach, get the total number of items for a class. Then I could call the AddItem function and lastly swap the items based on the criteria with the SwapPositions function.
Lastly I swap out the actual inventory with the temp inventory and be done with it.
The reorder function would be super short and no double code.

But for that, I cannot simply make a temp array. For that to work I’d have to write some of the same code I already have in AddItem, just for this function.

The thing is: It works (with a different spawning function) if the inventory is an actual actor. But I need to have an actor component for performance reasons.

You could just run a for each on every item, and rebuild the stacks in the temporary array, and then implement a function for swapping the temporary array with the old one. It should be able to manage its inventory with temporary variables, instead of a temporary side object.

That said, if you really want what you’re currently doing to work with little changes, that construct bp_inventory node would work if the bp_inventory blueprint was not an extension of the actor class.

see this link for details on that:

You don’t understand his problem at all.

Actor component can not be spawn via blueprint, this is the issue that have persisted for 2 years.