Download

Can't repeat simpest functionality

Guys, help me please. I’m so exhausted. Didn’t sleep for about 1 week trying to solve easy problem.

All I need is object reference from file path. I’ve tried a lot of examples but nothing works for me. Where am I wrong?

First variant:


 .h
UCLASS()
class LOADBRO_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
		// Load a material from the corresponding path
		UFUNCTION(BlueprintCallable, Category = "Asset Loading")
		static UMaterial * LoadMaterialReference(const FString& materialPath);

	
};
 .cpp
UMaterial * UMyBlueprintFunctionLibrary::LoadMaterialReference(const FString& materialPath)
{
	FStringAssetReference assetRef(materialPath);
	return Cast<UMaterial>(assetRef.TryLoad());
}


Second one:


.h
	UPROPERTY(EditAnywhere, Category = "Test")
		FString Path;

	UPROPERTY(EditAnywhere, Category = "Test")
		UStaticMesh* TestMesh;

	virtual void OnConstruction(const FTransform& Transform) override;
.cpp
void ALoadBroCharacter::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

	TestMesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), nullptr, *Path));
}

The result should be EF1k5cO.gif

Maybe I don’t understand something very basic at the ue4 coding. UE v4.12.5, VS2015.v3.upd3

And yeah I test this functionality wuth basic source files. Like 1M_Cube.uasset or CubeMaterial.uasset. And path like this 6048300d5dcca1d2196a37844a4edbbc365c3cd9.png

hm… shouldn’t be path be relative to the Content folder and start with /Game ?



TAssetPtr<UMaterial> MyMat;

static ConstructorHelpers::FObjectFinder<UMaterial> Asset(TEXT("/Relative/Path/MyMaterial.MyMaterial"));
if (Asset.Succeeded()) {MyMat = Asset.Object;}




TAssetSubclassOf<UMaterial> MyMat;

SomeMat = MyMat.Get();


So the essence of problem is “/” instead windows “” in the path and crazy “MyMaterial.MyMaterial” instead “MyMaterial.uasset” names. Wow… I suppose there will be a problem with package project after all) I hope this user will suffer for offering different paths and told everyone it works!

You can get this “crazy” name/path by rightclicking the Asset in the Editor and searching for something called “Copy Reference”.
This is not copying the ASSET itself, but you will get a Path to your Clipboard, which should look similar to what you search.

This AnswerHUB posts seems to deal with the same problem of getting the correct name for loading, maybe that helps you:

https://answers.unrealengine.com/questions/182569/how-do-i-get-the-folder-path-of-an-actor.html

I should look around AH better then) because I believed this guy that his function works)

BTW what about cooked project? It suppose if you use editor-format relative path there will be problem with getting reference for nocooked object. Or there is a AddToPackage/DLC system so you can scan your folders after that and load/get refs?

Ok guys. I don’t understand one thing still… Why C++ coding is SO difficult? It tooks 10 hours to make same functionality as a one BP node. It’s all about over 9000 classes and methods and literally no docs or even examples for them( I can’t make one stupid feature for 2 weeks. In this time i can make a building system or inventory with complicated 3D GUI. ****…

That Answerhub question has nothing to do with what you want.
You want to load an object from its path, not list all files within a OS folder.

The engine has builtin asset system to manage assets, using Windows common path logic won’t work the way you think.
Any asset you have in game are inside /Game/ directory; In Editor or packaged game, it doesn’t matter.
“Relative path” means folders within the “/Game/” root path, it has nothing to do with your C:… windows directory.

“MyMaterial.MyMaterial” means ‘PackageName.UObject’;
“MyMaterial.MyMaterial_C” means ‘PackageName.UClass’.

‘.UAsset’ may have a lot of objects in it, so there’s no reason to think the uasset is the object you want to load. Instead, you take its package and try to load the default object or class in it.
There’s no documentation about this because if you look around in engine code it is pretty clear, makes a lot of sense if you’re used to work with assets in another game engines.

I’m not sure I understand you clearly Bruno. I can use “C:… windows directory” as a absolute path parameter to take a list of files after FPaths::NormalizeDirectoryName. You get this paths after FPaths::ConvertRelativePathToFull so it makes any sense to use it. But the thing is I can’t find any methods to make relative path automatically. There is simple and perfect GetPathName in BP but no such thing in the C++ universe. I thought FPaths::ConvertToRelativePath could work but… The only way to make acceptable parameter to StaticLoadObject(UStaticMesh::StaticClass(), nullptr, *Path) is string mathematics?

FString UKismetSystemLibrary::GetPathName(const UObject* Object) is the C++ equivalent to the BP node GetPathName, unless they are “written” in BP, all nodes have some representation in C++.

I’m wondering though, if you already have the Object, what’s the point in using a pathname (unless it is for async loading or something along those lines)?
In the example shown in the gif, would it not be easier if you could directly select the StaticMesh for the Test Mesh property with a preview rather than first having to find the path for that mesh you want to use and then copy-paste it into the Path property?

I’m wondering where did you find that I have the Object. This is my goal. Get object references from any folder. Whole problem is in using right function with right parameter. It seems I found function, but can’t generate “normal” relative path to it.