C++ construction script behaves completely differently to BP construction script

Hi,

I’ve made a simple scattering tool in BP and now I am trying to rewrite it in C++. The entire thing happens inside construction script. The main point of the tool is generating instances using Hierarchical Instanced Static Mesh component.

The way I understand construction script is that each time an actor is interacted with in the scene, either by being transformed or parameters changed, the construction script fires and recomputes from scratch. And that is indeed the case for my BP construction script. Here is a simplified version:


And it works well. Every time I interact with the blueprint, it creates one HISM instance at the world center.

Now here is supposedly identical thing in C++ (with the exception of SM validity check for nullpointer):



void AScatter::OnConstruction(const FTransform& transform)
{
    Super::OnConstruction(transform);

    InstComp->SetStaticMesh(InstancedMesh);

    if (bEnabled)
    {
        if (IsValid(InstancedMesh))
        {
            InstComp->AddInstanceWorldSpace(FTransform::Identity);
        }
    }
}


But it doesn’t work the same way. When C++ one is interacted with, it keeps creating new HISM instances at the world center, but keeping the old ones and moving it with actor. I assumed that construction script first flushes all existing data before firing again. Here’s a video:

How do I make C++ construction script behave like blueprint one? Am I using the wrong method? (OnConstruction(const FTransform& transform))

Thanks in advance

Yes sometimes BP handles stuff for you, in this case the bp construction script is handling the instances array you have inside your component, but c++ does not, so everytime you are executing the construction script you are adding a new instance without cleaning the old instances that were there before. All you need to do in your c++ code is clear the instance yourself, or keep track about how many you have.

Cheers

Thanks!

These things really confuse me. Is there any place where I can find out with certainty how are similar features handled differently between C++ and BP, or do I have to manually find such things out by trial and error approach every time? I mean, I can get over the fact I have to do much more things manually in C++ than in BP, but the issue is many of these things seem to be some obscure knowledge that needs to be found out by wasting a lot of effort instead of derived from some rigid rules.

Would I be right in guessing that in the first case, you added the HISM component to the actor blueprint, whereas in the second, the component was created in C++?

The key difference is in how components added in a blueprint behave compared to those created natively. Blueprint-added components get completely recreated every time the construction script is run (before your custom construction script code, be that C++ OnConstruction or BP Construction Script), with some very complex stuff done under the hood to copy across some, but not all, of their state. C+±created components on the other hand, are just re-registered. It can be entirely dependent on the specific component class as to what is and isn’t maintained by each of these two processes, but generally, BP components will have more of their state reset.

And no, there is nowhere you’re going to find a good explanation of the details of any of this. In fact, I wouldn’t be surprised if there isn’t even a single person at Epic that could explain in detail exactly how it works. Good luck!