How to get StaticMesh reference from the path of .uasset

I give up Epic Games. Your “helpful C++” got me. I’ve read a lot of topics and docs about that:

but all solutions don’t work.

I can get a list of my files. So I have an array of Fstring, but trying that:

.h
    UCLASS()
    class BATTERYCOLLECTOR_API UMyGameInstance : public UGameInstance
    {
    	GENERATED_BODY()
    public:
    		// Load a material from the corresponding path
    		UFUNCTION(BlueprintCallable, Category = "Asset Loading")
    		static UStaticMesh * LoadStaticMeshReference(const FString& materialPath);
    };

.cpp
UStaticMesh * UMyGameInstance::LoadStaticMeshReference(const FString& materialPath)
{
	FStringAssetReference assetRef(materialPath);
return Cast<UStaticMesh>(assetRef.TryLoad());
}

nothing happens. I tried to load using your favorite TAssetPtr but I don’t understand how to create it with FStringAssetReference? What is the

/**  
 * Copy from a unique object identifier
 * @param ObjectID Object identifier to create a weak pointer to
 */
FORCEINLINE void operator=(const FStringAssetReference &ObjectID)
	{
		AssetPtr = ObjectID;
	}

means?

What is the most simple and understandable way to get and load ObjectReference from any folders I want?

Did you try this?

UYourObjectClass* YourObject = Cast<UYourObjectClass>(StaticLoadObject(UYourObjectClass::StaticClass(), nullptr, *PathToYourObject));

I’m using this in one of my projects and it works without any problems.

Path I used here looks like:

/Game/TestAssets/TeamAttitudes_1.TeamAttitudes_1

for object:

I didn’t test it in static function tho.

Didn’t work.

Can you be more specific? It didn’t compile? It didn’t load the object? It doesn’t work in static function? It’s not what you want or what?

It does work for me:

When I use it like so

Header:

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

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

    virtual void OnConstruction(const FTransform& Transform) override;

Source:

void ACharacterBase::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

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

LoadStaticMeshReference returns NULL.
assetRef.isValid returns TRUE.

I’ve moved it to static function and it still works fine.

Can you show me where you use it? Also, double check if path is correct. I see that your variable for path is named ‘materialPath’ yet, you’re searching for static meshes. Dunno if you didn’t overlook it somewhere and you maybe still passing path to material or smth.

I use this function in BP. for example in the actor

Afaik this method won’t work for absolute path of a file. It expects a relative path of an asset in current project.

If the file you’re trying to load is in project where you use that function, then you could convert that path to a relative one that is acceptable by this function.

Otherwise I have no other idea for now.

But it doesn’t work with a relative too)))

Upgraided VS to 2015 and tried exactly your code into blank 3Dcharacter but it doesn’t work at all. Idk man… What is going on here) This bug drives me crazy.

Could you upload that blank project?

Okay, I’ll take a look at it in the evening.

LoadBro.rar — Yandex.Disk Here you are.

You just sent me a project with no source. How am I supposed to check why your code doesn’t work if you didn’t send it?

I think source isn’t different on the same versions but i’m not sure in everything now) LoadBro.rar — Yandex.Disk

I tested it and it works. :stuck_out_tongue:

Implementation, usage, and mat location:

Result in game:

Links to pics as these weren’t added automatically :<

Wtf… How… Emm… “/” instead “” ? What is the Testmat.TestMat? And if only have ***.uasset files?
Ok whatever. What about StaticMesh? Could you add static mesh component at character?

This function will work only for asset files, yes.

If you want to search for static mesh using this, simply change UMaterial to UStaticMesh.

TestMat.TestMat is… a way references to assets are represented in UE4. I don’t know why to be honest. Basically you have to repeat your asset name after a dot. So since I was searching for material called ‘TestMat’, my path contained ‘[…]/TestMat.TestMat’ at the end.

Where could I found this information about different backslashes and this ■■■■ extensions… wtf((( It took me 2 weeks to solve this problem! Nobody told me about proper path format… All specs tried to offer a different solution and noone didn’t look to the essence. LMAO ))
Thank you anyways) You’re the best, man)