Destroy ActorComponent On BeginPlay

It seems that an ActorComponent cannot call DestroyComponent() in BeginPlay().

There is an assert that checks for a bHasBegunPlay that is set to true when the Super::BeginPlay is called, but if the DestroyComponent() is called then that flag gets set to false. Welcome to an assert.

Basically the functionality I’m looking for is that by default, any Actor can slap my CustomActorComponent on them, particularly by default for designers. But I want to be able to automatically detach the component if it doesn’t meet certain criteria or under specific conditions. For ease of use, my use case was that designer should not have to manually evaluate if this component can be attached in my scenario or inversely, manually removed.

However, if you cannot call DestroyComponent in BeginPlay or even anything prior to it being called, I don’t see an alternative :frowning: There is no such thing as a PostBeginPlay or any hooks I can tap into, and I do not want to wait for it to tick.

Any suggestions?

1 Like

Swap the Super and Destroy calls around?

That then asserts on super::BeginPlay due to a flag being false set by destroycomponent

And what happens if you destroy after super?

After super is what I described in the original post. Another assert.

1 Like

Makes sense - Can you use AActor::PostInitializeComponents?

1 Like

This is ActorComponent and it does not have PostInitializeComponents. Actor will call ActorComponent::RegisterComponentWithWorld which is where the asserts happen. It calls InitializeComponent but its prior to BeginPlay so you get the same assert.

1 Like

Ah yes - My mistake. It looks like the best option is to simply hook into the point after which the actor has loaded the components and broadcast this to its components.