I’m trying to create a project for University where I can load a “.fbx” model, into a static mesh, using C++. However, I am having some trouble actually adding the model to my world and after consulting a lot of videos and forum posts I am still unsure as to how I would go about doing this.
Here is the .h file at the moment:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "ObjectLoader.generated.h"
UCLASS()
class OBJECTLOADING_API AObjectLoader : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AObjectLoader();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere)
UShapeComponent* Root;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* mesh;
UPROPERTY(EditAnywhere)
UStaticMesh* model1;
};
The .cpp file at the moment:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ObjectLoading.h"
#include "ObjectLoader.h"
// Sets default values
AObjectLoader::AObjectLoader()
{
// 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;
Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
RootComponent = Root;
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
mesh->AttachTo(RootComponent);
model1 = CreateDefaultSubobject<UStaticMesh>(TEXT("'Model1' /Content/Models/model1.fbx"));
mesh->SetStaticMesh(model1);
}
// Called when the game starts or when spawned
void AObjectLoader::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AObjectLoader::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
Thanks for taking the time to reply. I had a look at that forum post while I was trying to get it working originally, but I took another shot at it again to see if something had changed. After adding the code, it seems to result in Unreal Engine crashing before it has finished loading.
Here is the .h file now:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "ObjLoader.generated.h"
UCLASS()
class OBJECTLOADER_API AObjLoader : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AObjLoader();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere)
UShapeComponent* Root;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* mesh;
UPROPERTY(EditAnywhere)
UStaticMesh* model1;
};
The .cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ObjectLoader.h"
#include "ObjLoader.h"
// Sets default values
AObjLoader::AObjLoader()
{
// 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;
Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> Jacket(TEXT("/Game/Models/model1'"));
RootComponent = mesh;
mesh->SetStaticMesh(Jacket.Object);
}
// Called when the game starts or when spawned
void AObjLoader::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AObjLoader::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
Here is the code that I used, and it seemed to work on my end. Have a look:
#pragma once
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class SETMESH_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//Changed to a UBoxComponent
UPROPERTY(EditAnywhere)
UBoxComponent* Root;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* mesh;
UPROPERTY(EditAnywhere)
UStaticMesh* model1;
};
#include "SetMesh.h"
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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;
//Created box component and setting it as the root component
Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
RootComponent = Root;
//Creating the mesh component and attaching it to the root component
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
mesh->AttachTo(RootComponent);
//Grabbing the mesh I'd like to use
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh (TEXT("/Game/FirstPerson/Meshes/FirstPersonProjectileMesh.FirstPersonProjectileMesh"));
//Setting the mesh
mesh->SetStaticMesh(Mesh.Object);
}