Multidimensional array of classes

I am having a bit of an odd issue, making me wonder why I cannot insert a class to an array.

Setup:

  • PlayerController has an array of UnitInventory Structure
  • UnitInventory structure has an array of BaseItem classes (defaulted to 5 empty values)
  • Trying to insert a BaseItem class to the array inside UnitInventory does nothing

Here’s the blueprint function that is called at the end of a drag and drop operation in UMG

All of the inputs return the proper values, I’ve tested them with a print string before adding to the array. The foreach loop returns the right amount of values, but, not the updated value that should have been set by the SetArrayElem function.

Thoughts?

When I tried making the struct in c++, for some reason the TArray< class AItem* > only allows for objects of class AItem instead of classes of type AItem.

TArray<TSubclassOf > MyItemList;

Ah, completely forgot about the existence of TSubclassOf, thanks. However, the same problem still occurs, when adding a class of AItem to the array, it will not set using the Set Array Elem node.

The array length is proper, the elements won’t get set.

to set an array of array in blueprints, you need a temp variable array that you can use to make the outer array element:

in C++ this would be easier and more efficient:

void UYourBlueprintFunctionLibrary::SetMy2DItemArray(int32 inA, int32 inB, TSubclassOf<class AItem> InItem)
{
	if (outerArray && outerArray.IsValidIndex(inA) && outerArray[inA].InnerArray.IsValidIndex(inB))
	{
		outerArray[inA].InnerArray[inB] = InItem;
	}
}

i have a feeling this is because setArrayElem() might be creating a new array at a different memory address, instead of editing the array inside the array directly. probably because when allocating the memory for an array of array, the outer array doesn’t have a standard ElementSize, since it changes every time you add to the inner array.

That worked, feels weird for some reason.

if you use SetArrayElement on an array of classes it works fine. but if you use SetArrayElement on an array of array of classes, it doesn’t work. see my other answer to solve the problem with a temp variable.

that weird feeling might be a memory leak, so make a stress test. set up a timer that calls your blueprint function every few frames, and let that run over night and see if it runs out of memory and crashes. if its still running by morning, your weird feelings should go away.

Works fine, ran the test all night, no leaks. I’ll just call it a victory and move on :slight_smile:

I made it more simple:

add:

and remove:

make the same for booth