FStringAssetReference::TryLoad failed on android device!

hey guys~

i have a problem.

why do i fail to my c++ code on android device

template<typename T> 
T* SpawnActorFromBlueprint(FString name, FVector location, FRotator rotator)
{
        FStringAssetReference itemRef(name);
	UObject *itemObj = nullptr;
	T *obj = nullptr;

	if (itemRef.TryLoad() != nullptr) // this line error on android device
	{
		itemObj = itemRef.ResolveObject();

		if (GetWorld() && itemObj)
		{
			UBlueprint *generatedBP = Cast<UBlueprint>(itemObj);
			obj = Cast<T>(GetWorld()->SpawnActor(generatedBP->GeneratedClass, &location, &rotator));
		}
	}
}

In use, SpawnActorFromBlueprint() call in other function which can call in blueprint

ue4 editor test play is good, no problem.

but when launch on android device. itemRef.TryLoad() function is error( fail to load)

why do i happen problem… how do you solve this problem???

Is your asset being cooked?

The editor will only cook assets that it can detect references to, and if you’re only referencing your asset by its name in code, then the editor won’t be able to detect that reference, and your asset won’t be cooked.

If you need to forcibly cook some assets, you can add the path(s) containing those assets to the “Additional Asset Directories to Cook” array in your project settings. “Project Settings” → “Packaging” → (Advanced Section) → “Additional Asset Directories to Cook”.

thank to answer :slight_smile:
i add to “Additional Asset Directories to Cook”
and run “File” → “Package Project” → “Android” → “Android All”
and run batch file(install device)
but fail still. :frowning:
what "s wrong …

I checked with the Blueprints team, and they say this is because Blueprint assets are editor-only and don’t actually appear in a cooked build.

What you’ll have in a cooked build is the Blueprint generated class (it’s UClass), and you should be able to find this using FStringClassReference, using the Blueprint asset name, followed by the “_C” suffix to denote that that it is a Blueprint generated class, eg) “/Game/MyBlueprint.MyBlueprint_C”.

ok.
i was have to use string which is “Blueprint’/Game/MyBP.MyBP’”
so i change string which is “/Game/MyBP.MyBP_C” and run play
then i get crash editor!, so start debugging… and i find error.

UBlueprint *generatedBP = Cast<UBlueprint>(itemObj);

generatedBP is null.
fail to cast UBlueprint class.

how do i do? T.T
help~

(do you know another way which load blueprint at runtime???(in C++))

additional,
error at SpawnActor function.
message is “SpawnActor failed because BlueprintGeneratedClass is not an actor class”

i resolve it !!!

my changed code.

template<typename T> T*
AActionPlayerController::SpawnActorFromBlueprint(FString name, FVector location, FRotator rotator)
{
	FStringClassReference itemRef(name);
	T *obj = nullptr;

	if (itemRef.TryLoadClass<T>() != nullptr)
	{
		UClass *itemClass = itemRef.ResolveClass();

		if (GetWorld() && itemClass)
		{
			obj = Cast<T>(GetWorld()->SpawnActor(itemClass, &location, &rotator));
		}
	}
	return obj;
}
  • change point

FStringAssetReference ===> FStringClassReference

.TryLoad() ===> .TryLoadClass()

.ResloveObject() ===> .ResloveClass()

spawn character on android device. !

Thank to Help Jamie!!! :slight_smile: