I have a custom widget with an image component. I want to change the image dynamically based on a string from a data table. How do I do this?
-
1\$\begingroup\$ Where is the iamge located? Is it on the web, the computer's filesystem, a uasset inside your project or something else? \$\endgroup\$Sturlen– Sturlen2017-10-29 10:03:45 +00:00Commented Oct 29, 2017 at 10:03
-
\$\begingroup\$ It is an image asset that is already registered in Unreal (already is a uasset). \$\endgroup\$Andrew U Baker– Andrew U Baker2017-10-29 23:12:42 +00:00Commented Oct 29, 2017 at 23:12
1 Answer
C++
You can use the LoadObject function to find and load and object at runtime. Full documentation is available from Epic Games here.
For loading an image the following should work:
GridTexture = LoadObject<UTexture2D>(NULL, TEXT("/Engine/EngineMaterials/DefaultWhiteGrid.DefaultWhiteGrid"), NULL, LOAD_None, NULL);
Blueprint
As of yet there is no native implementation, but it is included in Rama's Victory Plugin.

Select the class you want to want to load and give it the reference path of your asset. The loaded asset will be an Object so you will have to cast it to Texture2D. Then you can store it as a variable or directly set the image of your widget.
Below is an example of using this with a DataTable with some assets.
To get a usable reference, simply right click an asset and select Copy Reference.
-
\$\begingroup\$ I tested this, and it definitely worked. Thanks again. Note: To get the "reference path", right-click on the asset in the asset viewer and select "Copy reference". \$\endgroup\$Andrew U Baker– Andrew U Baker2017-11-01 04:07:10 +00:00Commented Nov 1, 2017 at 4:07
