Get Length of Array of Struct Arrays

Hi All,

I need to build, and then sort, an integer array.
What I have is an array of nested arrays. I want to add the length of each nested array to the int array, but the following error occurs:
E0135 class “FOPTPLacementArray” has no member “Num”

I couldn’t find a solution, any help is appreciated!

The Structures

USTRUCT(BlueprintType)
struct FOPTPlacement
{
GENERATED_BODY()

UPROPERTY(BlueprintReadWrite)
	UGridPanel* GridPanel;

UPROPERTY(BlueprintReadWrite)
	bool Column;

UPROPERTY(BlueprintReadWrite)
	int Index;

UPROPERTY(BlueprintReadWrite)
	float Length;

};

USTRUCT(BlueprintType)
struct FOPTPLacementArray
{
GENERATED_BODY()

UPROPERTY(BlueprintReadWrite)
	TArray<FOPTPlacement> OPTPlacements;

};

The Code in the .cpp file
TArray<int> LengthArray;

for (auto& elem : ArrayOfOPTPlacements)
{
	LengthArray.Add(elem.Num());

};
1 Like

Thanks a lot for the reply. Unfortunately, your approach gives me the following three errors:

Error Code Description
C2039 ‘Num’: is not a member of ‘FOPTPLacementArray’
C2819 type ‘FOPTPLacementArray’ does not have an overloaded member ‘operator ->’
E3364 operator → or ->* applied to const FOPTPLacementArray instead of to a pointer type

In the Source Code, Unreal is using .Num() preferably. The issue is that I have to link the “array element” to the “TArray” it solely consists of…

I think I solved the mistery:

.h File
USTRUCT(BlueprintType)
struct FOptPlacement
{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadWrite)
	bool Column;

	UPROPERTY(BlueprintReadWrite)
	int Index;

	UPROPERTY(BlueprintReadWrite)
	float Length;

	// Constructor, only for debug purpose
	FOptPlacement()
	{
		Column = false;
		Index = 0;
		Length = 0.0f;
	}
};

USTRUCT(BlueprintType)
struct FOptPlacementArray
{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadWrite)
	TArray<FOptPlacement> OptPlacementsArray =
	{
	// Adding three values, only for debug purpose
		FOptPlacement(),
		FOptPlacement(),
		FOptPlacement()
	};
};
.cpp File
	// This must be be inside a function
	TArray<int> LengthArray;
	FOptPlacementArray OptPlacementsArray;

	for(auto Element : OptPlacementsArray.OptPlacementsArray)
	{
		LengthArray.Add(Element.Length);
	};

	// Should log a red 3 in the output log	
	UE_LOG(LogTemp, Error, TEXT("%d"), LengthArray.Num());

image

2 Likes

Thanks really much for the time you are taking for me, it is much appreciated!!

I see what you did here, my sincere apologies, I forgot to clarify. The Struct contains a var “Length” for me to determine the optimal placement later on. The “Length” I was referring to was the number of array elements inside the array of arrays.

But your approach might not be that far off. In BP I have to “split” the array element to get access to the nested array. I need to incorporate this “split” action in c++ and this might be through the 3rd line of your code.

FOptPlacementArray OptPlacementsArray;

I need to show the code that the n-th element of the “FOPTPLacementArray” is an array of type “FOPTPlacement” which I am relatively sure, it doesn’t know right now…

1 Like

Ooops. I didn’t notice it because I didn’t write it :sweat_smile:
I trusted Git Copilot too much and I didn’t read the struct variable names :rofl::rofl:

I’ll try again because I’m curious to sort this out

1 Like

I think I am missing something:

We obviously can’t get the .Num() of the Element, because it is not an array!

Can you please explain again what do you need to obtain?

2 Likes

It is! But the code doesn’t recognize it as such xDD

“FOPTPLacementArray” contains Arrays of “FOPTPlacement”. But if we run a for each loop, it will get the n-th element of the “FOPTPLacementArray” which is a struct. But the only member of this struct is an array of “FOPTPlacement”.
Theoretically, if I take elem[0] it should give me that array, but

E0349 no operator “” matches these operands

I am currently trying to do:

TArray"<“FOPTPlacement”>" elem2 = elem;

but it doesn’t know how to convert…

1 Like

Solved it LOL. Instead of trying to access the 0th element of the struct, just access the struct property directly and place it into a new variable with matching type. Then, I can get the .Num() from the new variable.

for (auto& elem : ArrayOfOPTPlacements)
{
	TArray<FOPTPlacement> elem2 = elem.OPTPlacements;
	LengthArray.Add(elem2.Num());

};

@Ares9323 I appreciate your help very very much!

1 Like