Download

Getting regular crashes with UInstancedStaticMeshComponent

Hello everyone,

I’ve recently started with UE4 and today was the fist time I tried working with construction scripts and static meshes instances. But now UE4 crashes whenever I compile my C++ derived Blueprint.

The goal is to make the same procedural generation as the one in this tutorial video : https://www.youtube.com/watch?v=x2EyPNucHK4

Here is my OnConstruction call :



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

    // Do some clean up first
    for (int i = 0; i < LadderStraight->GetInstanceCount(); i++) {
        LadderStraight->RemoveInstance(i);
    }

    for (int i = 0; i < LadderAttach->GetInstanceCount(); i++) {
        LadderAttach->RemoveInstance(i);
    }

    for (int i = 0; i < LadderBottom->GetInstanceCount(); i++) {
        LadderBottom->RemoveInstance(i);
    }

    for (int i = 0; i < LadderTop->GetInstanceCount(); i++) {
        LadderTop->RemoveInstance(i);
    }

    // Now we start the real construction

    for (int i = 0; i < AmountLadderMeshes; i++)
    {
        if (i == 0) {
            LadderBottom->AddInstance(FTransform(FVector(0, 0, i*MeshHeight)));
        }
        else if (i == AmountLadderMeshes - 1) {
            LadderTop->AddInstance(FTransform(FVector(0, 0, i*MeshHeight)));
        }
        else if (i % 3 == 0) {
            LadderAttach->AddInstance(FTransform(FVector(0, 0, i*MeshHeight)));
        }
        else {
            LadderStraight->AddInstance(FTransform(FVector(0, 0, i*MeshHeight)));
        }
    }
}


I’ve tried removing the second for loop and the crashing stops.
I’ve also tried making the same visual script as on the video but the crashing still occurs. :frowning:
So I thought that maybe it’s the UInstancedStaticMeshComponent objects. But I have no idea if it can be them, anyway here is how I make them in the class constructor. Just in case.



    // Setup the meshes
    LadderStraight = CreateAbstractDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("LadderStraight"));
    ConstructorHelpers::FObjectFinder<UStaticMesh> mesh(TEXT("StaticMesh'/Game/Meshes/Ladder_Straight.Ladder_Straight'"));
    if (mesh.Succeeded()) {
        LadderStraight->SetStaticMesh(mesh.Object);
    }

    LadderAttach = CreateAbstractDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("LadderAttach"));
    mesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Ladder_Attach.Ladder_Attach'"));
    if (mesh.Succeeded()) {
        LadderAttach->SetStaticMesh(mesh.Object);
    }

    LadderTop = CreateAbstractDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("LadderTop"));
    mesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Ladder_Top.Ladder_Top'"));
    if (mesh.Succeeded()) {
        LadderTop->SetStaticMesh(mesh.Object);
    }

    LadderBottom = CreateAbstractDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("LadderBottom"));
    mesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Ladder_Bottom.Ladder_Bottom'"));
    if (mesh.Succeeded()) {
        LadderBottom->SetStaticMesh(mesh.Object);
    }


I’ve found the solution!

If it can help anyone it’s actually the missuse of RemoveInstance that’s making the crash. Everytime that method is called the index numbers are re-arranged accordingly, so using those for loops some left overs will always remain.
And since here we are removing all instances ClearInstances() does the job without needing a loop. Sweet!