![8ad66803904bd050cc8df912f5e2e358.png](https://i-blog.csdnimg.cn/blog_migrate/721e7a5bd47729c2f7892afd33a13a6f.jpeg)
1. Overview
上一期讲述了CustomThunk方式实现泛型蓝图节点中前二部分,泛型蓝图节点的声明以及自定义Thunk函数体的方法,本期将继续介绍第三部分:泛型函数(Generic)的实现。虽然不同泛型蓝图节点的功能千差万别,泛型函数代码也可能迥然不同,但是ue4编写中泛型函数与编写一般的c++函数完全相同。接下来将结合具体实例(如下图所示的3个泛型蓝图节点),让大家更全面的了解泛型蓝图节点的实现过程。特别提醒:在查看本期内容之前,请先了解上一期的内容。
![2513b5bb7fded15b757159be5835c953.png](https://i-blog.csdnimg.cn/blog_migrate/4c2023c8024aab4aca790e5e1a9377ce.png)
2. Approach
2.1 Struct to Json String节点,功能:将任意类型结构体UStruct转换Json格式字符串
![9c6c00605b581013568590f2494d6f36.png](https://i-blog.csdnimg.cn/blog_migrate/a72aab2921b1022511845c3a25aa5d83.jpeg)
![e9be4ee01462f0bec14d100f4c8df764.png](https://i-blog.csdnimg.cn/blog_migrate/e30cd3d4a68b82dc467f46bee1665ec2.png)
首先,在该模块的Build.cs文件中引入JsonUtilities,和Json模块,如下代码所示位置
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
"JsonUtilities",
"Json",
}
);
由于UStruct to Json String的泛型函数,在源代码JsonObjectConverter.h中已经存在了,所以无需自己编写泛型函数,直接调用即可。
代码实现如下:
//h 文件,引入头文件 #include "JsonObjectConverter.h"
/**
* Save any type of struct object to JSON format string, no struct type restriction
*
* @param StructReference The UStruct instance to read from
* @return JSON String Json Object string to be filled in with data from the ustruct
*/
UFUNCTION(BlueprintPure, CustomThunk, meta = (CustomStructureParam = "StructReference", DisplayName = "Struct to JSON String"), Category = "File|Json")
static void UStructToJsonObjectString(const int32& StructReference, FString& JSONString);
/// Custom execFunciton thunk for function UStructToJsonObjectString.
DECLARE_FUNCTION(execUStructToJsonObjectString)
{
//Get input wildcard single variable
Stack.Step(Stack.Obj