AddSplineMeshComponent. What's the difference BP/CPP?

Hello, everyone!

I am trying to add USplineMeshComponent at runtime.

Firstly, I implemented everything in bluprints:

After that I started transferring the logic into code and faced the fact that I could not find the implementation of the AddSplineMeshComponent blueprint node anywhere.

Therefore, I had to find a way to add runtime components on my own. And that’s what I have:

	constexpr ESplineCoordinateSpace::Type LocalSpace = ESplineCoordinateSpace::Local;
	for (int PointIndex = 0; PointIndex <= RoadSpline->GetNumberOfSplinePoints() - 2; ++PointIndex)
	{
		UE_LOG(LogTemp, Warning, TEXT("Creating SplineComponent..."));

		USplineMeshComponent* SplineMeshComponent = NewObject<USplineMeshComponent>();

		SplineMeshComponent->SetStaticMesh(WideRoad.Road);
		SplineMeshComponent->SetMobility(EComponentMobility::Movable);
		SplineMeshComponent->RegisterComponentWithWorld(GetWorld());
		SplineMeshComponent->AttachToComponent(RoadSpline, FAttachmentTransformRules::KeepRelativeTransform);

		
		SplineMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
		SplineMeshComponent->SetForwardAxis(ESplineMeshAxis::X);
		
		const FVector StartPoint = RoadSpline->GetLocationAtSplinePoint(PointIndex, LocalSpace);
		const FVector StartTangent = RoadSpline->GetTangentAtSplinePoint(PointIndex, LocalSpace);
		const FVector EndPoint = RoadSpline->GetLocationAtSplinePoint(PointIndex + 1, LocalSpace);
		const FVector EndTangent = RoadSpline->GetTangentAtSplinePoint(PointIndex + 1, LocalSpace);

		SplineMeshComponent->SetStartAndEnd(StartPoint, StartTangent, EndPoint, EndTangent);
	}

With the help of this code, I managed to add components, but there was one serious problem with the collision of newly made components: as soon as the character steps on them, he simply gets stuck and can’t move at all, just rotate.


You can see collision enabled. And it is already suspicious that static meshes of two implementations use a different kind of collision: complex(working one) and simple.

So, question is:

What is the difference between the blueprint implementation of adding a Spline Mesh Component and my code that creates components that are impossible to walk on?

Also after looking into the sources, I managed to find out that, it seems, the creation of “AddComponent” nodes occurs using UK2Node_AddComponent::ExpandNode at compilation time.

But I guess there must be a place where AddSplineMeshComponent “configuration or template stuff” is located to create such node.

Where it is located?

1 Like

Solution

Simply use another NewObject<>() function overload:

		USplineMeshComponent* SplineMeshComponent = NewObject<USplineMeshComponent>(
			this, USplineMeshComponent::StaticClass());

		SplineMeshComponent->RegisterComponent();