So I have an array of actor references that I want to remove an instance of. So I have these actors as a pickup item and I have it set up in code that when I run into it’s position the visual instance is removed but when I go back I still hear my pickup sound. Meaning that it still exists. I have tried to use remove method. The compiler says that remove() is not a part of this. Is there anything else I could use?
Can you share the cpp?
if (keysRef.Num() > 0) {
for (int i = 0; i < keysRef.Num(); i++) {
if (keysRef[i]->GetActorLocation() == FVector(newx, newy, 0)) {
keysRef[i]->Destroy();
cont.currentKey++;
haskey = true;
playPickup = true;
break;
}
}
}
Unrelated to your issue: don’t think you need this conditions since loop will simply do nothing if the array is empty.
You won’t get consistent results (if any) by comparing doubles. Try using FVector::PointsAreNear().
Try this if you don’t care about the order of items in the array:
// Iterate TArray<TObjectPtr<AActor>> MyArray;
for (auto Iterator = MyArray.CreateIterator(); Iterator; ++Iterator)
{
AActor& Element = **Iterator;
// Check distance to destroy and remove from array.
if (FVector::PointsAreNear(Element.GetActorLocation(), FVector(newx, newy, 0), 0.1))
{
// Destroy actor.
Element.Destroy();
// Swap to back and pop from array. --Iterator so next loop we get swapped element.
Iterator.RemoveCurrentSwap();
/* */
break;
}
}
Let me know if it helps.
This works great but what if I am using GetAllActorsOfClass()?
I also have another problem I want to get rid of an actor reference, but I have an integer that is lined up with it
so I have these:
TArray<AActor*> doorsRef;
TArray<int> doornums;
I have an integer here because I need to know if the key opens the right door.
Can you give more details on how it is supposed to work?
If I had to guess: player overlaps an actor, stores an ID that is later is used to trigger something on another actor?
What I mean is that when I delete the doorRef I need to get rid of the doornum that would be at the same index if it were doorRef[i].
GetAllActorsOfClass() returns an array of type AActor*, there is not difference as far as the loop is concerned.
With the example above you can get the current index with Iterator.GetIndex(). Should look something like this: MyIntArray.RemoveAtSwap(Iterator.GetIndex());