How to use OnConstruction in C++?

The Constructor and OnConstruction() are different things. Since you’re using ConstructorHelpers, I can tell the function in called by the constructor, i.e. by your AMyCharacter::AMyCharacter() function.

OnConstruction() is a separate function that you can override, and it corresponds to blueprints’ ConstructionScript.

.h:

void OnConstruction(const FTransform &Transform) override;

.cpp

void AMyCharacter::OnConstruction(const FTransform &Transform)
{
    Super::OnConstruction(Transform);
    //Your Logic Here
}

Try using OnConstruction() for your materials, but leave ConstructorHelpers stuff in the Constructor, obviously.

Plus, note that every variable you change OnConstruction() must be UPROPERTY(), otherwise it will be defaulted on BeginPlay();

8 Likes