アンリアる! C++入門編 ~対話形式で学ぶUnreal Engine~
BOOTHでUnreal Engine C++入門書を販売していますUnreal Engine上でC++を使って開発するために必要となる基礎知識を一冊の本にまとめた本です。 対話形式によるわかりやすい説明を目指しました。無料の試し読みも可能ですので、ぜひ読んでみてください!
[UE5] Execute Construction Script Intentionally

Construction Scripts are automatically executed when you change the value of an Actor variable or move an Actor placed on a Level.
This is a very useful function for work on the Level Editor, such as changing multiple variables simultaneously.

Although the Construction Script is executed at the specific time, do you want to intentionally (forcefully) invoke it?
For example, when one Actor is executed, another Actor should be executed.

This article explains how to intentionally execute a Construction Script.
Note that this method requires implementation in C++.
We will also explain how to execution in BP (Blueprints) for general use.

  • This method can be executed during gameplay, but it may not be safe.

When is Construction Script Automatically Executed

Construction Script is usually executed automatically by the following operations on Actors (Example).

  • Change the value of a member variable
  • Move, rotate, or expand the Actor
  • Compile on the BP editor

The process executed by Construction Script needs to be implemented in Construction Script on Actor’s BP editor.

Construction Script

How to Intentionally Execute a Construction Script

Now, we will explain how to intentionally execute a Construction Script.
To summarize, it can be achieved by calling Actor’s member function RerunConstructionScripts.

Sample Specifications

We will create the following sample.

  • Create a class ActorA derived from Actor in C++. And, create a class ActorB derived from Actor in BP. Then, place them on Level.
  • The ActorA class has a member variable TargetActor whose type is TObjectPtr (AActor* in UE4). The variable TargetActor is set to ActorB placed on the Level.
  • The ActorA class displays "A called" on the screen when the construction script is executed. And executes the Construction Script of the Actor set in the variable TargetActor.
  • The ActorB class displays "B called" on the screen when the construction script is executed.

How to Execute Construction Script

The class Actor has a member function RerunConstructionScripts to execute Construction Scripts.

RerunConstructionScripts()

This function cannot be called in BP, only in C++.
We will explain how to call it in BP later.

By calling this function, the Construction Script can be executed at any time you want.
By specifying the Actor, it is also possible to execute Construction Scripts of the other Actors.

Sample: Actor Implementation

We will show C++ source code for the ActorA class.
The project name is MyProject.
In C++, the Construction Script need to be implemented in the OnConstruction function.
In the OnConstruction function, call the RerunConstructionScripts function.

Header file.

#pragma once

#include "GameFramework/Actor.h"
#include "ActorA.generated.h"
    
UCLASS()
class MYPROJECT_API AActorA : public AActor
{
    GENERATED_BODY()
    
public:
    virtual void OnConstruction(const FTransform& Transform) override;
    
    UPROPERTY(EditAnywhere)
    TObjectPtr TargetActor;    // TObjectPtr is AActor* in UE4
};

Source file.

#include "ActorA.h"

void AActorA::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

    GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, "A called");
    if (TargetActor) {
        TargetActor->RerunConstructionScripts();
    }
}

Next, we will show the BP of the ActorB class.

BP of ActorB class

Run the sample

Place the created ActorA and ActorB on the Level.
If ActorA created by C++ is placed on the level, it cannot be moved.
So, create a derived class of ActorA by BP and place it.
Set the placed ActorB to the variable TargetActor of the placed ActorA.

Actor Placement and Setup

Run ActorA‘s Construction Script by moving ActorA or changing some variables.
You will see "A called" and "B called" on the screen, and you can confirm that ActorB’s Construction Script is executed at the same time as ActorA.

Simultaneous Execution of Construction Script

As described above, Construction Scripts can be intentionally executed by calling the RerunConstructionScripts function.
In this example, we showed the process of executing the Construction Script from another Actor. But, the RerunConstructionScripts function can be called at any time,

How to Intentionally Execute Construction Script in BP

You think it is inconvenient that Construction Scripts can only be executed in C++.
You can also call it in BP by creating a BP function that calls the RerunConstructionScripts function in the BP function library.

Since we can call the RerunConstructionScripts only in C++, create a BP function library in C++.
We will show an example of C++ source code.
The library function name is MyFunctionLibrary.
It takes an Actor* as input and executes the RerunConstructionScripts function of it.

Header file.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyFunctionLibrary.generated.h"

UCLASS()
class MYPROJECT_API UMyFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable)
	static void RerunConstructionScripts(AActor* Target);
};

Source file.

#include "MyFunctionLibrary.h"

void UMyFunctionLibrary::RerunConstructionScripts(AActor* Target)
{
    if (Target) {
        Target->RerunConstructionScripts();
    }
}

This library function can be used to create the ActorA class.

BP for ActorA class

Thus, it is possible to intentionally execute Construction Script in BP as well as C++.

Summary

The important points of this article are as follows.

  • Member function RerunConstructionScripts of Actor class makes it possible to intentionally execute Construction Script.
  • BP can also intentionally execute Construction Scripts by creating BP function that call the RerunConstructionScripts function.