OnComponentCreated not being called on UActorComponent

I have a component which I have added to a BluePrint actor. I need to perform initialisation, in the component, based on settings that have been set and on geometry that it references.

Doing this in the C++ Constructor doesn’t work because I find the pointers to the assets are all NULL.

I was doing the initial set up in OnComponentCreated which allowed me to perform my own processing. However this seems to have mysteriously, recently, stopped being called in game. The constructor is still called though.

When I edit the Blueprint, changing values, or switching things on or off, then I see the constructor is called and then OnComponentCreated is called successfully.

However if I PIE then the constructor is called but the OnComponentCreated method is never called. This means that the game crashes because the component is not set up correctly.

Is there something, somewhere, I need to click to ensure OnComponentCreated is always called after creation? Switching “Auto Activate” on or off in the component seems to have no effect.

In the end I got around this by using the OnRegister() method instead. However it’d still be interesting to know why OnComponentCreated isn’t called and to hear from veterans as to where custom initialisation code should go.

OnComponentCreated is only for when the component is created, i.e. it is placed in world in the editor or dynamically created in the game. Starting up PIE or running the game and loading an existing component is not creation.

InitializeComponent is probably what you want, it is roughly the component equivalent of BeginPlay. Keep in mind that bWantsInitializeComponent needs to be true for your component type for this to get called.

OnRegister can work for some cases, but you need to be cognizant that registration can be rerun periodically for a variety of reasons mostly to do with the render and/or physics state having been torn down, something being changed, and then the component reregistered rebuilding it.

1 Like