C++ Spline creates hundreds of meshes

How to do this correctly

This took a while to sort out. I still have actor cleanup issues, but a teleport arc spline is working in c++

I hope this helps someone else trying to implement VR teleport arc in C++.



... H CODE

UPROPERTY()
TArray<class USplineMeshComponent*> AddedSplineMeshComponents;

UPROPERTY()
USplineComponent* TeleportArcSplineComponent;

... CPP CODE

for (int ArcIndex = 0; ArcIndex < OutPathPositions.Num() - 1; ArcIndex++) {

	USplineMeshComponent* ArcSplineMeshComponent = NewObject<USplineMeshComponent>();
	ArcSplineMeshComponent->CreationMethod = EComponentCreationMethod::UserConstructionScript;
	ArcSplineMeshComponent->SetMobility(EComponentMobility::Movable);
	ArcSplineMeshComponent->SetupAttachment(TeleportArcSplineComponent);
	ArcSplineMeshComponent->bCastDynamicShadow = false;

	FVector pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd;
	TeleportArcSplineComponent->GetLocalLocationAndTangentAtSplinePoint(ArcIndex, pointLocationStart, pointTangentStart);
	TeleportArcSplineComponent->GetLocalLocationAndTangentAtSplinePoint(ArcIndex + 1, pointLocationEnd, pointTangentEnd);

	// AGeneratedActor is my actor. It has a variable that holds the mesh: GeneratedActorMesh 
	FTransform SpawnTransform = FTransform(FRotator(0.0f, 0.0f, 0.0f), pointLocationStart, FVector(10.1f, 10.1f, 10.1f));
	AGeneratedActor* ArcSegment = GetWorld()->SpawnActor<AGeneratedActor>(AGeneratedActor::StaticClass(), SpawnTransform);
	ArcSplineMeshComponent->SetStaticMesh(ArcSegment->GeneratedActorMesh->GetStaticMesh());
	ArcSplineMeshComponent->SetMaterial(0, ArcSegment->GeneratedActorMesh->GetMaterial(0));

	ArcSplineMeshComponent->SetStartAndEnd(pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd);
	ArcSplineMeshComponent->RegisterComponentWithWorld(GetWorld());

	AddedSplineMeshComponents.Add(ArcSplineMeshComponent);

}