Spline mesh update in a run time doesn't work correctly

Hi, I’m trying to update a spline each tick and make sure that the mesh over it follows. The number of points in spline is fixed (let’s say it’s 6)

1) I’m using the following code in BeginPlay:


for (int SplineCount = 0; SplineCount < (ChunkComponent->GetNumberOfSplinePoints() - 1); SplineCount++)
		{
			UE_LOG(LogTemp, Warning, TEXT("UPDATING IS OKE"));
			SplineMeshComponentChunk = NewObject<USplineMeshComponent>(this, USplineMeshComponent::StaticClass());
			SplineMeshComponentChunk->SetStaticMesh(MeshChunkRoad);
			SplineMeshComponentChunk->SetMobility(EComponentMobility::Movable);
			SplineMeshComponentChunk->CreationMethod = EComponentCreationMethod::UserConstructionScript;
			SplineMeshComponentChunk->RegisterComponentWithWorld(GetWorld());
			SplineMeshComponentChunk->AttachToComponent(ChunkComponent, FAttachmentTransformRules::KeepRelativeTransform);

			const FVector StartPoint = ChunkComponent->GetLocationAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);
			const FVector StartTangent = ChunkComponent->GetTangentAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);

			const FVector EndPoint = ChunkComponent->GetLocationAtSplinePoint(SplineCount + 1, ESplineCoordinateSpace::Local);
			const FVector EndTangent = ChunkComponent->GetTangentAtSplinePoint(SplineCount + 1, ESplineCoordinateSpace::Local);

			SplineMeshComponentChunk->SetStartAndEnd(StartPoint, StartTangent, EndPoint, EndTangent, true);

			SplineMeshComponentChunk->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

			SplineMeshComponentChunk->SetMaterial(0, DefaultMaterial);

		}

And it generates mesh over the spline without any issues.

2) Then I’m updating spline points location in a loop each tick, also, no issues, I can see all points are updated without problems in console:

for (int SplineCount = 0; SplineCount <= (ChunkComponent->GetNumberOfSplinePoints()); SplineCount++)
	{
		OldVika = ChunkComponent->GetLocationAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);
		NewRank = FVector(OldVika.X, OldVika.Y, OldVika.Z + 1.0);
		ChunkComponent->SetLocationAtSplinePoint(SplineCount, NewRank, ESplineCoordinateSpace::Local, true);	
	}

3) And after that I’m trying to update spline mesh by SetStartAndEnd to follow new points of the spline:

	for (int SplineCount = 0; SplineCount < (ChunkComponent->GetNumberOfSplinePoints() - 1); SplineCount++)
		{

			StartPoint = ChunkComponent->GetLocationAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);
			StartTangent = ChunkComponent->GetTangentAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);

			EndPoint = ChunkComponent->GetLocationAtSplinePoint(SplineCount + 1, ESplineCoordinateSpace::Local);
			EndTangent = ChunkComponent->GetTangentAtSplinePoint(SplineCount + 1, ESplineCoordinateSpace::Local);

			SplineMeshComponentChunk->SetStartAndEnd(StartPoint, StartTangent, EndPoint, EndTangent, true);

		}

In the end, I see that only the last piece of spline is actually updated when I press play, all others remains the same. I was trying to redo the same code in blueprint only but I ended up with the same results. Could you help me to figure out what’s wrong?

Previously I managed to do the task by using full constructor from 1) every tick, but the performance was awful.

Ok, it was easy, my bad:

So in BeginPlay you have to create an array:

SplineMeshComponentChunk.Add(SplineMeshComponentChunkPiece);

And then iterate over this array to get things done