Simple (maybe) constructor problem.

Hello everyone.

I have a little problem with C++ Actor class.

First, I will tell about the Blueprint test:

  • I create an Actor Blueprint “Actor_BP” and enter in the Blueprint Editor.
  • Edit the Constructor Script.
  • Add in the Constructor Script an UTextRenderComponent with static text in component’s details : “HELLO”.
  • Compile and save the Blueprint.
  • Back to UE4Editor, I drag the “Actor_BP” and put in the Level around 5 instances of it.
  • All of them appeared with HELLO rendered when I drag 'n drop in the world.
  • Now I open the “Actor_BP” and change “HELLO” to “HI !”, compile, save, back to editor and everything is now “HI !”.

Then, in C++ test:

  • I create an Actor C++ Class “Actor_CPP” and open in Xcode/Visual Studio (tested both in Windows 10 and OS X)
  • In the Actor_CPP::Actor_CPP() constructor, I add the following code:

     UTextRenderComponent *textComp = CreateDefaultSuboject<UTextRenderComponent>(TEXT("texthere"));

  • Compile in the IDE, go to UE4 editor, compile in the UE4 editor as well.
  • Drag the “Actor_CPP” and put in the Level around 3 instances of it.
  • All of them appeared with HELLO rendered when I drag 'n drop in the world.
  • Now I edit the Actor_CPP.cpp (C++ class) one more time and change “HELLO” to “HI !”, compile in IDE, compile in UE4 and nothing changed!
  • But, if I drag 'n drop the “Actor_CPP” to the world, it will goes with “HI !”, like the anothers instances of “Actor_CPP” was already compiled, fixed and couldn’t be refreshed/changed like the Blueprint “Actor_BP”.

This is normal? The Blueprints are realtime, but C++ classes not?
There are any way to refresh the “Actor_CPP” without need to remove the instances in World and add it again?

The second problem is:

  • In Blueprint, if I put a variable (editable in UE4 editor) in the “Actor_BP” and then, adding a new function (Set Text) with this variable, then I can go to UE4 and change in Editor’s component details the text from “HELLO” to “HI !” to “WORLD” and it will change the selected instances from “Actor_BP” to the text I input in UE4 editor.
  • In C++, I did a UPROPERTY(EditAnywhere) FText myText; and use as textComp->SetText(this->myText); and doesn’t worked. Why?

Thanks in advance.

If i remember correctly the constructor is only getting called once when the instance of the actor/objhect is created.
If you want that to change every time you could reference the text component in your actor’s class, set it in the constructor and then set the text on begin play.

If I understand your conundrum correctly:

The problem is that the terminology is a bit confusing between BP and C++ here. To make a native class do stuff in the same way that a BP class does in the ConstructionScript graph, you need to override ‘OnConstruction’ in your code. So, your native header should contain a declaration with the following signature:

virtual void OnConstruction(const FTransform& Transform) override;

…and then you provide the implementation code for that method in your .cpp as per usual. For your example to work, the ‘textComp’ should be declared in the header, so your code can access it, and then you’d just call SetText from within OnConstruction instead:

void SomeClass::OnConstruction(const FTransform& Transform)

TheAxcell, thanks for the info, but the problem is that I want at UE4 Editor, not on BeginPlay. :slight_smile:

Xenome, thank you for the info.
I was using SomeClass::SomeClass() {} default C++ constructor, now I’m using OnConstruction like you pointed out.

But, the problem persists.

For make more clear, for test purpose, I want to put a UTextRenderComponent in an Actor (C++) that.
Then I want to instance 10 of this Actor in the UE4 Editor with drag 'n drop to World.
All of these Actors will show in the World : “HELLO”.
When I change the SetText(TEXT(“HI”)); and compile (from UE4 editor), all of this 10 actors will change the text automatically from “HELLO” to “HI”.

In Blueprints this works, but not in C++.
Using the latest version from today (4.12.4) and the same behavior.

Any idea?



Just for update. I could do it, but every time I want to make a change the OnConstruction body, I must change something in the Actor header file !

If I don’t change the header file, only the cpp file, nothing will reflect automatically in the UE4. Only in new instances of that Actor.
To reflect the existing instances of that Actor, I need to change a comment, insert some variable or something in the .h file.

It makes sense, but for me, it’s still a bug.