【UE4 C++ 基础知识】<2> UFUNCTION宏、函数说明符、元数据说明符

UFunction声明

  • UFunction 是虚幻引擎4(UE4)反射系统可识别的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  );

image

BlueprintPure 蓝图纯函数(必须要有返回值)

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

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

image

BlueprintImplementableEvent 蓝图可实现事件

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

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

image

BlueprintNativeEvent 蓝图原生事件

  • 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);
}

image

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));
}

image


meta说明符

ExpandEnumAsExecs

h文件

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

UFUNCTION(BlueprintCallable, Category = "methods", Meta = (ExpandEnumAsExecs = "Branches"))
		void FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches);

cpp文件

void AMyActor::FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches)
{
	if (Input == 0)
	{
		Branches = BranchOutput::Branch0;
	}
	else if(Input == 1)
	{
		Branches = BranchOutput::Branch1;
	}
	else
	{
		Branches = BranchOutput::Branch2;
	}
}

image

WorldContext 简介获取当前世界

UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "MyBPAsyncAction")
		static UMyBPAsyncAction* AsyncCountdown(UObject* WorldContextObject, AAsyncTickActor* AsyncTickActor, int32 StartNum);

参考

posted @ 2021-04-22 21:03  砥才人  阅读(2026)  评论(0编辑  收藏  举报