mane
mane

Reputation: 3

How to Unreal Engine C++ CustomStruct RemoveAt or Remove UE4

#1 or #2

The function is not working but It works fine with inEditor Blueprint (Write operator==)

.h 
TArray<FStruct> StructArray

.cpp
void Func(FStruct struct_2)
{ 
  const uint32 Index = SturctArray.Find(struct_2); // Always Value = 0
  if(StructArray[Index] == struct_2)
  {
   StructArray.RemoveAt(struct_2) // #1
   StructArray.Remove(Index) // #2
   }
}

Upvotes: 0

Views: 1072

Answers (1)

KompjoeFriek
KompjoeFriek

Reputation: 3885

Calling Func will create a copy of the FStruct object passed in. StructArray will never contain the copy that was made for function Func. To make that work, have Func use something that does not create a copy. Like a reference.

Don't use the result of TArray.Find() before checking if it is valid.

TArray.RemoveAt() expects an index, your code supplies it an object.

TArray.Remove() expects on item of the same type used to declare the TArray, your code supplies it an index.

#1

void Func(FStruct& struct_2)
{ 
  const uint32 Index = StructArray.Find(struct_2);
  if (Index != INDEX_NONE)
  {
    StructArray.RemoveAt(Index); // #1
  }
}

#2

void Func(FStruct& struct_2)
{ 
  StructArray.Remove(struct_2); // #2
}

Upvotes: 1

Related Questions