Calling construction script through c++?

I am confused as to how to get a similar behavior like the Construction Script of blueprints in my Actor in C++.

I tried using OnConstruction(const FTransform&) according to this link but it doesn’t seem to work when I move the actor. I put some debug logs into the OnConstruction override but it is only called when I drag the actor into the scene. However, if I attach a completely EMPTY blueprint to the actor and then move, it starts updating (I haven’t touched the blueprint at all). So it seems to call the OnConstruction again and again ONLY if a blueprint is attached to it else it doesn’t. Is there any way around it?

Thanks in advance!

How are you spawning the actor?

I don’t have any immediate solution, other than to say, it works for me?

OnConstruction() is always invoked if you call World::SpawnActor. In some cases it’s possible to defer an actor, for example, using SpawnActorDeferred(), as the docs note:

“Spawns given class and returns class T pointer, forcibly sets world position. WILL NOT run Construction Script of Blueprints to give caller an opportunity to set parameters beforehand. Caller is responsible for invoking construction manually by calling UGameplayStatics::FinishSpawningActor (see AActor::OnConstruction).”

So perhaps if you’re doing something odd, you need to call FinishSpawningActor()?

Notice that OnConstruction is invoked at various times; both when you drag it in the editor, and when you spawn an instance of it on the scene.

Also see https://answers.unrealengine.com/questions/165787/aactoronconstruction-called-on-every-property-chan.html

You don’t understand. My problem is not in spawning an actor. It is in UPDATING the actor while moving it around in the Editor viewport (NOT while the game is executed while in “Play” mode).

I had a bit of trouble with my self at one stage. As far as I’m aware there is no way to get a ‘construction script’ like function in C++. That is a function that is called everytime you move or edit something in the editor.

The best work around is to override the PostEditChangeProperty function. This gets called whenever you edit some property of the actor. You can then call whatever code you need to construct the object from here. You can just add some dummy property to your actor with EditAnywhere, when ever it’s changed your construction function will be called.

wow I didn’t know this method existed! I am going to try it out tomorrow and get back to you asap!

Hello! Thank you this method got me on the right track and I found the correct function PostEditMove() function that did the trick in conjunction with the PostEditChangeProperty() function. Thank you again! Cheers!

Override this:

/**
 * Called when an instance of this class is placed (in editor) or spawned.
 * @param	Transform			The transform the actor was constructed at.
 */
virtual void OnConstruction(const FTransform& Transform) {}

The documentation is lacking. This is the actual construction script. It covers PostEditChangeProperty and PostEditMove cases as well.

The down side is that it will not run for non blueprintable object (i.e. volumes).

The up side is that it coversa more than the Post methods. In my case OnContruction was called when I changed the StaticMesh property of o component, but PostEditChange didn’t. (I did not go all the way and test PostEditChangeChainProperty as well)