Edit:
It seems like the problem is just the following line. I just want to check if the array at the given index is a nullptr.
if (!SplineMeshComponents[MeshComponentIndex])
Original Post:
I cannot figure out what’s wrong with it. I am pretty new to C++ programming and tried for way to long to resolve this
When I comment out everything inside the first while loop except the last two lines, it doesn’t crash.
TArray<USplineMeshComponent*> UTeleportComponent::DrawTrajectoryTrace(FTrajectoryTraceResult TrajectoryTraceResult)
{
AActor* Owner = GetOwner();
int32 TracePointIndex = 0, MeshComponentIndex = 0;
// iterate through the TraceStartPoints
while (TracePointIndex <= TrajectoryTraceResult.TracePointLocations.Num()-2) // "TracePointLocations.Num()-1" would be the TraceEndPoint, which we do not want
{
/// Set up the component if it does not exist otherwise make sure the component is visible
if (!SplineMeshComponents[MeshComponentIndex])
{
SplineMeshComponents.Emplace(NewObject<USplineMeshComponent>(GetOwner()));
SplineMeshComponents[MeshComponentIndex]->SetStaticMesh(TrajectoryMesh);
SplineMeshComponents[MeshComponentIndex]->SetMaterial(0, TrajectoryMaterial);
SplineMeshComponents[MeshComponentIndex]->SetMobility(EComponentMobility::Movable); // maybe not neccessary
SplineMeshComponents[MeshComponentIndex]->RegisterComponent();
}
else
{
SplineMeshComponents[MeshComponentIndex]->SetVisibility(true);
}
/// set start and end locations and tangents
SplineMeshComponents[MeshComponentIndex]->SetRelativeLocation(TrajectoryTraceResult.TracePointLocations[TracePointIndex]); // RelativeLocation is used as WorldLocation for USplineMeshComponent
SplineMeshComponents[MeshComponentIndex]->SetStartAndEnd(
FVector(0.f), // NullVector, because the location is in component space and we want to start at the components origin
TrajectoryTraceResult.TracePointVelocities[TracePointIndex],
TrajectoryTraceResult.TracePointLocations[TracePointIndex+1] - TrajectoryTraceResult.TracePointLocations[TracePointIndex], // converting world space to component space
TrajectoryTraceResult.TracePointVelocities[TracePointIndex+1]
);
SplineMeshComponents[MeshComponentIndex]->UpdateMesh();
SplineMeshComponents[MeshComponentIndex]->UpdateBounds();
/// Update variables for next loop iteration
TracePointIndex += 2; // because we want a dashed line, we skip every other TracePoint (TracePointIndex+=2)
MeshComponentIndex++;
}
// set unused SplineMeshComponents invisible
while (MeshComponentIndex <= SplineMeshComponents.Num()-1)
{
SplineMeshComponents[MeshComponentIndex]->SetVisibility(true);
MeshComponentIndex++;
}
return SplineMeshComponents;
}