Hello,
I am fairly new to Unreal. I am trying to spawn a simple spline mesh. I have had no success so far. The code I have causes the engine to crash. I have even tried removing the for loop to do only a single start, end and tangent for a mesh, but it did not work either. I’ve tried removing different parts and changing it up, and it looks like it crashes when calling the “MySplineMesh->SetStartAndEnd()” function.
My method and code may be completely wrong. Any help or advice would be very much appreciated.
Thank you.
(about the error:
Access violation - code c0000005 (first/second chance not available)
I received a similar error c0000001 at one point with this struggle as well, but not most recently.)
AMyActor::AMyActor()
{
// Set this actor to call Tick()
PrimaryActorTick.bCanEverTick = false;
// Create a root component
USceneComponent *TopScene = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RootComponent = TopScene;
// create a SplineComponent
USplineComponent *MySpline = CreateDefaultSubobject<USplineComponent>(TEXT("SplineComponent"));
MySpline->AttachTo(TopScene);
// add tangents
MySpline->AddSplinePoint(FVector(1000, 1, 1), ESplineCoordinateSpace::World);
MySpline->AddSplinePoint(FVector(1, 1000, 1), ESplineCoordinateSpace::World);
MySpline->AddSplinePoint(FVector(2000, 1, 1), ESplineCoordinateSpace::World);
MySpline->AddSplinePoint(FVector(1, 2000, 1), ESplineCoordinateSpace::World);
MySpline->AddSplinePoint(FVector(3000, 1, 1), ESplineCoordinateSpace::World);
MySpline->SetClosedLoop(false);
// create static mesh
ConstructorHelpers::FObjectFinder<UStaticMesh>MyMeshCube(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
for (int32 i = 0; i < MySpline->GetNumberOfSplinePoints() - 1; i++)
{
//USplineMeshComponent *MySplineMesh = CreateDefaultSubobject<USplineMeshComponent>(TEXT("SplineMeshComponent"));
USplineMeshComponent *MySplineMesh = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass(), this);
MySplineMesh->SetMobility(EComponentMobility::Movable);
MySplineMesh->AttachParent = MySpline;
MySplineMesh->SetStaticMesh(MyMeshCube.Object);
// set scale of mesh
MySplineMesh->SetStartScale(FVector2D(1, 1));
MySplineMesh->SetEndScale(FVector2D(1, 1));
FVector pointLocationStart, pointLocationEnd, pointTangentStart, pointTangentEnd;
MySpline->GetLocationAndTangentAtSplinePoint(i, pointLocationStart, pointTangentStart, ESplineCoordinateSpace::Local);
MySpline->GetLocationAndTangentAtSplinePoint(i + 1, pointLocationEnd, pointTangentEnd, ESplineCoordinateSpace::Local);
MySplineMesh->SetStartAndEnd(pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd);
}
RegisterAllComponents();
}