HuldaGnodim 发表于 2022-12-14 15:51

unreal engine c++不修改蓝图的情况下调用蓝图多种方式研究

/方法原型int32 UMyClass::Func(float param1); UFUNCTION(BlueprintCallable)int32 InvokeFunction(UObject* obj, FName functionName,float param1){    struct MyClass_Func_Parms   //定义一个结构用来包装参数和返回值,就像在gen.cpp里那样    {      float param1;      int32 ReturnValue;    };    UFunction* func = obj->FindFunctionChecked(functionName);    MyClass_Func_Parms params;    params.param1=param1;    obj->ProcessEvent(func, &params);    return params.ReturnValue;}//使用int r=InvokeFunction(obj,"Func",123.f);/调用1obj->ProcessEvent(func, &params);//调用2FFrame frame(nullptr, func, &params, nullptr, func->Children);obj->CallFunction(frame, &params + func->ReturnValueOffset, func);//调用3FFrame frame(nullptr, func, &params, nullptr, func->Children);func->Invoke(obj, frame, &params + func->ReturnValueOffset);template<typename... TReturns, typename... TArgs>void InvokeFunction(UClass* objClass, UObject* obj, UFunction* func, TTuple<TReturns...>& outParams, TArgs&&... args){    objClass = obj != nullptr ? obj->GetClass() : objClass;    UObject* context = obj != nullptr ? obj : objClass;    uint8* outPramsBuffer = (uint8*)&outParams;    if (func->HasAnyFunctionFlags(FUNC_Native)) //quick path for c++ functions    {      TTuple<TArgs..., TReturns...> params(Forward<TArgs>(args)..., TReturns()...);      context->ProcessEvent(func, &params);      //copy back out params      for (TFieldIterator<UProperty> i(func); i; ++i)      {            UProperty* prop = *i;            if (prop->PropertyFlags & CPF_OutParm)            {                void* propBuffer = prop->ContainerPtrToValuePtr<void*>(&params);                prop->CopyCompleteValue(outPramsBuffer, propBuffer);                outPramsBuffer += prop->GetSize();            }      }      return;    }    TTuple<TArgs...> inParams(Forward<TArgs>(args)...);    void* funcPramsBuffer = (uint8*)FMemory_Alloca(func->ParmsSize);    uint8* inPramsBuffer = (uint8*)&inParams;    for (TFieldIterator<UProperty> i(func); i; ++i)    {      UProperty* prop = *i;      if (prop->GetFName().ToString().StartsWith("__"))      {            //ignore private param like __WolrdContext of function in blueprint funcion library            continue;      }      void* propBuffer = prop->ContainerPtrToValuePtr<void*>(funcPramsBuffer);      if (prop->PropertyFlags & CPF_OutParm)      {            prop->CopyCompleteValue(propBuffer, outPramsBuffer);            outPramsBuffer += prop->GetSize();      }      else if (prop->PropertyFlags & CPF_Parm)      {            prop->CopyCompleteValue(propBuffer, inPramsBuffer);            inPramsBuffer += prop->GetSize();      }    }    context->ProcessEvent(func, funcPramsBuffer);   //call function    outPramsBuffer = (uint8*)&outParams;    //reset to begin    //copy back out params    for (TFieldIterator<UProperty> i(func); i; ++i)    {      UProperty* prop = *i;      if (prop->PropertyFlags & CPF_OutParm)      {            void* propBuffer = prop->ContainerPtrToValuePtr<void*>(funcPramsBuffer);            prop->CopyCompleteValue(outPramsBuffer, propBuffer);            outPramsBuffer += prop->GetSize();      }    }}#include "OutputDevice.h"    FString cmd = FString::Printf(TEXT("TestFun HelloWorld"));    FOutputDeviceDebug device;    实例对象->CallFunctionByNameWithArguments(*cmd, device, NULL, true);//调用通过蓝图actor调用蓝图函数void AMyActor::CallActorFunc(AActor * c, FString funcName){    FOutputDeviceNull ar;    c->CallFunctionByNameWithArguments(*funcName, ar, NULL, true);}
https://zhuanlan.zhihu.com/p/61042237
https://www.unrealengine.com/en-US/search?x=0&y=0&filter=All&keyword=CallFunctionByNameWithArguments
https://docs.unrealengine.com/5.0/en-US/API/Runtime/CoreUObject/UObject/UObject/CallFunctionByNameWithArguments/
https://blog.csdn.net/qq_35014708/article/details/84569831
页: [1]
查看完整版本: unreal engine c++不修改蓝图的情况下调用蓝图多种方式研究