1

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;
    }

I tried to pass DefaultObject and change FindNotifyByClass to check UClasses (but I can't return object of required class). The problem is with TSubclassOf. I want to pass type of child class to the FindNotifyByClass().

  • It would be helpful if you would provide a clearer problem statement. There is not really a question in your text. You should mention some kind of expected result / result you get, which line causes an issue, etc. – goose_lake Aug 07 '23 at 08:05

0 Answers0