Loading static meshes during runtime?

Hi guys,

I am interested in assigning static mesh to my static mesh component during runtime, after I receive a string with path to specific mesh from other class. So, in constructor, I just do

Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("PickupMesh1P"));
RootComponent = Mesh;
...

then, in the other member function of the class, when I get the FString with correct mesh path (e.g. “StaticMesh’/Game/Shapes/Shape_Pipe_180.Shape_Pipe_180’”), I would like to assign that mesh to my component. The only way I know is this:

FString mpath = Item ->getMeshInfo();
static ConstructorHelpers::FObjectFinder<UStaticMesh> PickupMesh(*mpath);
Mesh->SetStaticMesh(PickupMesh.Object);

which obviously does not work, as ConstructorHelpers::FObjectFinder can only be used in constructor.

How do I do that in the right way? I do not believe that there is no way to dynamically alter static mesh models during runtime.

#Dynamic Load Object

I have an in-game editor where I get and set static mesh assets all the time!

Here’s how I do it

please note

If you have a lot of mesh assets to reference that you know before compile time, you should stick them in a .h somewhere like this

UPROPERTY(EditDefaultsOnly,BlueprintReadOnly,Category="SM Assets")
UStaticMesh* MySmAsset1;

UPROPERTY(EditDefaultsOnly,BlueprintReadOnly,Category="SM Assets")
UStaticMesh* MySmAsset2;

UPROPERTY(EditDefaultsOnly,BlueprintReadOnly,Category="SM Assets")
UStaticMesh* MySmAsset3;

And then use SetStaticMesh directly with that

Reason: During packaging the above references will be carried over correctly.

Reason2: They’re super easy to change at any time without requiring code compile.

Hardcoding asset paths is bad idea cause they will change during packaging and may not get updated.

My Dynamic Load Object is for when you dont know which mesh to load pre-compile time, but the user picks one in game / you maybe randomly choose from a series of meshes

#Video

Here’s a video of my runtime use case!

In the very beginning, I am clicking on different mesh and using my Dynamic Load Object code above to get the mesh asset to build with

:slight_smile:

Rama

Thanks, man, that works now. However, while in “Simulation” mode I clearly see my mesh, for some reason if I start playing as a player I don’t see it on the map. Do u have any thoughts, why does that happen?
I have flags set in a right way, as far as I understand:

Mesh->SetVisibility(true);
	Mesh->bOnlyOwnerSee		 = false;
	Mesh->bCastDynamicShadow = false;
	Mesh->bCastStaticShadow  = false;
	Mesh->bReceivesDecals    = false;

#SetMobility(EComponentMobility::Movable)

Are you sure you set the mesh to be moveable

#you can’t change the meshes of static mobility meshes at run time

9957-mobilitymoveable.jpg

:slight_smile:

#CPP Setting in Constructor

//Mobility
StaticMeshComponent->SetMobility(EComponentMobility::Movable);

or in your case

//Mobility
Mesh->SetMobility(EComponentMobility::Movable);

Rama

Dude! fifth time today I come under the same wiki file that doesn’t exist anymore, and the video doesnt’even share source code