UMG Image Binding Based On String Value

I want to display 1 out of a 100 images based on the value of a string which also has around 100 values. I was thinking about using Switch on String and having multiple return nodes in the binding but this would be quite time consuming and tedious. Any alternatives?
Thanks

I don’t think there is a way you can do this with blueprints except of creating a massive Select block with 100 entries and even than you would need to create an additional enum.

The easiest way is by creating a C++ Blueprint Function Libary and use this function

UFUNCTION(BlueprintCallable, Category = "Texture By Name")
static UTexture2D * GetTextureByName(const FString& Path, const FString& TextureName)
{
	
	FString AssetPath = "Texture2D'/{Path}/{TextureName}.{TextureName}'";
	AssetPath = AssetPath.Replace(TEXT("{TextureName}"), *TextureName).Replace(TEXT("{Path"), *Path);
	FStringAssetReference AssetRef(AssetPath);

	return Cast<UTexture2D>(AssetRef.TryLoad());
}

Even with little or no C++ knowledge it is easy to use this, just create a Blueprint Function Libary fom the wizard and copy that code.

Thanks for the reply, just tried this and it did not work.
I pasted the code in the .cpp file under the ‘#include’ lines and built the solution but there was no group of nodes called ‘texture by name’

You would need to paste it in the .h file or split it in declaration and implemantation.

BPLibrary.h:

 UFUNCTION(BlueprintCallable, Category = "Texture By Name")
 static UTexture2D * GetTextureByName(const FString& Path, const FString& TextureName);

BPLibrary .cpp:

UTexture2D * <BPLibraryClassname>::GetTextureByName(const FString& Path, const FString& TextureName)
 {
     
     FString AssetPath = "Texture2D'/{Path}/{TextureName}.{TextureName}'";
     AssetPath = AssetPath.Replace(TEXT("{TextureName}"), *TextureName).Replace(TEXT("{Path"), *Path);
     FStringAssetReference AssetRef(AssetPath);
 
     return Cast<UTexture2D>(AssetRef.TryLoad());
 }

Maybe this tutorial helps you to see what you must do:

These are the files:
TextureByName.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "TextureByName.h"

UTexture2D * <TextureByName>::GetTextureByName(const FString& Path, const FString& TextureName)
{

	FString AssetPath = "Texture2D'/{Path}/{TextureName}.{TextureName}'";
	AssetPath = AssetPath.Replace(TEXT("{TextureName}"), *TextureName).Replace(TEXT("{Path"), *Path);
	FStringAssetReference AssetRef(AssetPath);

	return Cast<UTexture2D>(AssetRef.TryLoad());
}

TextureByName.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "TextureByName.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT2_API UTextureByName : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
		UFUNCTION(BlueprintCallable, Category = "Texture By Name")
		static UTexture2D * GetTextureByName(const FString& Path, const FString& TextureName);
	
	
};

Still receiving multiple errors :frowning:

Is there something I’m doing wrong?

Sorry, I assumed you have a little understanding of programming so I kept it short, the <> brackets where part of the placeholder, should have made that more clear, my bad, and you forgot to add a “public:” in your .h file. Things programmers would notice, anyways, this it how it should look like exactly:

.cpp file:

 #include "MyProject2.h"
 #include "TextureByName.h"
 
 UTexture2D * UTextureByName::GetTextureByName(const FString& Path, const FString& TextureName)
 {
 
     FString AssetPath = "Texture2D'/{Path}/{TextureName}.{TextureName}'";
     AssetPath = AssetPath.Replace(TEXT("{TextureName}"), *TextureName).Replace(TEXT("{Path"), *Path);
     FStringAssetReference AssetRef(AssetPath);
 
     return Cast<UTexture2D>(AssetRef.TryLoad());
 }

.h file:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "Kismet/BlueprintFunctionLibrary.h"
 #include "TextureByName.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class MYPROJECT2_API UTextureByName : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()
 
public:    
         UFUNCTION(BlueprintCallable, Category = "Texture By Name")
         static UTexture2D * GetTextureByName(const FString& Path, const FString& TextureName);
     
     
 };

In Blueprint you can use a data sheet and get all info allready set inside with the node get data table row, with your string result. I can make you an exemple if needed

Thanks for the solution , it worked perfectly!

I’d love to have a look after trying out solution.
Thanks!

This way you would still have to create and maintain the list of textures, create an entry for each of the hundred textures and map them to their names.

Not at all !!