What's the correct way to add spline mesh in C++?

I’m trying to make grenade trajectory with spline component and spline mesh. Have looked through some tutorials. I know there’s a function called Add Spline Mesh Component in Blueprint (I don’t know why many people use this while the description says don’t call it manually), but there’s no equivalent in C++. So I tried something like this:

After getting the spline points, I loop over the points and create new spline mesh.

USplineMeshComponent* NewMesh = NewObject<USplineMeshComponent>();
			
NewMesh->SetStaticMesh(BaseSplineMesh);
NewMesh->Mobility = EComponentMobility::Movable;
NewMesh->AttachToComponent(PredictionSpline, FAttachmentTransformRules::KeepRelativeTransform);
NewMesh->RegisterComponent();
SplineMeshes.Add(NewMesh);
// ...
// get / set location and tangent stuff.

where SplineMeshes is a TArray<USplineMeshComponent*> used to stored the spline meshes.

But I can’t see any spline mesh in game (the spline itself looks good). I even get an error at SplineMeshes.Add(NewMesh); (no detailed info, VS just says A breakpoint instruction (__debugbreak() statement or a similar call) was executed in UnrealEditor-Win64-DebugGame.exe.). But it will continue to run if I press Continue button.

So could someone please tell me what’s going on here? What’s the correct way to do this?

Seems about right, but I guess you need to set the owner USplineMeshComponent* NewMesh = NewObject<USplineMeshComponent>(this);
And I don’t think you need to attach mesh to the spline, but that depends on what locations and tangents you get. Make sure the mesh doesn’t spawn somewhere around 0.0.0 coordinates.

1 Like

You are right. this should be passed to NewObject. I can see the meshes now.

Thank you so much!

until the last Level is reached, and then start over again.