How to load static mesh asset for custom Actor?

How to load static mesh asset for custom Actor?
I’m using the documentation on: https://docs.unrealengine.com/latest/INT/Programming/Tutorials/Components/1/index.html

But it doesn’t work and it causes unreal editor to crash when trying to play.

Here is my ctor:

ACubeBaseCube::ACubeBaseCube()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	UStaticMeshComponent* CubeVisualComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	CubeVisualComponent->AttachTo(RootComponent);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMeshAsset(TEXT("StaticMesh'/Game/MyMeshes/Level_geometry/SimpleCube001.SimpleCube001'"));
	if (CubeMeshAsset.Succeeded())
	{
		CubeVisualComponent->SetStaticMesh(CubeMeshAsset.Object);
		CubeVisualComponent->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
		CubeVisualComponent->SetWorldScale3D(FVector(0.8f));
	}

	SetActorEnableCollision(true);
}

How does one correctly load static meshes in unreal engine 4? And what am I doing wrong?

You need to do that in a constructor that as the Object Initializer. Basically, you want to use the GENERATED_UCLASS_BODY() instead of GENERATED_BODY() in your class .h

Then, you dont need to declare the constrcutor in the .h (the macro will take care of that for you) and then your constructor should look like this:

ACubeBaseCube::ACubeBaseCube(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMeshAsset(TEXT("StaticMesh'/Game/MyMeshes/Level_geometry/SimpleCube001.SimpleCube001'"));

 CubeVisualComponent->SetStaticMesh(CubeMeshAsset.Object);
}

Thank! But I’m afraid that it still is not working…
And here are is my code

CubeBaseCube.h

    #pragma once
    
    #include "GameFramework/Actor.h"
    #include "CubeBaseCube.generated.h"
    
    UCLASS()
    class CUBEGAME_API ACubeBaseCube : public AActor
    {
    	GENERATED_UCLASS_BODY()
    
    public:	
    	// Called when the game starts or when spawned
    	virtual void BeginPlay() override;
    	
    	// Called every frame
    	virtual void Tick( float DeltaSeconds ) override;
    
    protected:
    	UStaticMeshComponent* CubeVisualComponent;
    
    };

CubeBaseCube.cpp

#include "CubeGame.h"
#include "CubeBaseCube.h"


// Sets default values
ACubeBaseCube::ACubeBaseCube(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMeshAsset(TEXT("StaticMesh'/Game/MyMeshes/Level_geometry/SimpleCube001.SimpleCube001'"));
	
	if (CubeMeshAsset.Succeeded())
	{
	
		CubeVisualComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CubeVisualComponent"));
		CubeVisualComponent->SetStaticMesh(CubeMeshAsset.Object);

	}
}

// Called when the game starts or when spawned
void ACubeBaseCube::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ACubeBaseCube::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

Hmm ok. So it seems there was some bug in the editor. Because I remove the CubeBaseCube files that was not working and rebuild the solution and restarted the editor and it would still crash when i hit play…

So i had remove the CubeBaseCube file AND create a new level from scratch. And now everything is fine.

Here are the now working code for manually loading static meshes:

.h file:

UCLASS()
class CUBEGAME_API ACubeBaseCube : public AActor
{
	GENERATED_BODY()
public:	
	// Sets default values for this actor's properties
	ACubeBaseCube();
	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;
protected:
	UStaticMeshComponent* CubeVisualComponent;
};

.cpp file:

#include "Game.h"
#include "CubeBaseCube.h"


// Sets default values
ACubeBaseCube::ACubeBaseCube()
{
 	PrimaryActorTick.bCanEverTick = true;
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMeshAsset(TEXT("StaticMesh'/Game/MyMeshes/Level_geometry/SimpleCube001.SimpleCube001'"));
	if (CubeMeshAsset.Succeeded())
	{
		PrimaryActorTick.bCanEverTick = true;
		CubeVisualComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CubeVisualComponent"));
		CubeVisualComponent->SetStaticMesh(CubeMeshAsset.Object);
		RootComponent = CubeVisualComponent;
		SetActorEnableCollision(true);
	}
}

void ACubeBaseCube::BeginPlay(){
	Super::BeginPlay();
}
void ACubeBaseCube::Tick( float DeltaTime ){
	Super::Tick( DeltaTime );
}

And by not working I meant that the editor was still crashing…