# Loading a texture from C++

**URL:** <https://forums.unrealengine.com/t/loading-a-texture-from-c/278354>\
**Category:** Rendering\
**Tags:** unreal-engine, question\
**Created:** [March 25, 2014, 11:38am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354 "2014-03-25T11:38:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [March 25, 2014, 11:38am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/1 "2014-03-25T11:38:10Z")

</div>

I can’t seem to figure out how this is done, I have some code in play, but it cannot find the asset.

```
static ConstructorHelpers::FObjectFinder<UTexture2D> MyObj(TEXT("/Game/Spells/spell_heal"));

```

Now, you’re probably wondering, “does he have that asset in his editor?” and yes, yes I do.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/4/3/64315bfd630c23986021bb1f2e81d648c000ca30.png) ](https://i.imgur.com/removed.png)

And in explorer

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/d/6bd1ffd15016c51091165adbe4e161f234f6796d.png)  
Just to clarify, the end-goal of this this texture is to be loaded into an SImage Slate widget. I’ve wasted a 16 hour day on this, I can’t believe it’s this difficult to load little 50x50 png into the UI (and yes I’ve looked at the RTS/Shooter sources).

If I place the offending code in my HUD’s constuctor, then I get a little popup telling me…

CDO Constructor: Failed to find /game/spells/spell\_heal

I’m stumped.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 25, 2014, 12:16pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/2 "2014-03-25T12:16:53Z")

</div>

Is this for the editor, or for the runtime?

If it’s for the runtime, you’d create a Slate Brush asset that references the texture and use that.  
If it’s for the editor, you’d simply add a brush entry to your style set which references that image from disk.

Either way a style set will need to involved as this handles loading, atlassing, and managing the lifespan of textures used by Slate brushes.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [March 25, 2014, 12:19pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/3 "2014-03-25T12:19:59Z")

</div>

I just did a little ninja update there. I was hoping I’d have it fixed before someone replied =/ . Anyway it’s done. I don’t understand, I’m loading the texture exactly how it’s done in the RTS examples. I’ll have to lookup this style-set.

Can you give an example of creating the Slate Brush asset? I literally spent about 8 straight hours with intellisense, nuts.

To answer your question, it’s for the runtime, I wasn’t aware Slate was functioning in the Editor, I was under the impression that was a security vuln. I guess that just goes for the Viewport.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 25, 2014, 12:26pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/4 "2014-03-25T12:26:19Z")

</div>

The RTS samples won’t be loading images from disk for use by Slate, I can assure you of that.

What they will be doing though is loading brushes (which is a reference to a texture as well as some information about how it should render in Slate).

You create them in the editor by clicking “New” in the content browser, and then selecting “Miscellaneous → Slate Brush”. This will create an asset that can be loaded by your game via the FSlateGameResources style for your game (see FStrategyStyle::Create for an example of setting one of these up; the search path used must include the path where your Slate brush asset is located).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [March 25, 2014, 12:29pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/5 "2014-03-25T12:29:00Z")

</div>

That sounds simple enough, ty.

EDIT:  
Got it working, only took about 20 hours. I’m beat, will start this up again tomorrow.

![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/7/df7b06d0facc8b5d91d4ec361e377f8c94e55ec1.gif)  
Seriously though, the long cycles are just part of the learning/research phase, it will take upwards of a couple minutes to do the same thing in the future.

I should add, I did things a little bit differently. Instead of creating a FSlateStyleSet, I used a FSlateGameResources pointer / heap object.

```
   TSharedRef<FSlateGameResources> pMyResources
    = FSlateGameResources::New(FName("MyStyle"),
        "/Game/Spells", "/Game/Spells");
    FSlateGameResources& MyResources = SlateResources.Get();

```

It seemed more appropriate since I wasn’t actually working with any StyleSets, but what I would consider to be “resources”. It’s a subclass of SSlateStyleSet. What is the difference?

Here are the humble beginnings of my spell-bar. It’s all black because I’m in debug mode, no lighting or baked lights.

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/2/0/720fa9a3a95b54251dfbcc5d69c8639c98afa158.png)

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 25, 2014, 12:32pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/6 "2014-03-25T12:32:11Z")

</div>

I’ve just checked, and if you check out /Game/UI/Styles/DefaultActionImageBrush, that is an example of using a Slate brush asset in the RTS game.

That will be loaded in the runtime because FStrategyStyle uses /Game/UI/Styles as its search path (it also uses it as a base path, so you can omit the /Game/UI/Styles part when search for brushes from the style set). The brush itself is used in SStrategyButtonWidget::GetButtonImage.

Obviously this information is valid for runtime use; the editor works slightly differently as it loads images from disk rather than using assets.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 25, 2014, 12:34pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/7 "2014-03-25T12:34:38Z")

</div>

Just noticed your comment about the editor. The entire editor UI is created and rendered in Slate, so it’s definitely available in the editor 🙂

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 25, 2014, 2:50pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/8 "2014-03-25T14:50:18Z")

</div>

Resources in this context just means anything used by Slate, the main thing that goes in there (for a game) is brushes, widget styles, and fonts (fonts you currently have to set in code as we don’t have an editor representation of Slate fonts; we’re working on that).

Have you called FSlateStyleRegistry::RegisterSlateStyle(\*pMyResources); for that FSlateGameResources instance? If not, I’m surprised that works as an unregistered style set won’t have registered any textures with the atlasser, so usually just ends up rendering invalid, white, semi-transparent brushes everywhere.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [March 26, 2014, 1:09am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/9 "2014-03-26T01:09:18Z")

</div>

No, I hadn’t registered anything, I’d only called

```
TSharedRef<FSlateGameResources> SlateResources = FSlateGameResources::New(FName("MyStyle"), "/Game/Spells", "/Game/Spells");
FSlateGameResources& Style = SlateResources.Get();
const FSlateBrush* slate_spell_heal = Style.GetBrush(FName("slate_spell_heal"));

```

and later on

```
[
SNew(SImage).Image( slate_spell_heal )
]

```

Odd that it worked, but I updated the code to reflect your suggestions anyway.

```
//Access resources through Shared Pointer for safety
TSharedRef<FSlateGameResources> MyUIResources = FSlateGameResources::New(FName("MyUIResources"), "/Game/Spells", "/Game/Spells");
//Uncomment for direct access, less safe
//FSlateGameResources &MyUIResources = MyUIResources.Get();
//Register resources with Atlasser
FSlateStyleRegistry::RegisterSlateStyle( MyUIResources.Get() );

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [March 26, 2014, 3:56am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/10 "2014-03-26T03:56:18Z")

</div>

Another question, what do I use to register FSlateGameResources

I’m using

```
FSlateStyleRegistery::RegisterSlateStyle( *MyUIResourcesPtr );

```

Is that correct?

You know, at first I really hated the RTS game’s design, but I started writing some classes of my own, and without even looking they ended up being 99% the same lmao.

---

<div class="post-metadata">

**Author:** ![RichmarIII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RichmarIII](https://forums.unrealengine.com/u/RichmarIII)\
**Post date:** [March 26, 2014, 5:31am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/11 "2014-03-26T05:31:31Z")

</div>

I would assume the reason run-time loading of resources fails or is unsupported is because of the platforms Unreal supports. some platform do not allow or allow limited access to the file system and instead need to use embedded resources much like how Win32 Uses them for the exe’s icon. I assume brushes ARE embedded correct?

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 26, 2014, 10:00am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/12 "2014-03-26T10:00:33Z")

</div>

Assuming MyUIResourcesPtr is some form of pointer to an FSlateGameResources, then yes, that’s correct.

It’s probably also a good idea to call FSlateStyleRegistery::UnRegisterSlateStyle when shutting down your game as well.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 26, 2014, 10:04am UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/13 "2014-03-26T10:04:38Z")

</div>

Not embedded as such, but a brush asset (like any other asset) will be cooked and packed for your game.

That actually raises a good point. I mentioned that if you wanted to use a font, you still have to load those from disk; what I forgot to mention though is that the fonts will need to be somewhere inside the /Content/Slate folder for your project. If they’re not placed here then they won’t be included in the package for your game so will fail to load - this is a stop-gap until we have font assets that Slate can use.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [April 4, 2014, 7:33pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/14 "2014-04-04T19:33:30Z")

</div>

I just want to add that if anyone is having trouble with fonts in /Content/Slate

Try something like /Content/UI or /Content/Widgets

And adjust your code to use the /Game/UI or /Game/Widgets path instead of /game/slate . I haven’t loaded any fonts up yet, only brushes, but in one of the Engine’s .ini files there is a short list of places Slate will load resources from, the two I mentioned and a couple more, /Content/Widget is another one.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [April 4, 2014, 9:23pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/15 "2014-04-04T21:23:46Z")

</div>

Thanks, but /Content/Slate should work for fonts since they’re loaded directly from disk. The sample games use that as a place to store their fonts, and I believe it is currently hard-coded somewhere as a packaging path.

If it doesn’t work, then that’s a bug that we need to fix 🙂

---

<div class="post-metadata">

**Author:** ![LuisAlaguna](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/luisalaguna/32/570408_2.png) [@LuisAlaguna](https://forums.unrealengine.com/u/LuisAlaguna)\
**Post date:** [August 8, 2019, 1:19pm UTC](https://forums.unrealengine.com/t/loading-a-texture-from-c/278354/16 "2019-08-08T13:19:19Z")

</div>

I solved it putting these instructions in the constructor.
