How to add a splinemeshcomponent in a function

Hi,

I’m trying to re-create Zak’s great live session of yesterday, but then in C++. At some point Zak is adding a SplineMeshComponent inside a function. Now I know how to add an object or subobject in the cpp constructor using the PCIP, but I cant figure out how to do this inside a function. I’m assuming I’ll be calling the function in a loop inside the OnConstruction override later. So do I then need a reference to the PCIP? Or do I have to do this another way?

Ok, So I updated my function. But it crashes the editor when using the line:

SplineMeshComp->SetStartAndEnd(StartLocation, StartTangent, EndLocation, EndTangent);

The function is the following. Any idea why that line would crash the editor?:

void APipesGenerator::BuildElement(int32 LoopIndex, UStaticMesh * StaticMesh){
    
    int32       CurrentLoopIndex;
    int32       NextLoopIndex;
    FVector     StartLocation, StartTangent, EndLocation, EndTangent;
    UStaticMesh * Mesh;
    USplineMeshComponent * SplineMeshComp;

    CurrentLoopIndex = LoopIndex;
    Mesh = StaticMesh;
    
    NextLoopIndex = (LoopIndex + 1);
    if(NextLoopIndex > NumberOfSplinePoints - 1) { NextLoopIndex = 0; }
    
    // Store data for each element
    // Start location & Tangent
    SplineComp->GetLocalLocationAndTangentAtSplinePoint(CurrentLoopIndex, StartLocation, StartTangent);
    
    // End location & Tangent
    SplineComp->GetLocalLocationAndTangentAtSplinePoint(NextLoopIndex, EndLocation, EndTangent);

    // Store data to build element
    SplineMeshComp = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass());
    SplineMeshComp->ForwardAxis = ESplineMeshAxis::Y;
    SplineMeshComp->SetStaticMesh(Mesh);
    
    // Crashes Editor....
    SplineMeshComp->SetStartAndEnd(StartLocation, StartTangent, EndLocation, EndTangent);
    SplineMeshComp->AttachTo(RootComponent);
}

After some more research, I found a forum post by JuddC where they also created objects in the OnContruction. Turns out that works for me too. So here’s the working function:

void APipesGenerator::BuildElement(int32 LoopIndex, UStaticMesh * StaticMesh){
    
    int32       CurrentLoopIndex;
    int32       NextLoopIndex;
    FVector     StartLocation, StartTangent, EndLocation, EndTangent;
    UStaticMesh * Mesh;
    USplineMeshComponent * SplineMeshComp;

    CurrentLoopIndex = LoopIndex;
    Mesh = StaticMesh;
    
    NextLoopIndex = (LoopIndex + 1);
    if(NextLoopIndex > NumberOfSplinePoints - 1) { NextLoopIndex = 0; }
    
    // Store data for each element
    // Start location & Tangent
    SplineComp->GetLocalLocationAndTangentAtSplinePoint(CurrentLoopIndex, StartLocation, StartTangent);
    
    // End location & Tangent
    SplineComp->GetLocalLocationAndTangentAtSplinePoint(NextLoopIndex, EndLocation, EndTangent);

    // Store data to build element
    SplineMeshComp = NewObject<USplineMeshComponent>(this);
    SplineMeshComp->SetStaticMesh(Mesh);
    SplineMeshComp->SetStartAndEnd(StartLocation, StartTangent, EndLocation, EndTangent);
    SplineMeshComp->AttachTo(RootComponent);
    SplineMeshComp->RegisterComponent();
    SplineMeshComp->bCreatedByConstructionScript = true;

}

You have to add this line immediately after you are initializing it.

SplineMeshComp->RegisterComponentWithWorld(GetWorld());