正如Unreal Engine文档所述:
类声明定义了类的名称、它继承自哪个类以及因此继承的任何函数和变量,以及通过类特定符号和元数据实现的其他引擎和编辑器特定行为。声明类的语法如下:
UCLASS([specifier, specifier, ...], [meta(key=value, key=value, ...)])
class ClassName : ParentName
{
GENERATED_UCLASS_BODY()
}
声明包含一个标准的C++类声明。在标准声明之上,通过UCLASS宏传递类描述符,例如类说明符和元数据。这些用于创建被声明的类的UClass,可以将其视为引擎对该类的专门表示。还必须在类主体的开头放置GENERATED_UCLASS_BODY()宏。
此宏是用于向引擎类添加一些元数据功能,例如RTTI、反射等等... 这个宏不是由C++ STD库提供的标准宏,而是由Epic Games编写的,以适应虚幻引擎的需求。
有关虚幻引擎Gameplay Classes及其使用的宏的更多信息,请参见这里。
下面是虚幻引擎为简单游戏类生成的一些宏代码示例:
#include "ObjectBase.h"
#ifdef MYPROJECTCODE_MyProjectCodeGameMode_generated_h
#error "MyProjectCodeGameMode.generated.h already included, missing '#pragma once' in MyProjectCodeGameMode.h"
#endif
#define MYPROJECTCODE_MyProjectCodeGameMode_generated_h
#define AMyProjectCodeGameMode_EVENTPARMS
#define AMyProjectCodeGameMode_RPC_WRAPPERS
#define AMyProjectCodeGameMode_RPC_WRAPPERS_NO_PURE_DECLS \
static inline void StaticChecks_Implementation_Validate() \
{ \
}
#define AMyProjectCodeGameMode_CALLBACK_WRAPPERS
#define AMyProjectCodeGameMode_INCLASS_NO_PURE_DECLS \
private: \
static void StaticRegisterNativesAMyProjectCodeGameMode(); \
friend MYPROJECTCODE_API class UClass* Z_Construct_UClass_AMyProjectCodeGameMode(); \
public: \
DECLARE_CLASS(AMyProjectCodeGameMode, AGameMode, COMPILED_IN_FLAGS(0 | CLASS_Transient | CLASS_Config), 0, MyProjectCode, MYPROJECTCODE_API) \
DECLARE_SERIALIZER(AMyProjectCodeGameMode) \
enum {IsIntrinsic=COMPILED_IN_INTRINSIC}; \
UObject* _getUObject() const { return const_cast<AMyProjectCodeGameMode*>(this); }
#define AMyProjectCodeGameMode_INCLASS \
private: \
static void StaticRegisterNativesAMyProjectCodeGameMode(); \
friend MYPROJECTCODE_API class UClass* Z_Construct_UClass_AMyProjectCodeGameMode(); \
public: \
DECLARE_CLASS(AMyProjectCodeGameMode, AGameMode, COMPILED_IN_FLAGS(0 | CLASS_Transient | CLASS_Config), 0, MyProjectCode, MYPROJECTCODE_API) \
DECLARE_SERIALIZER(AMyProjectCodeGameMode) \
enum {IsIntrinsic=COMPILED_IN_INTRINSIC}; \
UObject* _getUObject() const { return const_cast<AMyProjectCodeGameMode*>(this); }
#define AMyProjectCodeGameMode_STANDARD_CONSTRUCTORS \
\
MYPROJECTCODE_API AMyProjectCodeGameMode(const FObjectInitializer& ObjectInitializer); \
DEFINE_DEFAULT_OBJECT_INITIALIZER_CONSTRUCTOR_CALL(AMyProjectCodeGameMode) \
private: \
\
MYPROJECTCODE_API AMyProjectCodeGameMode(const AMyProjectCodeGameMode& InCopy); \
public:
#define AMyProjectCodeGameMode_ENHANCED_CONSTRUCTORS \
private: \
\
MYPROJECTCODE_API AMyProjectCodeGameMode(const AMyProjectCodeGameMode& InCopy); \
public: \
DEFINE_DEFAULT_OBJECT_INITIALIZER_CONSTRUCTOR_CALL(AMyProjectCodeGameMode)
#undef UCLASS_CURRENT_FILE_NAME
#define UCLASS_CURRENT_FILE_NAME AMyProjectCodeGameMode
#undef UCLASS
#undef UINTERFACE
#if UE_BUILD_DOCS
#define UCLASS(...)
#else
#define UCLASS(...) \
AMyProjectCodeGameMode_EVENTPARMS
#endif
#undef GENERATED_UCLASS_BODY
#undef GENERATED_BODY
#undef GENERATED_IINTERFACE_BODY
#define GENERATED_UCLASS_BODY() \
PRAGMA_DISABLE_DEPRECATION_WARNINGS \
public: \
AMyProjectCodeGameMode_RPC_WRAPPERS \
AMyProjectCodeGameMode_CALLBACK_WRAPPERS \
AMyProjectCodeGameMode_INCLASS \
AMyProjectCodeGameMode_STANDARD_CONSTRUCTORS \
public: \
PRAGMA_POP
#define GENERATED_BODY() \
PRAGMA_DISABLE_DEPRECATION_WARNINGS \
public: \
AMyProjectCodeGameMode_RPC_WRAPPERS_NO_PURE_DECLS \
AMyProjectCodeGameMode_CALLBACK_WRAPPERS \
AMyProjectCodeGameMode_INCLASS_NO_PURE_DECLS \
AMyProjectCodeGameMode_ENHANCED_CONSTRUCTORS \
private: \
PRAGMA_POP
它的类别。
#pragma once
#include "GameFramework/GameMode.h"
#include "MyProjectCodeGameMode.generated.h"
UCLASS(minimalapi)
class AMyProjectCodeGameMode : public AGameMode
{
GENERATED_BODY()
public:
AMyProjectCodeGameMode(const FObjectInitializer& ObjectInitializer);
};
正如您所看到的,此宏添加了额外的字段变量和方法来提供接口以获取虚幻引擎元数据信息。因此,引擎可以使用此信息来构建关卡、生成玩家等。
虚幻引擎定义的宏不能被重写,除非您想设计自己的引擎框架以添加一些新功能(使用新的宏定义),或修改当前的虚幻引擎以适应您的特定需求(这是非常艰苦的工作)。
#define
定义? - Vincent Savard#ifndef
标志。 - Jarod42