Load Static Mesh With Path in c++

hi.
how can I load static mesh in a TSubobjectPtr with a path.
for example I have a mesh in my content in folder Mesh/Cube;
Now I want load it with c++ code in a TSubobjectPtr like

TSubobject<UStaticMeshComponent> mesh = /* the mesh in Mesh/Cube */
1 Like

First you will need to create a default subobject in your constructor:

mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("mesh"));

Then, to set the actual mesh to use (can be done from anywhere):

UStaticMesh* meshToUse = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("pathToYourAssetHere")));

if(meshToUse && mesh)
{
    mesh->SetStaticMesh(meshToUse);
}

Please note that you will have to declare “mesh” as a class variable.

1 Like

You can use this

TSubobject<UStaticMeshComponent> mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/Mesh/Cube/YourMesh"));
mesh->SetStaticMesh(MeshObj.Object);

thanks very much.

thanks. i find what i need

is there a way to do this without PCIP in the current version 4.14?

Hi guys!! Do you have a tutorial for doing this? I want to load a mesh from path working in BP mode, but I can’t figure out how to do it :frowning:

I’m not a developer but I understand very well bluprints :smiley:

Doesn’t work outside constructors, beware

See the other answer. StaticLoadObject() works anywhere.