I made some changes to the code but I didn’t test them, they ended up being similar to what @3dRaven said:
void AMeshSplineTool::SpawnMeshAlongSpline()
{
const float SplineLength = MySplineComponent->GetSplineLength();
const float MeshSize = 10.0f; // Using Blueprint as example, dont know where the value comes from.
const int LastIndex = FMath::TruncToInt(SplineLength / MeshSize);
UE_LOG(LogTemp, Warning, TEXT("LastIndex = %d"), LastIndex);
for (int Index = 0; Index < LastIndex; ++Index)
{
if (Index * MeshSize + MeshSize <= MySplineComponent->GetSplineLength())
{
FVector StartPos = MySplineComponent->GetLocationAtDistanceAlongSpline(Index * MeshSize, ESplineCoordinateSpace::Local);
FVector EndPos = MySplineComponent->GetLocationAtDistanceAlongSpline(Index * MeshSize + MeshSize, ESplineCoordinateSpace::Local);
FVector StartTangent = StartPos.GetClampedToSize(0.0f, MeshSize);
FVector EndTangent = EndPos.GetClampedToSize(0.0f, MeshSize);
FString Name = FString::Printf(TEXT("MySpline_%d"), Index);
USplineMeshComponent* Spline = NewObject<USplineMeshComponent>(this, *Name);
if (Spline && MeshAsset && MeshAssetMaterial)
{
Spline->SetMobility(EComponentMobility::Movable);
Spline->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
Spline->SetStaticMesh(MeshAsset);
Spline->SetMaterial(0, MeshAssetMaterial);
Spline->SetStartAndEnd(StartPos, StartTangent, EndPos, EndTangent, true);
Spline->RegisterComponent();
Spline->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, true));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Error creating component OR MeshAsset NULL or MeshAssetMaterial NULL"));
}
}
}
}
Please, post the error too.