Download

Array of Struct - Filter content to another Array by keep the same struct

Hi,

I face an issue yesterday and I wasn’t able to get it sorted “easily”.

Let’s say you have


TArray<FMyStruct> MyArray;
TArray<FMyStruct> MyArray2;

You do a Range loop like


for (FMyStruct& Item : MyArray)
{
// Here Item can be modified and the element in the MyArray will be updated due to the "&"

//Add only elements that are active to MyArray2
  if (Item.IsActive)
{
MyArray2.Add(Item);
}
}

The issue here is that MyArray2 will contains copy of Struct of MyArray, so if I change some struct data in MyArray2, it won’t be reflected in MyArray.

Do you know how to solve this issue?

the only way I found was to create MyArray2 as TArray<FMyStruct*>

Hi! Yep, MyArray2 contains copies of FMyStruct. You can’t store references in the array. So the only solution is to store pointers in the array. For example:

TArray<TSharedPtr<FVector>> temp;
TArray<TWeakPtr<FVector>> temp1;
TArray<FVector*> temp2;

Preferable:

TArray<TSharedPtr<FMyStruct>> MyArray; // main array
TArray<TWeakPtr<FMyStruct>> MyArray2; // secondary array

Thanks for the clarification.

Do you know if any of
TArray<TSharedPtr<FMyStruct>> MyArray; // main array
TArray<TWeakPtr<FMyStruct>> MyArray2; // secondary array

are compatible with Blueprint?

TArray<FVector*> temp2; is not but I don’t know for the others.

Pointers to Structs are unfortunately not allowed in Blueprint, in any form. That’s my experience of it anyway.