So I’ve been trying to figure out the correct way to do this for the last two days without any luck, Whenever I go to place my actor class the editor crashes with an access violation, I believe this is caused due to the lack of checks to see if a mesh has been loaded, which I have no idea how to do, I could of course use the FObjectFinder but I don’t really want to hard code the path to the mesh, so what is the correct way to have it so you can select the mesh in the editor without it attempting to use an invalid mesh?
Header Class
UCLASS()
class GAME_API AChryseus_SplineMesh : public AActor
{
GENERATED_BODY()
public:
AChryseus_SplineMesh();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
USceneComponent* C_Scene;
USplineComponent* C_Spline;
UPROPERTY(EditAnywhere, Category = Mesh)
UStaticMesh *StartMesh;
UPROPERTY(EditAnywhere, Category = Mesh)
UStaticMesh *MiddleMesh;
UPROPERTY(EditAnywhere, Category = Mesh)
UStaticMesh *EndMesh;
UPROPERTY(EditAnywhere, Category = Mesh)
UStaticMesh *SingleMesh;
UPROPERTY(EditAnywhere, Category = Material)
UMaterial *Material;
};
Source Constructor
AChryseus_SplineMesh::AChryseus_SplineMesh()
{
C_Spline = CreateDefaultSubobject<USplineComponent>(TEXT("Spline"));
C_Scene = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
StartMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Start Mesh"));
MiddleMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Middle Mesh"));
EndMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("End Mesh"));
SingleMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Single Mesh"));
Material = CreateDefaultSubobject<UMaterial>(TEXT("Override Material"));
RootComponent = C_Scene;
C_Spline->SetupAttachment(RootComponent);
RootComponent->SetMobility(EComponentMobility::Type::Movable);
C_Spline->SetMobility(EComponentMobility::Type::Movable);
UStaticMesh *useMesh = nullptr;
int32 splinePoints = C_Spline->GetNumberOfSplinePoints();
UE_LOG(LogTemp, Display, TEXT("Spline points %d."), splinePoints);
FVector locStart;
FVector tanStart;
FVector locEnd;
FVector tanEnd;
// Two point spline
if (splinePoints == 2)
{
useMesh = SingleMesh;
C_Spline->GetLocationAndTangentAtSplinePoint(0, locStart, tanStart, ESplineCoordinateSpace::Local);
C_Spline->GetLocationAndTangentAtSplinePoint(1, locEnd, tanEnd, ESplineCoordinateSpace::Local);
USplineMeshComponent *splineMesh = CreateDefaultSubobject<USplineMeshComponent>(TEXT("SplineMeshComponent"));
bool reg = splineMesh->IsRegistered();
splineMesh->SetMobility(EComponentMobility::Type::Movable);
splineMesh->SetForwardAxis(ESplineMeshAxis::X);
splineMesh->SetStaticMesh(useMesh);
splineMesh->SetMaterial(0, Material);
splineMesh->SetStartAndEnd(locStart, tanStart, locEnd, tanEnd);
splineMesh->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
//splineMesh->AttachToComponent(C_Spline, FAttachmentTransformRules::FAttachmentTransformRules(EAttachmentRule::KeepRelative, true));
splineMesh->SetupAttachment(C_Spline);
}
PrimaryActorTick.bCanEverTick = true;
}
Help would be very much appreciated.