Can you give me an example of SplineMeshComponent in C++?

Hi guys,
I´m currently stuck at coverting my spline-based road from blueprints to C++. Off course everything is working fine in blueprints already and I was able to spawn the spline from C++. However, there is no Mesh showing in the game world. As examples seem to be very rare, I would welcome any help you can offer :smiley:

What I have done so far:

if (Mesh)
		SplineMeshComp->SetStaticMesh(Mesh);
		SplineMeshComp->SetStartAndEnd(
			Spline->GetWorldLocationAtDistanceAlongSpline(i*MeshSize),
			Spline->GetWorldDirectionAtDistanceAlongSpline(i*MeshSize),
			Spline->GetWorldLocationAtDistanceAlongSpline(i*MeshSize + MeshSize),
			Spline->GetWorldDirectionAtDistanceAlongSpline(i*MeshSize + MeshSize)*TangentScale);
		if (Material)
		SplineMeshComp->SetMaterial(0,Material);
		SplineMeshComp->RegisterComponent();
		SplineMeshComp->RegisterComponentWithWorld(GetWorld()); //not sure about that line
		SplineMeshComp->AttachTo(Spline);
		
		SplineMeshComp->bCreatedByConstructionScript = true;

Here is how I do it :

void ARaceSpline::OnConstruction(const FTransform& Transform)
{
	for (int32 i = 0; i < mSplineComponent->GetNumSplinePoints() - 1; i++)
	{
		USplineMeshComponent* SplineMesh = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass(), this);

		SplineMesh->bCreatedByConstructionScript = true;
		SplineMesh->SetMobility(EComponentMobility::Movable);
		SplineMesh->AttachParent = mSplineComponent;

		//Set the color!
		UMaterialInstanceDynamic* dynamicMat = UMaterialInstanceDynamic::Create(mSplineMeshMaterial, NULL);
		dynamicMat->SetVectorParameterValue(TEXT("Color"), FLinearColor(mSegments[i].mColor));

		SplineMesh->bCastDynamicShadow = false;
		SplineMesh->SetStaticMesh(mGridMesh);
		SplineMesh->SetMaterial(0, dynamicMat);

		//Width of the mesh 
		SplineMesh->SetStartScale(FVector2D(50, 50));
		SplineMesh->SetEndScale(FVector2D(50, 50));
		
		FVector pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd;
		mSplineComponent->GetLocalLocationAndTangentAtSplinePoint(i, pointLocationStart, pointTangentStart);
		mSplineComponent->GetLocalLocationAndTangentAtSplinePoint(i + 1, pointLocationEnd, pointTangentEnd);

		SplineMesh->SetStartAndEnd(pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd);
	}

	RegisterAllComponents();
}

What seems to be missing in your case is the actual SplineMesh Component creation. Hope this code helps you :slight_smile:

1 Like

Thank you very much!

bCreatedByConstructionScript is now deprecated. Even if I try to set it to true, my meshes are not deleted on a new construction, so I am left with hundreds of meshes when I move my spline around. How do I fix this?

Edit: Actually, I found your other post at:

Thanks!

It’s weird that you commenting here actually remove the accepted answer :S I wonder if Epic could change that so we don’t actually lose Karma when this happens :S

Anyway, glad you found your answer.

I hope you don’t mind me commenting again. I have a new issue, regarding collisions, specifically that they only work in-editor and not in an exported Windows or Android build. I am not sure how to contact you through the answer hub, but if you could message me on the forums, I would greatly appreciate any assistance you can offer. Thanks!

https://forums.unrealengine.com/member.php?32254-shpen