Setting default mesh to a class property

I want to create a tile-based map of a given size (like 8*8 tiles) from a given Tile (square or hexagonal).
I plan to store this default mesh in a class property:
UPROPERTY(EditDefaultOnly, Category = “Default Mesh”)
UStaticMesh * TileMesh;
But the problem is that a don’t know how to properly initialize this property (in c++ class) from an exist mesh in my project.
I’ve tried CreateDeafaultSubobject(), static ConstructorHelpers::FObjectFinder MyMesh(TEXT(%mesh reference%)), but it is always NULL on game start.
How can i set default mesh to my class property?

1 Like

If you want to use your method of exposing it as a class property, like so :

UPROPERTY(EditDefaultsOnly, Category = "Default Mesh") 
UStaticMesh* TileMesh;

Than, you need to create a Blueprint in the editor with your class. In this blueprint, in the Class Defaults section, you will have your property show up there so you can select a Static Mesh from those you have imported in your game.

Otherwise, you can use the object finder to automatically load the mesh in your class constructor like so :

ATestActor::ATestActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> MyMesh(TEXT("StaticMesh'/Game/ThirdPerson/Meshes/CubeMesh.CubeMesh'"));
	TileMesh = MyMesh.Object;
}

Of course this is only the static mesh, to actually use it you need to assign it to a StaticMeshComponent, but I guess you already know that?

Hope this helps!

Mick

■■■■ it! i knew it was really simple, but still i didn’t get it. Thank you!
This line i was looking for - TileMesh = MyMesh.Object;
I was tryint to use Get,Assign,Attach etc.

I’m not sure if i fully understand mesh component system: i thought that when you spawn an Actor you aasign mesh to it (like StaticMesh) and then if you need additional parts - you create StaticMeshComponent and attach it to basic mesh.

About Blueprint - i made it blueprint accessible to be sure it works :slight_smile: but i needed it through c++

Great, you can tag it as an accepted answer than :slight_smile: