x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Finding texture2d from a fstring at runtime

Hello, I have a FString variable that has my relative path to the uasset texture. How do I use the variable in getting the utexture2d object?

static ConstructorHelpers::FObjectFinder CrosshairTexObj(TEXT("Texture2D'%s'"), someFStringVariablePath);

EDIT: Looks like that code above is just for construct. Is there something similar for runtime?

Product Version: UE 4.21
Tags:
more ▼

asked Dec 21 '18 at 09:33 AM in C++ Programming

avatar image

Redsett
45 9 9 12

avatar image Redsett Dec 21 '18 at 07:03 PM

I don't think Texture2D is actually needed in that.

static ConstructorHelpers::FObjectFinder CrosshairTexObj(TEXT(someFStringVariablePath);

After some more research, I believe ConstructorHelpers::FObjectFinder is not the function I'm looking for. Because it seems to be only for ObjectInitializer.

I'm getting paths from json files at run time. These paths are relative paths to already imported textures. I just need to know how to convert these FString paths that I'm getting from the json files to a texture2d.

avatar image Leota786 Jan 08 '19 at 01:33 PM

I want to create a Texture2D dynamically which is readable. There doesn't appear to be any way to set the IsReadable property to true. The docs reference IsReadable, but I can't even find it declared anywhere.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I found the solution here:

https://wiki.unrealengine.com/Dynamic_Load_Object

header file:

 template <typename ObjClass>
 static FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
 {
     if (Path == NAME_None) return NULL;
     //~
 
     return Cast<ObjClass>(StaticLoadObject(ObjClass::StaticClass(), NULL, *Path.ToString()));
 }
 
 static FORCEINLINE UTexture2D* GetTexture2DByName(const FName& Path)
 {
     if (Path == NAME_None) return NULL;
     //~
 
     return LoadObjFromPath<UTexture2D>(Path);
 }

C++ file:

 GetTexture2DByName(FName(*someFStringVariable));
more ▼

answered Dec 21 '18 at 09:58 PM

avatar image

Redsett
45 9 9 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question