[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English Source address

Following the tutorial in the next section, let’s start by creating a new Actor subclass called AddMeshFromFile.

We don’t need to do anything in the header file.

Here is the default header file generated when a new class is created.

AddMeshFromFile.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AddMeshFromFile.generated.h"

UCLASS(a)class UNREALCPP_API AAddMeshFromFile : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	AAddMeshFromFile(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
};
Copy the code

To programmatically add a specific grid, we must include the constructorhelpers. h file.

The code looks like this, including the Actor subclass header after it.

#include "AddMeshFromFile.h"
// add constructor header
#include "UObject/ConstructorHelpers.h"
Copy the code

Next, in the constructor of the Actor subclass, we will set the default values for the mesh to add to the Actor. Create a pointer to UStaticMeshComponent and set it to RootComponent.

AAddMeshFromFile::AAddMeshFromFile()
{
 	// 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;

	// add Cylinder to root
    UStaticMeshComponent* Cylinder = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    Cylinder->SetupAttachment(RootComponent);

}
Copy the code

After our initial setup, the next step is to add the mesh we want to our character. We’ll use the method FObjectFinder in ConstructorHelpers to locate our grid. In this example, I construct and invoke the variables CylinderAsset and pass in the path of cylinder shapes provided by the Beginner content. We are introduced into the path of the/Game/StarterContent/Shapes/Shape_Cylinder Shape_Cylinder.

AAddMeshFromFile::AAddMeshFromFile()
{
 	// 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;

	// add Cylinder to root
    UStaticMeshComponent* Cylinder = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    Cylinder->SetupAttachment(RootComponent);

    static ConstructorHelpers::FObjectFinder<UStaticMesh> CylinderAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder"));

}
Copy the code

Next, let’s do an error check to make sure we successfully get the grid.

if (CylinderAsset.Succeeded() {}Copy the code

If the check statement passes, let’s proceed to execute the following three statements on the cylinder component

  • SetStaticMesh
  • SetRelativeLocation
  • SetWorldScale3D
if (CylinderAsset.Succeeded()) 
{
    Cylinder->SetStaticMesh(CylinderAsset.Object);
    Cylinder->SetRelativeLocation(FVector(0.0 f.0.0 f.0.0 f));
    Cylinder->SetWorldScale3D(FVector(1.f));
}
Copy the code

The final complete CPP is shown below

#include "AddMeshFromFile.h"
// add constructor header
#include "UObject/ConstructorHelpers.h"


// Sets default values
AAddMeshFromFile::AAddMeshFromFile()
{
 	// 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;

	// add Cylinder to root
    UStaticMeshComponent* Cylinder = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    Cylinder->SetupAttachment(RootComponent);

    static ConstructorHelpers::FObjectFinder<UStaticMesh> CylinderAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder"));

	if (CylinderAsset.Succeeded())
    {
        Cylinder->SetStaticMesh(CylinderAsset.Object);
        Cylinder->SetRelativeLocation(FVector(0.0 f.0.0 f.0.0 f));
        Cylinder->SetWorldScale3D(FVector(1.f)); }}// Called when the game starts or when spawned
void AAddMeshFromFile::BeginPlay(a)
{
	Super::BeginPlay(a); }// Called every frame
void AAddMeshFromFile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
Copy the code

The final result is as follows

Drag the new Actor subclass into the scene