Remove Actor Instance

Hi,

Wondering if anyone has successfully implemented this function in 5.6, and if so you could share your implementation?

Any way I’ve attempted it causes the engine to crash.
I’ve tried various things, generally along the lines of this example, often with additional logic to identify a specific instance to remove. But it always crashes on the RemoveActorInstance call.

UInstancedActorsSubsystem* Subsystem = UInstancedActorsSubsystem::Get(WorldContextObject);

Subsystem->ForEachInstance(QueryBox, [&](const FInstancedActorsInstanceHandle& Handle, const FTransform& /Unused/, FInstancedActorsIterationContext& Context)
{
if (!Handle.IsValid())
return true;
Subsystem->RemoveActorInstance(Handle, false);
});

The errors are always along the same lines of.

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:D:\UE56\UnrealEngine\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 1067]
Array index out of bounds: 418 into an array of size 0

Exception thrown at 0x00007FF9365EA6F4 (UnrealEditor-InstancedActors.dll) in UnrealEditor.exe: 0xC0000005: Access violation writing location 0x0000000000009CC0.
Unhandled exception at 0x00007FF9365EA6F4 (UnrealEditor-InstancedActors.dll) in UnrealEditor.exe: 0xC0000005: Access violation writing location 0x0000000000009CC0.

Interested in hearing from anyone who has successfully implemented this.

I’ve not used this specifically, but I suspect what’s going on is you’re calling something that’s changing the array out from underneath the “ForEachInstance” function. I’m willing to bet it was not written/intended to be used in this manner. Think of just a normal for loop through an array. You wouldn’t want to change the size of the array inside the loop itself. Make sense?

Looking at the API I don’t see a nice way to do what you want, however, maybe you could add the handle to a local array and then iterate over your local array calling RemoveActorInstance? As a side note, your lambda is not returning anything when the handle is valid.