How can I spawn a mesh without Blueprint?

Hello all,

Noob here with a (hopefully) quick question. My goal is to create a “prop” class that is basically a mesh that can take damage. Preferably with the ability to pass a string with the relative path to the mesh to be spawned as an argument.

Now, I am having an issue with the mesh part. I created the prop class using the in-editor tool using AActor as the parent. I have tried using AActor::AddComponent and UWorld::SpawnActor and I can’t get them to create a mesh. I knew there was going to be an issue when I couldn’t even find a way to pass the string of the mesh to be created. Any suggestions?

Edit: Here is my code

Prop.h

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "GameFramework/Actor.h"
#include "Prop.generated.h"

/**
 * 
 */
UCLASS()
class AProp : public AActor
{
    GENERATED_UCLASS_BODY()

    UPROPERTY()
    int32 m_Health;

    /** Static Mesh Comp, Set In BP Default Properties */
    UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "StaticMesh Components")
    TSubobjectPtr<UStaticMeshComponent> m_Mesh;

    virtual void BeginPlay() OVERRIDE;

};

Prop.cpp

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "MyGame.h"
#include "Prop.h"


AProp::AProp(const class FPostConstructInitializeProperties& PCIP)
    : Super(PCIP)
{
    m_Health = 100;

    m_Mesh = PCIP.CreateAbstractDefaultSubobject(this, TEXT("m_Mesh"));

    static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

    static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

    m_Mesh->SetStaticMesh(StaticMesh.Object);
    m_Mesh->SetMaterial(0, Material_Blue.Object);
}

void AProp::BeginPlay()
{
    Super::BeginPlay();

    if (GEngine)
    {
       GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World!"));
       GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(m_Health));
    }
}

Have a look at this thread i made, this is how.

please bare in mind i made a mistake when I copied it over.
this line

static ConstructorHelpers::FObjectFinder StaticMesh(TEXT(“StaticMesh’/Game/Shapes/Shape_Cube.Shape_Cube’”));er

should be

static ConstructorHelpers::FObjectFinder <UStaticMeshComponent>StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

The forum is formatting the line, i cant post correct version
,

I can’t get it to work. Here is my code:

Prop.h

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "GameFramework/Actor.h"
#include "Prop.generated.h"

/**
 * 
 */
UCLASS()
class AProp : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY()
	int32 m_Health;

	/** Static Mesh Comp, Set In BP Default Properties */
	UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "StaticMesh Components")
	TSubobjectPtr<UStaticMeshComponent> m_Mesh;

	virtual void BeginPlay() OVERRIDE;
	
};

Prop.cpp

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "MyGame.h"
#include "Prop.h"


AProp::AProp(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	m_Health = 100;

	m_Mesh = PCIP.CreateAbstractDefaultSubobject(this, TEXT("m_Mesh"));

	static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

	static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

	m_Mesh->SetStaticMesh(StaticMesh.Object);
	m_Mesh->SetMaterial(0, Material_Blue.Object);
}

void AProp::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World!"));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(m_Health));
	}
}

Hey there,

 m_Mesh = PCIP.CreateAbstractDefaultSubobject(this, TEXT("m_Mesh"));

this line should be

m_Mesh = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("m_Mesh"));

and those:

 static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

    static ConstructorHelpers::FObjectFinder<UStaticMeshComponent> Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

should be:

 static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

    static ConstructorHelpers::FObjectFinder<UMaterial> Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

it helped for me :wink:

Hey,
kindof an old thread, but for those stumbling with this problem, heres the solution working for me as of UE4 v4.8

First of all there are 2 moments at which you may want to create a mesh; at construction time and at runtime. Theres a different procedure for each.

For these examples the mesh I want to load is called “BlameGT_Mesh” and in the UE4 editor is found at “Content/MyCharacter/Meshes/weapons/”

The Actor is Weapon and the mesh I instantiated during its construction is called baseMesh.
So ill be replacing Weapons’ mesh called baseMesh with the newly loaded BlameGT_Mesh.

At construction time: (when you’re constructing the actor with “xclass::xclass(const class FObjectInitializer& FOI)…”
you may use the method describe previously by oOo.DanBO.oOo

    FString pathName= "StaticMesh'/Game/MyCharacter/Meshes/weapons/BlameGT_Mesh'";
    //do take notice of the ' symbols. they seem important.
    ConstructorHelpers::FObjectFinder<UStaticMesh> mySSMesh12412(*pathName);
   //if you're inputing a FString, the * is necessary.
    Weapon->baseMesh->SetStaticMesh(mySSMesh12412.Object);

At runtime: (as of my understanding, every rest of the time when not in construction time)

    FString pathName= "StaticMesh'/Game/MyCharacter/Meshes/weapons/BlameGT_Mesh.BlameGT_Mesh'";
    //do take notice of the ' symbols. they seem important.
   //also of the . and that we repeat the name.
   UStaticMesh* mySSMesh12412= Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *pathName));
   //if you're inputing a FString, the * is necessary.
    Weapon->baseMesh->SetStaticMesh(mySSMesh12412);

I think thats it. This is my first post on these forums, so if I forgot something, please let me know.