In the last recipe, we created a Static Mesh Component, but we didn't try to load a mesh for the Component to display. While it's possible to do this in the Editor, sometimes it is helpful to specify a default in C++.
Follow the previous recipe so you have a custom Actor
subclass with a Static Mesh Component ready.
In your Content Browser, click on the View Options button, and select Show Engine Content:
Browse to Engine Content, then BasicShapes to see the Cube we will be using in this recipe.
auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")); if (MeshAsset.Object != nullptr) { Mesh->SetStaticMesh(MeshAsset.Object); }
FObjectFinder
class, passing in the type of asset that we are trying to load as a template parameter.FObjectFinder
is a class template that helps us to load assets. When we construct it, we pass in a string that contains a path to the asset that we are trying to load."{ObjectType}'/Path/To/Asset.Asset'"
. Note the use of single quotes in the string.auto
keyword, from C++11, to avoid typing out our whole object type in its declaration; the compiler deduces it for us. Without auto
, we would have to use the following code instead:ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'"));
FObjectFinder
class has a property called Object
that will either have a pointer to the desired asset, or will be NU
LL
if the asset could not be found.nullptr
, and if it isn't null, assign it to Mesh
using SetStaticMesh
.3.143.231.26