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)

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值