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++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项