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

相关文章

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

一文全面详解Python变量作用域

《一文全面详解Python变量作用域》变量作用域是Python中非常重要的概念,它决定了在哪里可以访问变量,下面我将用通俗易懂的方式,结合代码示例和图表,带你全面了解Python变量作用域,需要的朋友... 目录一、什么是变量作用域?二、python的四种作用域作用域查找顺序图示三、各作用域详解1. 局部作

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P