Get type of TSubclassOf<> object

Good day!
I have a structure:

USTRUCT(BlueprintType)
struct FNotifyFunc
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Animation")
	TSubclassOf<UBaseAnimNotify> Notify;
	
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Animation")
	FName FuncName;
};

USTRUCT(BlueprintType)
struct FAnimations
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Animation")
	FName NameOfAnimation;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Animation")
	UAnimMontage* AnimMontage;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Animation")
	TArray<FNotifyFunc> AnimNotifies;
};

I have multiple child classes based on UBaseAnimNotify, each for every NotifyEvent.

Every animation has a set of notifies (of unique classes) and I want to initialize them in the begining of the game bu iterating through structures:

void ABaseRangeWeapon::InitAnimations()
{
	for (const FAnimations AnimationSet : Animations)
	{
		if (!AnimationSet.AnimNotifies.Num()) continue;
		for (const FNotifyFunc NotifySet : AnimationSet.AnimNotifies)
		{
			if (auto Notify = AnimUtils::FindNotifyByClass<decltype(NotifySet.Notify->GetDefaultObject())>(AnimationSet.AnimMontage))
			{
				Notify->GetDefaultObject()->OnNotified.AddUFunction(this, NotifySet.FuncName);
				continue;
			}
			//UE_LOG()
			checkNoEntry();
		}
	}
}

AnimUtils::FindNotifyByClass function:

template<typename T>
	static T* FindNotifyByClass(UAnimMontage* Animation)
	{
		if (Animation)
		{
			for (auto NotifyEvent : Animation->Notifies)
			{
				if (auto  AnimationNotify = Cast<T>(NotifyEvent.Notify))
				{
					return AnimationNotify;
				}
			}
		}
		return nullptr;
	}

The problem is with TSubclassOf.
I want to pass type of child class to the FindNotifyByClass().

If I understood the question correctly that’s not possible. The compiler should resolve <decltype(NotifySet.Notify->GetDefaultObject())> to UBaseAnimNotify* but it will never be able to take the actual runtime value of NotifySet.Notify into account and change the return type of FindNotifyByClass from it.

From what your code looks like you don’t even have to do this since you don’t actually access any properties of the casted-to type assuming OnNotified is already declared in UBaseAnimNotify. The only place where the subclassof actually matters is the cast in FindNotifyByClass but you can also write it like this to achieve the same effect:

	static UBaseAnimNotify* FindNotifyByClass(UAnimMontage* Animation, TSubclassOf<UBaseAnimNotify> NotifyClass)
	{
		if (Animation)
		{
			for (auto NotifyEvent : Animation->Notifies)
			{
				UBaseAnimNotify* AnimationNotify = Cast<UBaseAnimNotify>(NotifyEvent.Notify);
				if (AnimationNotify && AnimationNotify->IsA(*NotifyClass))
				{
					return AnimationNotify;
				}
			}
		}
		return nullptr;
	}

Yes, that solved!
Thank you very much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.