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?
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;
}