Set N evenly spaced locations along a spline
Unreal pastebin link: https://blueprintue.com/blueprint/z2jsqc7q/
This is a blueprint function library node where you input a number of points and assign the involved Spline component, then get back an array of locations on the spline according to the even spacing of the points.
This is useful if you want to extrude a mesh from a spline, or just evenly space out some spawners at locations.
Here is the same BP node Get Local Points Along Line after converting the BP into code. Instead of showing the function name, it's showing, from its Meta info, the DisplayName... "Create array of Locations Along Spline". It uses an actor's spline component and a Draw Debug Box node for each location in the array:
In my case, the blueprint function library class is called FunFunctionLibrary (or 'UFunFunctionLibrary' in the actual code body). The header has to include SplineComponent (#include "Components/SplineComponent.h"). You can see what a Function Library looks like by default by adding that class type in C++ from in the editor:
Filter All Classes, BlueprintFunctionLibrary, then name it what you want after choosing the correct source class:
This is just the function we're talking about. The body of a function library can contain lots of functions. .h
UFUNCTION(BlueprintCallable, Category = "SplineFunctions", meta = (DisplayName = "Create Array of Locations along Spline", ToolTip = "This function creates an array of vectors placed along a spline component by N even segments.", Keywords = "spline points location segments"))
static TArray<FVector> GetLocalPointsAlongSpline(const USplineComponent* SplineComponent, int32 NumEdges);
.cpp
TArray<FVector> UFunFunctionLibrary::GetLocalPointsAlongSpline(const USplineComponent* SplineComponent, int32 NumEdges)
{
TArray<FVector> LocationsToReturn;
// Ensure the spline component is valid and the number of edges is positive
if (!SplineComponent || NumEdges < 1)
{
return LocationsToReturn;
}
// Reserve memory for the array to prevent multiple allocations
LocationsToReturn.Reserve(NumEdges + 1);
const float SplineLength = SplineComponent->GetSplineLength();
const float SectionLength = SplineLength / NumEdges;
for (int32 i = 0; i <= NumEdges; ++i)
{
const float Distance = SectionLength * i;
FVector Location = SplineComponent->GetLocationAtDistanceAlongSpline(Distance, ESplineCoordinateSpace::World);
LocationsToReturn.Add(Location);
}
return LocationsToReturn;
}
Comments