//FunctionClassName为具体执行方法的组件名字 例如:MyRadarComponent
//data是一个结构体 记录了所要执行方法的名字(例如:test)和具体要执行方法的组件名字 没有也可以 直接调用 下边代码修改下就行
//具体调用是填充data数据结构,例如要执行方法的组件名字,要执行的方法名,方法名样式:test 或者 test(1)或者test(1,1),多个参数逗号隔开,括号记得写
UObject* FunctionObject = data->FunctionClassName.IsNone() ? this : GetOwner()->GetDefaultSubobjectByName(data->FunctionClassName);
if (!FunctionObject)
{
UE_LOG(LogFlight, Warning, TEXT("This Component(%s) does't exist,Please check your data"), *data->FunctionClassName.ToString());
//确保FunctionObject不为空
FunctionObject = this;
}
UFunction* Func = nullptr;
//检查方法名是否存在
if (data->FunctionNames.IsValidIndex(data->NextState))
{
//查找对应按键方法
FName FunctionName = data->FunctionNames[data->NextState];
if (!FunctionName.IsNone())
{
//区分方法名和参数
FString functionNameFullStr = FunctionName.ToString();
FString FunctionStr;
FString rightStr;
TArray<FString> paramArray;
bool bSuccess = functionNameFullStr.Split(TEXT("("), &FunctionStr, &rightStr);
if (bSuccess)
{
FString paramStr;
FString rightStr2;
bool bSuccess2 = rightStr.Split(TEXT(")"), ¶mStr, &rightStr2);
if (bSuccess2)
{
paramStr.ParseIntoArray(paramArray, TEXT(","), false);
}
else
{
int num = rightStr.ParseIntoArray(paramArray, TEXT(","), false);
if (num == 0)
{
paramArray.Add(rightStr);
}
}
}
else
{
FunctionStr = functionNameFullStr;
}
Func = FunctionObject->FindFunction(FName(FunctionStr));
if (Func)
{
void* buf = FMemory_Alloca(Func->ParmsSize);
FMemory::Memzero(buf, Func->ParmsSize);
int i = 0;
for (TFieldIterator<FProperty> PropertyIterator(Func); PropertyIterator; ++PropertyIterator, i++)
{
FString* p = PropertyIterator->ContainerPtrToValuePtr<FString>(buf);
if (paramArray.IsValidIndex(i))
{
*p = paramArray[i];
}
else
{
*p = FString();
}
}
FunctionObject->ProcessEvent(Func, (void*)buf);
}
else
{
// PRINTWARNING(TEXT("This cockpit button does't have function"));
UE_LOG(LogFlight, Warning, TEXT("This Component(%s) does't have function:%s"), *FunctionObject->GetName(), *data->FunctionNames[data->NextState].ToString())
}
}
}