Event that is fired when an actor is deleted from editor?

I have an actor that uses AActor::OnConstuction() to perform some setup. When the actor is removed through the editor, I would like to perform some cleanup. Is there an event I can use for this?

I attemped to hook OnDestroyed from the ctor, but it is not being invoked.

this->OnDestroyed.AddDynamic(this, &AMyActor::DoStuff);

Hey jeff2365-

You may want to try using BeginDestory() rather than OnDestoryed(). BeginDestory() is called before the actor is actually destroyed where OnDestoryed() is called when the actor is destroyed, which may lead to issues depending on if the actor still exists when the call is made. You can bind your function as you indicated or you can simply call your function inside BeginDestroy().

Cheers

Thanks. I think I managed to get it working, though it wasn’t very intuitive. Upon deleting the actor in the editor, BeginDestroy() only seems to be called for the transient placeholder instances that are created as you drag your mouse over the viewport to place the actor. After saving the level twice it will then be called for the non-transient instance.

Just for anyone else that sees this, when you use AddDynamic to register a callback, the callback must be decorated with UFUNCTION() otherwise it won’t be called.

Overiding BeginDestroy() crashes my editor anu idea 4.15 vs2013

" Fatal error: [File:D:\Build++UE4+Release-4.15+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 788]
CBaseUnitCharacter /Engine/Transient.BPGC_ARCH_FOR_CDO_CBaseUnitCharacter_1 failed to route BeginDestroy "

Hey -

More information would be necessary to fully understand your issue. Also, to prevent the details of your issue from being lost in this post, and to help others who may be having the same/similar issue, it would be helpful to create a new post. Please be sure to include the full call stack from the crash as well as the code you’re using to override the BeginDestroy() function.

For anyone else who ends up here, AActor::Destroyed() is called when an Actor is deleted in the editor. If you override it you can do your cleanup!

3 Likes

Dude, thank you!! Got stuck on this for days.