I would like to create a Blueprint node using C++ that I can pass a string reference to an asset (specifically for Materials and Media Players) that will return the asset reference itself.
For example, if I pass this string (for Material):
Material'/Game/Movies/Alan_Senauke_001_Tex_Mat.Alan_Senauke_001_Tex_Mat'
or this string (for Media Player):
MediaPlayer'/Game/Movies/Alan_Senauke_001.Alan_Senauke_001'
Then it would return a reference to the Material, or the Media Player, respectively.
This is for initialization of lots of objects at the start of the game. I was attempting to do it with a struct and Data Table, but Data Tables seem to be very unstable and are crashing the project repeatedly and inconsistently.
If I can create a node like this I can avoid Data Tables altogether.
Does anyone know if this is possible?
EDIT:
I’ve been trying 's suggestion, but it’s not showing up in BP:
I’ve tried using the code you’ve suggested, but for some reason it’s not appearing in Blueprint. Here is the full code for what I’m trying. I’m including another function here in the code, because that one is showing up in Blueprint.
MyBlueprintFunctionLibrary.h
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"
/**
*
*/
UCLASS()
class ASSETLOADER_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// Load a material from the corresponding path
UFUNCTION(BlueprintCallable, Category = "Asset Loading")
UMaterial * LoadMaterialReference(const FString& materialPath);
UFUNCTION(BlueprintCallable, Category = "Asset Loading")
static bool SaveStringTextToFile(FString fileName, FString SaveText, bool AllowOverWriting);
};
MyBlueprintFunctionLibrary.cpp
#include "AssetLoader.h"
#include "MyBlueprintFunctionLibrary.h"
UMaterial * UMyBlueprintFunctionLibrary::LoadMaterialReference(const FString& materialPath)
{
FStringAssetReference assetRef(materialPath);
return Cast<UMaterial>(assetRef.TryLoad());
}
bool UMyBlueprintFunctionLibrary::SaveStringTextToFile(FString fileName, FString SaveText, bool AllowOverWriting)
{
FString path;
path = FPaths::GameDir();
path += "/User_Playlists";
if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
{
FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*path);
if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
{
return false;
}
}
path += "/";
path += fileName;
path += ".playlist";
if (!AllowOverWriting)
{
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
{
return false;
}
}
return FFileHelper::SaveStringToFile(SaveText, *path);
}
Here is how it looks in BP after compiling:
Any idea why it doesn’t show up?