微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UE4 C++:UFUNCTION宏、函数说明符、元数据说明符

目录

UFUNCTION声明的作用

函数说明符(常见):

BlueprintCallable 蓝图可调用

BlueprintPure 蓝图纯函数(无执行线,必须要有返回值)

BlueprintImplementableEvent 蓝图可实现事件(覆盖C++)

BlueprintNativeEvent 蓝图原生事件(C++必须实现,蓝图也可以实现)

Exec 控制台可调用函数

meta说明符

ExpandEnumAsExecs:多执行流(多返回值)


UFUNCTION声明的作用

  • UFunction时UE反射系统可识别的C++函数。UObject或蓝图函数库可将成员函数声明为UFunction,方法是将 UFUNCTION 宏放在头文件函数声明上方的行中。宏将支持 函数说明符 更改UE4解译和使用函数的方式。
  • 可利用函数说明符将UFunction对蓝图可视化脚本图表公开,以便开发者从蓝图资源调用或扩展UFunction,而无需更改C++代码
  • 在类的属性中,UFunction可绑定到委托,从而能够执行一些操作(例如将操作与用户输入相关联)
  • 它们还可以充当网络回调,这意味着当某个变量受网络更新影响时,用户可以将其用于接收通知并运行自定义代码用户甚至可创建自己的控制台命令(通常也称 debug、configuration 或 cheat code 命令),并能在开发版本中从游戏控制台调用这些命令,或将拥有自定义功能的按钮添加到关卡编辑器中的游戏对象。
UFUNCTION([specifier1=setting1, specifier2, ...], [Meta(key1="value1", key2, ...)])
	ReturnType FunctionName([Parameter1, Parameter2, ..., ParameterN1=DefaultValueN1, ParameterN2=DefaultValueN2]) [const];

函数说明符(常见):

BlueprintCallable 蓝图可调用

  • 可设置返回值
  • 可通过参数形式,返回多个参数
UFUNCTION(BlueprintCallable, Category="methods")
	void FunBlueprintCallable1();

UFUNCTION(BlueprintCallable, Category = "methods")
	bool FunBlueprintCallable2(FString Path,float input1, const float& input2, float& output, TArray<int32> Points1, const TArray<int32>& Points2, TArray<FVector>& Position  );

BlueprintPure 蓝图纯函数(无执行线,必须要有返回值)

  • 没有执行引脚
  • 必须要有返回值
UFUNCTION(BlueprintPure, Category = "methods")
	float FunBlueprintPure1();

UFUNCTION(BlueprintPure, Category = "methods")
	void FunBlueprintPure2(float& Value);

BlueprintImplementableEvent 蓝图可实现事件(覆盖C++)

  • 无需再C++写实现函数,需要在蓝图override
  • 有返回值和无返回值 有所区别
UFUNCTION(BlueprintImplementableEvent, Category = "methods")
	void FunBlueprintImplementableEvent1();

UFUNCTION(BlueprintImplementableEvent, Category = "methods")
	void FunBlueprintImplementableEvent2(float& Value);

BlueprintNativeEvent 蓝图原生事件(C++必须实现,蓝图也可以实现)

  • C++中定义事件,C++和蓝图中都可以实现(C++必须实现)。
  • 如果蓝图不实现,会执行C++的函数实现
  • 如果蓝图和C++都实现,蓝图则会覆盖C++实现,只执行蓝图实现.
  • C++函数实现,需要额外定义一个名为:函数名+_Implementation的返回值和参数列表都一致的函数.
  • BlueprintNativeEvent需要配合BlueprintCallable一起使用,否则蓝图中不可调用
  • 有返回值和无返回值 表现形式有所区别
//displayName 蓝图中实际调用函数名
//DeprecationMessage显示警告信息
//此处参数用Fsting str 编译不通过
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "methods", Meta=(displayName="FunBlueprintNativeEvent测试",DeprecatedFunction, DeprecationMessage = "This FunBlueprintNativeEvent 的测试."))
	void FunBlueprintNativeEvent(const FString& str="From C++");
void AMyActor::FunBlueprintNativeEvent_Implementation(const FString& str)
{
	GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Blue, str);
}

Exec 控制台可调用函数

函数可从游戏中的控制台中执行。Exec命令仅在特定类中声明时才产生作用。包括

  • Pawns,
  • Player Controllers,
  • Player Input,
  • Cheat Managers,
  • Game Modes,
  • Game Instances,
  • overriden Game Engine classes,
  • Huds
UFUNCTION(Exec, Category = "methods")
	void FunExec(float Value);
void AMyPawn::FunExec(float Value)
{
	GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Blue, FString::Printf(TEXT("BPNative C++ Call  Value:%f"), Value));
}

Meta说明符

ExpandEnumAsExecs:多执行流(多返回值)

UENUM(BlueprintType)
enum class BranchOutput : uint8
{
	Branch0,
	Branch1,
	Branch2,
};

UFUNCTION(BlueprintCallable, Category = "methods", Meta = (ExpandEnumAsExecs = "Branches"))
		void FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches);
void AMyActor::FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches)
{
	if (Input == 0){
		Branches = BranchOutput::Branch0;
	}
	else if(Input == 1){
		Branches = BranchOutput::Branch1;
	}
	else{
		Branches = BranchOutput::Branch2;
	}
}

 

参考链接

虚幻引擎UFunction | 虚幻引擎5.0文档 (unrealengine.com)

【UE4 C++ 基础知识】<2> UFUNCTION宏、函数说明符、元数据说明符 - 砥才人 - 博客园 (cnblogs.com)

原文地址:https://www.jb51.cc/wenti/3284082.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐