0
点赞
收藏
分享

微信扫一扫

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, ¶ms);
return params.ReturnValue;
}
//使用
int r=InvokeFunction(obj,"Func",123.f);

/调用1
obj->ProcessEvent(func, ¶ms);
//调用2
FFrame frame(nullptr, func, ¶ms, nullptr, func->Children);
obj->CallFunction(frame, ¶ms + func->ReturnValueOffset, func);
//调用3
FFrame frame(nullptr, func, ¶ms, nullptr, func->Children);
func->Invoke(obj, frame, ¶ms + 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, ¶ms);
//copy back out params
for (TFieldIterator<UProperty> i(func); i; ++i)
{
UProperty* prop = *i;
if (prop->PropertyFlags & CPF_OutParm)
{
void* propBuffer = prop->ContainerPtrToValuePtr<void*>(¶ms);
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/CallFunctionByNameWithArgu​​

举报

相关推荐

0 条评论