Properly delete actor from level

I am trying to delete an actor from a level with this:

world->ContainsActor(actor){
   world->DestroyActor(actor, true, true);
}

And immediately after, I am trying to spawn the actor with same name with related actor factory.

I get error like this:

An actor of name 'SomeActorName already exists in level ‘Level SomeLevelPath’.

Following seems to fix the problem. But if I change actor settings in the editor and re-run the same code again, I still get the error:

    world->ContainsActor(actor){
      	world->DestroyActor(actor, true, true);
	      world->ForceGarbageCollection(true);
	      world->PerformGarbageCollectionAndCleanupActors();
    } 

What is the proper way to delete an actor from a level, so that i can immediately after spawn an actor with the same name?

1 Like

Well, thank you for teaching me something then!

Thank you for your quick answer,

Actually what “actor->Destroy()” does is inside the function: “actor->GetWorld()->DestroyActor()”. So they effectively seem to do the same thing. I tried it and the result is same.

It seems to be about garbage collection. Forcing garbing collection does not seem the work in some cases as in my original post.

I think I will solve my problems by just enumerating actor names like Actor001, Actor002. And I will let the garbage collection collect its garbage whenever it desires.

1 Like

I don’t do a lot of C++, so I could be very wrong here… but I could have sworn the language to destroy was:

YourActorHere->Destroy();

Like, you get the actor and then call the destroy function. I don’t think the name of the actor goes in the parentheses. Does that not work?

I am facing similar problem. Do we have any resolution figured out ?
Thanks for the help in advance !