skip to Main Content

Construction Script based on C++

Each time you make a change to a Blueprint (for example you drag it to a different position on your map) it’s construction script runs. This script contains code which is responsible for certain initialization operations. Using the construction script can accelerate certain operations, for example you can create an actor which generates random foliage around a point in your map, thus helping you or your level designer.

In this post, I will create a construction script in C++ which will spawn a given static mesh based on given values around a placed actor. Here is the end result:

Preparing an Actor

Add a new C++ class based on the Actor class and inside the header file, type in the following code:

Switch to your source file and inside your constructor type in the following code:

Compile your code an create a Blueprint based on the class you’ve added above. Then, don’t forget to assign a static mesh to the SM paremeter. Now we are ready to create the actual construction script.

Creating a construction script

Go to the header file of your C++ class and add the following declaration:

After that, switch to your source file and type in the following implementation of the above function:

Please note that you may have to change the name of the class, otherwise you will get a compile error!

That’s it! Go to the UE4 Editor, place the blueprint of your actor and temper with the values we exposed. Of course, you can create an improved version of the above script so the static meshes won’t overlap with each other!

Avatar photo

This Post Has 5 Comments

  1. I use : virtual void PostInitializeComponents(); . I do not know if it’s a good option. Is the same or different?

    1. The lifecycle of an actor is as follows: the constructor is first, then the PostInitializeComponents and after the BeginPlay the PostBeginPlay.

      -Orfeas

  2. Hi Orfeas, nice tutorial!

    I’m experiencing a little issue when adding/removing components like you do in the tutorial: it looks like the Details panel for the Actor doesn’t get properly updated after such changes.

    For example, every time I move the Actor or change one of its properties, the OnConstruction function gets called, and I see the Actor’s components named like “Unnamed”. Then, if I deselect and reselect the Actor, everything looks fine.

    Does it happen to you as well?

    1. Hello,

      This tutorial was written using an old version of the engine (4.12 maybe?) which at the time was working fine. I was able to replicate your issue using the 4.14.3 version of the engine. I’m assuming it’s a bug in the current version of the engine.

      -Orfeas

  3. It looks like AttachTo is about to be deprecated. They suggest you use AttachToComponent.
    as far as I can tell this:
    FAttachmentTransformRules AttachmentRules(EAttachmentRule::KeepRelative, false);
    NewComp->AttachToComponent(GetRootComponent(), AttachmentRules, NAME_None);
    Should be the same as this:
    NewComp->AttachTo(GetRootComponent(), NAME_None, EAttachLocation::KeepRelativeOffset);
    But it is not.
    Attach to still works as shown in the tutorial but AttachToComponent does not produce any new static meshes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top