Load a TSubclassOf at runtime ?

Hi everyone,

I’m experiencing issues with loading assets, class …etc

So let’s make things clearier :

I have a custom FDataAsset which contains a TMap, FCustomStruct>

Here is my custom struct :

USTRUCT(BlueprintType)
struct FCameraConfigStruct
{
	GENERATED_BODY()

public:

	/** If we are outside of this range, we consider the camera as critical and enable the modifiers to bring it back */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera|Ranges")
		FFloatRange CriticalRange;

	/** If we were out of the critical range, the range we consider the camera back to its usual distance */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera|Ranges")
		FFloatRange TargetRange;

	/** List of modifiers to apply to this class */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera|Modifiers")
		TArray<TSubclassOf<UCameraModifier>> ModifiersToUse;

	FCameraConfigStruct()
	{
		TRangeBound<float> lowerBound(75.f), upperBound(300.f);

		CriticalRange = FFloatRange(
			TRangeBound<float>::Inclusive(75.f),
			TRangeBound<float>::Inclusive(300.f));

		TargetRange = FFloatRange(
			TRangeBound<float>::Inclusive(150.f),
			TRangeBound<float>::Inclusive(250.f));

		FStringClassReference stringRef(TEXT("/Game/Blueprints/Camera/Modifiers/CM_ApplyVolumeMods"));
		stringRef.TryLoadClass<TSubclassOf<UCameraModifier>>();

		UObject* modifierRef = LoadObject<UObject>(nullptr, TEXT("Blueprint'/Game/Blueprints/Camera/Modifiers/CM_ApplyVolumeMods.CM_ApplyVolumeMods'"));
		if (modifierRef != nullptr)
		{
			UE_LOG(LogTemp, Warning, TEXT("Modifier found !"));
			TSubclassOf<UCameraModifier>* modifier = Cast<TSubclassOf<UCameraModifier>>(modifierRef);
			if (modifier)
			{
				UE_LOG(LogTemp, Warning, TEXT("Modifier CASTED !"));
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Modifier Cast FAILED !"));
			}
		}
		//static ConstructorHelpers::FClassFinder<UCameraModifier> modifier(TEXT("Blueprint'/Game/Blueprints/Camera/Modifiers/CM_ApplyVolumeMods'"));
		//if (modifier.Succeeded())
		//{
		//}

	}

};

So first thing you need to know : UE4 doesn’t consider structs constructor as constructors so the ConstructorHelpers don’t work.

And I have many camera modifiers that I need to auto register for the designers (otherwise they will have to fill the array themselves for each character we are controlling in the game).

And because each modifier inherit from UCameraModifier I need to use TSubclassOf to fill that array (it works btw with manual filling)

So my question is : How can I load and store TSubclassOf at runtime ? I tried FStringClassReference, LoadObject LoadClass… I have the reference but I can’t cast them into TSubclassOf

So if anyone succeed to do it, you save my week.

Best regards,

Alex

I has class AMyActor

UCLASS()
class ANSWERHUB_API AMyActor : public AActor
{
	GENERATED_BODY()
}

it is BP inherit from AMyActor

293492-111.png

Next, load the path into TSubclassOf

	const FString MyActorBpPath = "/Game/Test/NewBlueprint.NewBlueprint_C";
	TSubclassOf<AMyActor> MyActorClass = LoadClass<AMyActor>(GetWorld(), *MyActorBpPath);

	UE_LOG(LogTemp, Warning, TEXT("Class Name is %s"), *MyActorClass->GetFName().ToString());

it is the log:

293493-1345.png

1 Like