Download

BP Construction after C++ Construction

I have a C++ pawn subclass with OnConstruction overidden:


void AMyPawn::OnConstruction(const FTransform& transform)
{
    UE_LOG(LogTemp, Warning, TEXT("OnConstruction"))
    Super::OnConstruction(transform);
}

In the Editor, I’ve created a Blueprint from AMyPawn, and created the following Construction script:

bp_construction.png

When the construction scripts run however, this is what I see in the log:

construction.png

The order of the messages is unexpected. I would expect my parent class’s OnConstruction method to behave like a C++ constructor and run before the subclassed construction script, or at least when I explicitly call Super::OnConstruction, but this doesn’t seem to be the case.

The underlying problem that led to this is that I’m creating a procedural mesh in C++ OnConstruction, but I’m setting its material in the BP subclass. I can’t do this however because my OnConstruction uses UKismetProceduralMeshLibrary::CopyProceduralMeshFromStaticMeshComponent, and that blows away an existing mesh on a procedural mesh component and any material data set by the BP construction script. Basically I need C++ OnConstruction to run first, set up the procedural mesh, then my child BP construction script to run, and assign the material to that procedural mesh.

Is it incorrect to use both a BP and a C++ construction script? Is there a better way to approach this?

I’ve never had to use OnConstruction in c++. Any reason you don’t use the class constructor or e.g. PostInitializeComponents?

My pawn is dynamic and changes with a number of different properties, so putting my procedural mesh logic in OnConstruction makes it respond to those property changes. IIRC PostInitializeComponents doesn’t run on each property change.

You don’t really need OnConstruction() to catch editor changes to properly tho.

True, and I do have separate code in a PostEditChangeProperty method, but I would need to copy (or refactor) a non-trivial amount of logic from OnConstruction to also be called in PostEditChangeProperty with an attribute changes. I also don’t need it to be rebuilding in real-time, like when a slider is dragged, which is something PostEditChangeProperty gets you. So It makes more sense in my case to be rebuilding the object from scratch once when my values changes, and so my logic is in OnConstruction.

I am still interested in how other people deal with both a BP construction script and a C++ OnConstruction though. It seems to be a broken pattern… if a parent class sets up components in C++ OnConstruction, the child BP cannot access those components in its construction script, because it runs before the parent.

Things like this have always a reason buried inside some of Epic’s game projects somewhere.
Somebody decided it must be this way for some reason, I don’t think something so obvious would pass unnoticed for so long.

Btw that’s a bigger problem because if you submit a bug report it might not be fixed (“working as intended”).
But I would submit a bug report anyway.

Good idea. I’ll update the thread with the ticket number when it gets approved by Epic.

Still in the process of getting the bug confirmed. The bug seems to be inconsistent. I captured a bizarre video of the bug in action. First, the construction order is fine, then for no reason at all, the construction order reverses itself.
https://youtube.com/watch?v=xBLoNpVWqlg
Attached the project files below if anyone else wants to confirm. Maybe it is only a 4.22.2 Linux issue?