What is the correct way to deal with asset loading

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.

Hey there, where does it crash exactly?

Ok so I’ve managed to get this working finally and learned a good bit in the process so I’m going to post what I’ve learned for anyone else having trouble.

Firstly since you want to create USplineMeshComponent dynamically the constructor is not the place to do it, instead override OnConstruction(const FTransform &Transform) and do it in there, if you don’t want it to be constructed in the editor do it in BeginPlay() instead.

splinemeshcomponent->CreationMethod = EComponentCreationMethod::UserConstructionScript; must be added or it will crash.

For creating dynamic objects use NewObject<>(this); instead of CreateDefaultSubobject<>(TEXT(“Name”));

AttachTo is depreciated use AttachToComponent instead.

Any changes will create a new instance, still not entirely sure how to go about dealing with this since it forgets my editor settings on any change but at least it’s sort of working now.