i want to make a generic function called FindAsset(FString name) which will return the object of the asset. Inside of it, i use the ConstructorHelper::FObjectFinder. Although everything is good, i cant put the name into the TEXT() area… What should i do? I have faced this problem again when naming an object with CreateDefaultSubobject. I can only write it down but not use a FString variable…
With a generic function you can’t avoid casting the referenced asset to proper type and checking for null pointer.
Use as many FObjectFinder as you want or create UPROPERTY member variables and set them manually. (The latter is better because it can track path changes, hard coded string can’t.)
Anyway, here is the code:
UObject* AMyActor::FindAsset(FString Name)
{
static ConstructorHelpers::FObjectFinder<UObject> MyAsset((TEXT("%s"), *Name)); // create and set only one static variable, I don't think this is the right way
return MyAsset.Object; // nullptr and casting!
}