# Calling construction script through c++?

**URL:** <https://forums.unrealengine.com/t/calling-construction-script-through-c/316142>\
**Category:** Blueprint\
**Tags:** unreal-engine, construction-script, editor, UE4, question, Blueprint, CPP\
**Created:** [April 23, 2015, 8:02pm UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142 "2015-04-23T20:02:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SudhanshuAggarwal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SudhanshuAggarwal](https://forums.unrealengine.com/u/SudhanshuAggarwal)\
**Post date:** [April 23, 2015, 8:02pm UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/1 "2015-04-23T20:02:30Z")

</div>

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](https://forums.unrealengine.com/showthread.php?2253-equivalent-of-construction-script-in-c) 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!

---

<div class="post-metadata">

**Author:** ![shadowmint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shadowmint](https://forums.unrealengine.com/u/shadowmint)\
**Post date:** [April 29, 2015, 4:45am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/2 "2015-04-29T04:45:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shadowmint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shadowmint](https://forums.unrealengine.com/u/shadowmint)\
**Post date:** [April 29, 2015, 4:50am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/3 "2015-04-29T04:50:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SudhanshuAggarwal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SudhanshuAggarwal](https://forums.unrealengine.com/u/SudhanshuAggarwal)\
**Post date:** [April 29, 2015, 5:11am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/4 "2015-04-29T05:11:52Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![karltheawesome\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@karltheawesome\_1](https://forums.unrealengine.com/u/karltheawesome_1)\
**Post date:** [April 30, 2015, 5:08am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/5 "2015-04-30T05:08:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SudhanshuAggarwal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SudhanshuAggarwal](https://forums.unrealengine.com/u/SudhanshuAggarwal)\
**Post date:** [April 30, 2015, 7:20am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/6 "2015-04-30T07:20:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SudhanshuAggarwal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SudhanshuAggarwal](https://forums.unrealengine.com/u/SudhanshuAggarwal)\
**Post date:** [May 1, 2015, 1:13am UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/7 "2015-05-01T01:13:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![plojnitza](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plojnitza](https://forums.unrealengine.com/u/plojnitza)\
**Post date:** [March 28, 2016, 2:19pm UTC](https://forums.unrealengine.com/t/calling-construction-script-through-c/316142/8 "2016-03-28T14:19:42Z")

</div>

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)
