How to run code when a blueprint of an Object is instantiated?

I notice a pattern begins to emerge in the construction code for my UObjects. Often these objects are not safe to construct by the reflection system, and heavily rely on the editor to work properly.

As an example, I am creating a custom animation instance class, in which I attempt to construct a UObject that encapsulates an animation sub-system. The catch is, such system requires the presence of a skeletal mesh. This works nicely when you create a BP of the animation instance because it is always tied to a skeletal mesh; however, a crash is inevitable if the code runs earlier when these properties are not set (i.e when reflection runs the default constructor on editor startup).

In order to avoid that, I find myself using this if-statement in the constructor of my UObjects.



    if (!HasAnyFlags(EObjectFlags::RF_ArchetypeObject | EObjectFlags::RF_ClassDefaultObject))
    {
        // Actual construction code
    }


I want to avoid having to rely on that check - it places a burden on anyone using the code to actually make that check and furthermore, I do not know if it is 100% reliable. For all I know, some environment variables or flags might be different when running on the built product, and my checks may fail.

Any suggestions on this issue? Perhaps I need a change of paradigm, or maybe this kind of check is indeed the way it’s meant to be?
I could not find an overridable initialize function that occurs when a BP is created, but the closest thing I can find is yet another check…


if([IsInBlueprint()](https://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/UObject/IsInBlueprint/index.html))
{
   // Initialize stuff only if it's a BP instance?
}