Hey Unreal Devs!
Just wanted to contibute some helpful knowledge I wasn’t able to find on the net myself.
If you need to Evaluate Choosers with a struct parameter output in C++ look no further than below! (As of current 5.5.4 Unreal Version).
// Very important to ensure the struct output matches the chooser output!
template<class T>
static UAnimationAsset* EvaluateAnimInstanceChooser(UAnimInstance* AnimInstance, UChooserTable* ChooserTable,
T& StructOutput)
{
if (!IsValid(AnimInstance) || !IsValid(ChooserTable))
{
UE_LOG(LogTemp, Error, TEXT("[YourClassName]::EvaluateAnimChooser - Invalid chooser evaluation data"));
return nullptr;
}
const FInstancedStruct ChooserInstance = UChooserFunctionLibrary::MakeEvaluateChooser(ChooserTable);
FChooserEvaluationContext ChooserEvaluationContext = UChooserFunctionLibrary::MakeChooserEvaluationContext();
ChooserEvaluationContext.AddObjectParam(AnimInstance);
ChooserEvaluationContext.AddStructParam(StructOutput);
UAnimationAsset* ChooserAnimation {
Cast<UAnimationAsset>(UChooserFunctionLibrary::EvaluateObjectChooserBase(ChooserEvaluationContext, ChooserInstance,
UAnimationAsset::StaticClass()))
};
return ChooserAnimation;
}
// Very important to ensure the struct output matches the chooser output!
template<class T>
static TArray<UAnimationAsset*> EvaluateAnimInstanceChooserMulti(UAnimInstance* AnimInstance, UChooserTable* ChooserTable,
T& StructOutput)
{
TArray<UAnimationAsset*> ResultAnimations;
if (!IsValid(AnimInstance) || !IsValid(ChooserTable))
{
UE_LOG(LogTemp, Error, TEXT("[YourClassName]::EvaluateAnimChooser - Invalid chooser evaluation data"));
return ResultAnimations;
}
const FInstancedStruct ChooserInstance = UChooserFunctionLibrary::MakeEvaluateChooser(ChooserTable);
FChooserEvaluationContext ChooserEvaluationContext = UChooserFunctionLibrary::MakeChooserEvaluationContext();
ChooserEvaluationContext.AddObjectParam(AnimInstance);
ChooserEvaluationContext.AddStructParam(StructOutput);
const TArray<UObject*> ChooserObjects {
UChooserFunctionLibrary::EvaluateObjectChooserBaseMulti(ChooserEvaluationContext, ChooserInstance,
UAnimationAsset::StaticClass())
};
for (UObject* ChooserObject : ChooserObjects)
{
if (UAnimationAsset* ChooserAnimation = Cast<UAnimationAsset>(ChooserObject))
{
ResultAnimations.Add(ChooserAnimation);
}
}
return ResultAnimations;
}
If you don’t need a struct output just create another function without the StructOutput parameter and remove the “ChooserEvaluationContext.AddStructParam(StructOutput);” line.
Hope this can help some peeps.