UE4尝试用C++创建蓝图并添加变量

2024-09-06 23:18
文章标签 c++ ue4 创建 尝试 变量 蓝图

本文主要是介绍UE4尝试用C++创建蓝图并添加变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目标

使用C++创建蓝图资源,并尝试给蓝图添加变量。
(引擎版本4.26)

步骤0. 创建测试用插件

使用编辑器工具栏按钮为模板创建插件。
在这里插入图片描述
使用这个模板的理由是它初始会有个按钮,可以用来触发操作:
在这里插入图片描述
之后将会替换这其中所触发的操作。

步骤1. 创建蓝图

KismetEditorUtilities.h进行include,因为创建蓝图需要用到其中的方法:

#include"Kismet2/KismetEditorUtilities.h"

AssetRegistryModule.h进行include,因为需要通知蓝图资源的创建。

#include"AssetRegistryModule.h"

随后,将按钮触发的操作替换为:

//资源名:
const FString AssetName = "YaksueTestBP";
//package路径名:
const FString PackageName = "/Game/" + AssetName;//创建Package
UPackage * Package = CreatePackage(nullptr, *PackageName);//创建蓝图
UBlueprint* NewBlueprint = FKismetEditorUtilities::CreateBlueprint(AActor::StaticClass(),						//ParentClass					the parent class of the new blueprintPackage,									//Outer							the outer object of the new blueprintFName(*AssetName),							//NewBPName						the name of the new blueprintEBlueprintType::BPTYPE_Normal,				//BlueprintType					the type of the new blueprint (normal, const, etc)UBlueprint::StaticClass(),					//BlueprintClassType			the actual class of the blueprint asset (UBlueprint or a derived type)UBlueprintGeneratedClass::StaticClass(),	//BlueprintGeneratedClassType	the actual generated class of the blueprint asset (UBlueprintGeneratedClass or a derived type)FName("YaksueTestContext"));				//CallingContext				the name of the calling method or module used to identify creation methods to engine analytics/usage stats (default None will be ignored)//通知这个蓝图资源创建了
FAssetRegistryModule::AssetCreated(NewBlueprint);//编译蓝图
FKismetEditorUtilities::CompileBlueprint(NewBlueprint);

效果:点击按钮后会创建一个空白的蓝图:
在这里插入图片描述

步骤2. 为蓝图添加变量

关于“添加变量”这个功能,我翻阅代码找到了 FBlueprintEditorUtils::AddMemberVariable 接口。

通过字符串搜索,我找到了一处使用范例,在\UE_4.26\Engine\Plugins\Tests\EditorTests\Source\EditorTests\Private\UnrealEd\EditorBuildPromotionTests.cpp
在这里插入图片描述
关于变量的类型,通过UEdGraphSchema_K2::PC_String指定为“字符串”。


需要添加BlueprintGraph这个模块到TestCppBP.Build.csPrivateDependencyModuleNames中。因为UEdGraphSchema_K2::PC_String等其他的类型实际是个名字,为 static:
在这里插入图片描述
其值在EdGraphSchema_K2.cpp中指定:
在这里插入图片描述
如果不添加BlueprintGraph这个模块,UEdGraphSchema_K2::PC_String等的值无法知道。


仿照范例添加字符串变量:

FEdGraphPinType StringPinType(UEdGraphSchema_K2::PC_String,		//PinCategory				Category of pin typeNAME_None,							//PinSubCategory			Sub-category of pin typenullptr,							//PinSubCategoryObject		Sub-category objectEPinContainerType::None,			//ContainerTypefalse,								//bIsReference				Whether or not this pin is a value passed by reference or notFEdGraphTerminalType());			//PinValueType				Data used to determine value types when bIsMap is trueFBlueprintEditorUtils::AddMemberVariable(NewBlueprint, "TestString", StringPinType);

效果,成功添加:
在这里插入图片描述

最终代码

...
#include"Actor.h"
#include"Kismet2/KismetEditorUtilities.h"
#include"Kismet2/BlueprintEditorUtils.h"
#include"AssetRegistryModule.h"
...
void FTestCppBPModule::PluginButtonClicked()
{//资源名:const FString AssetName = "YaksueTestBP";//package路径名:const FString PackageName = "/Game/" + AssetName;//创建PackageUPackage * Package = CreatePackage(nullptr, *PackageName);//创建蓝图UBlueprint* NewBlueprint = FKismetEditorUtilities::CreateBlueprint(AActor::StaticClass(),						//ParentClass					the parent class of the new blueprintPackage,									//Outer							the outer object of the new blueprintFName(*AssetName),							//NewBPName						the name of the new blueprintEBlueprintType::BPTYPE_Normal,				//BlueprintType					the type of the new blueprint (normal, const, etc)UBlueprint::StaticClass(),					//BlueprintClassType			the actual class of the blueprint asset (UBlueprint or a derived type)UBlueprintGeneratedClass::StaticClass(),	//BlueprintGeneratedClassType	the actual generated class of the blueprint asset (UBlueprintGeneratedClass or a derived type)FName("YaksueTestContext"));				//CallingContext				the name of the calling method or module used to identify creation methods to engine analytics/usage stats (default None will be ignored)//通知这个蓝图资源创建了FAssetRegistryModule::AssetCreated(NewBlueprint);//添加一个字符串类型的变量{FEdGraphPinType StringPinType(UEdGraphSchema_K2::PC_String,		//PinCategory				Category of pin typeNAME_None,							//PinSubCategory			Sub-category of pin typenullptr,							//PinSubCategoryObject		Sub-category objectEPinContainerType::None,			//ContainerTypefalse,								//bIsReference				Whether or not this pin is a value passed by reference or notFEdGraphTerminalType());			//PinValueType				Data used to determine value types when bIsMap is trueFBlueprintEditorUtils::AddMemberVariable(NewBlueprint, "TestString", StringPinType);}//编译蓝图FKismetEditorUtilities::CompileBlueprint(NewBlueprint);
}
...

这篇关于UE4尝试用C++创建蓝图并添加变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143400

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(