Loading assets into components using FObjectFinder

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++.

Getting ready

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:

Getting ready

Browse to Engine Content, then BasicShapes to see the Cube we will be using in this recipe.

How to do it...

  1. Add the following code to the constructor of your class:
    auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")); ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.