UE 反射应用
方法一:
UFunction* bpFunc = ik_actor->GetClass()->FindFunctionByName(FName("GetReturnValue"));
if (!bpFunc)
{
return -1;
}
struct EventFunc_Params
{
FString Args;
FString ReturnValue;
} Func_Params;
ik_actor->ProcessEvent(bpFunc, &Func_Params);
方法二:
UFunction* bpFunc = ik_actor->GetClass()->FindFunctionByName(FName("GetReturnValue"));
if (!bpFunc)
{
return -1;
}
FStrProperty* returnValue = nullptr;
for (TFieldIterator<UProperty> propIt(bpFunc, EFieldIteratorFlags::ExcludeSuper); propIt; ++propIt)
{
UProperty* property = *propIt;
bool isOut = property->HasAnyPropertyFlags(CPF_ReturnParm | CPF_OutParm);
if (!isOut)
{
continue;
}
FStrProperty* strProperty = CastField<FStrProperty>(property);
if (strProperty)
{
returnValue = strProperty;
}
}
FStructOnScope funcParam(bpFunc);
ik_actor->ProcessEvent(bpFunc, funcParam.GetStructMemory());
if (returnValue != nullptr && IsValid(bpFunc))
{
FString value = returnValue->GetPropertyValue_InContainer(funcParam.GetStructMemory());
if (value.IsEmpty())
{
GEngine->AddOnScreenDebugMessage(9840, 10.0, FColor::Red, "Value is Empty!", false);
}
else
{
FString Text = "Value is: " + value;
GEngine->AddOnScreenDebugMessage(9840, 10.0, FColor::Red, Text, false);
UE_LOG(LogTemp, Warning, TEXT("%s"), *value);
return 0;
}
}