一.UObject
UObject是所有對象的基類,往上還有UObjectBaseUtility。
注釋:所有虛幻引擎對象的基類。對象的類型由基于?UClass 類來定義。
這為創建和使用UObject的對象提供了 函數,并且提供了應在子類中重寫的虛函數。
/** * The base class of all UE objects. The type of an object is defined by its UClass.* This provides support functions for creating and using objects, and virtual functions that should be overridden in child classes.* * @see https://docs.unrealengine.com/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Objects*/
class UObject : public UObjectBaseUtility
為 UObject 提供工具類的函數,不應直接使用此類。
/*** Provides utility functions for UObject, this class should not be used directly*/
class UObjectBaseUtility : public UObjectBase
{
為更底層接口提供給UObject,也不能直接使用
/** * Low level implementation of UObject, should not be used directly in game code */
class UObjectBase
{
反射系統直接,一般是收集UObject的信息。是在運行的時候,拿到運行的對象注冊的信息。將這個信息寫入對象的表,隨時可以取到,訪問。
二.UCLASS 宏的理解
The UCLASS Macro
The?UCLASS?macro gives the?UObject
?a reference to a?UCLASS
?that describes its Unreal-based type. Each?UCLASS
?maintains one Object called the?Class Default Object(CDO). The CDO is essentially a default 'template' Object, generated by the class constructor and unmodified thereafter. Both the UCLASS and the CDO can be retrieved for a given Object instance, although they should generally be considered read-only. The UCLASS for an Object instance can be accessed at any time using the?GetClass()
?function.
A?UCLASS
?contains a set of properties and functions that define the class. These are normal C++ functions and variables available to standard C++ code, but tagged with Unreal Engine-specific metadata that controls how they behave within the Object system. For more details about the tagging syntax, refer to the?Programming
UCLASS宏為 UObject 提供一個對 UCLASS 的引用,而UCLASS描述了一個基于虛幻引擎的類型。 每個 UCLASS 都持有一個?類默認對象(Class Default Object)?,簡稱CDO。CDO 本質上是一個默認的 “模板” 對象,由類的構造函數生成,隨后保持不變。盡管UCLASS和CDO通常都會被看做是“只讀”的,但是每個Object實例都可以取得他們的UCLASS和CDO。你隨時都可以使用 GetClass() 函數來訪問Object實例的UCLASS。
UCLASS 包含了一套用于定義這個類的屬性和函數——他們就是原生的C++函數與變量,但被虛幻引擎特有的元數據所標記,它們在UObject系統中的行為也因此受到控制。如需了解標記語法的更多細節,請查閱?Programming Reference。
需要注意 UObject 類也可以包含?native-only(譯注:應該是指C++原生的,未被標記的屬性或函數),這些屬性不存在于相應的 UCLASS 中。
三.個人驗證解讀
重點是 類的構造函數生成,隨后保持不變。每個Object實例都可以取得他們的UCLASS和CDO。?原生的C++函數與變量,但被虛幻引擎特有的元數據所標記,它們在UObject系統中的行為也因此受到控制。
上測試代碼
// Sets default values
AMyActor::AMyActor():money(200)
{// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;RootComponent = CreateDefaultSubobject<USceneComponent>("Root");money = 300;
}int32 AMyActor::GetCurrentMoney()
{return money;
}int32 AMyActor::GetMyCDOMoney()
{UClass* GWXClass = AMyActor::StaticClass();UObject* MyObject = GWXClass->GetDefaultObject();AMyActor* MyCDO = Cast<AMyActor>(MyObject); if (MyCDO){return MyCDO->money;}return -1;
}// Called when
GetCurrentMoney(),在編輯器上使用,會修改它的不同實例返回值。但使用GetMyCDOMoney(),通過CDO創建時,都是構造函數中的300。
因為藍圖的操作,反射等。都是通過構造函數后,重寫編譯寫在MyActor.gen.cpp文件中,CDO是默認的實例對象,不會改變。我們在編輯其中,修改。再在關卡藍圖里測試都是,通過了這個文件拿到后續再處理。甚至藍圖運行時,斷點都能打到這個文件上。說明藍圖其實就是從這里來的。由此我們可以初見,反射系統的神奇與巧妙。