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

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM