UE4框架之游戏性类
官网链接:https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/Classes/index.html
虚幻引擎中每个游戏性类都有一个头文件(.h)和一个类源文件。类头文件包含类和类成员(变量和函数)的声明,而在类源文件中通过实现属于类的函数来定义类的功能。
虚幻引擎的游戏性类有一个标准化的命名方案,通过首字母或前缀即可立即明了声明的类为什么类。游戏性类的前缀有:
A:表示继承于可生成游戏性对象的基础类(AActor),它们是Actor,可以直接生成到世界场景中;
U:表示继承于所有游戏性对象的基础类(UObject).必须从属于Actor.总体而言,他们与组件类似。Actor是可以挂载组件
F:表示是原始的c++类,并没有继承于虚幻引擎任何游戏性矿建类;
游戏戏类的声明格式:
游戏性类声明定义类的名称,继承的类,及其继承的函数和变量。类声明还通过声明类修饰符和元数据来定义该类的其他引擎行为和其在编辑器中的表现行为;
UCLASS([specifier, specifier, ...], [meta(key=value, key=value, ...)]) class ClassName : public ParentName { GENERATED_BODY()或GENERATED_UCLASS_BODY()
}
在类声明之上,类描述符(类修饰符和元数据)将被传递到UCLASS()宏,它们可以被看作虚幻引擎对类的专有表达, 它们决定了该类的其他引擎行为和其在编辑器中的表现行为(e,g 当把类修饰符abStract传递给UCLASS()宏时,表明将该类是声明为一个抽象基类,这样就会组织用户在虚幻编辑器中向这个世界中添加这个类的对象,或者在游戏过程中创建这个类的实例)。除此之外,
GENERATED_BODY()和GENERATED_UCLASS_BODY()必须放在类体的最前方。
GENERATED_BODY()和GENERATED_UCLASS_BODY()的区别:
论坛链接:https://forums.unrealengine.com/showthread.php?52236-4-6-constructor-changes-am-confused
参考链接:http://blog.csdn.net/a359877454/article/details/52511893
GENERATED_BODY()宏允许类在编译时没有构造函数的定义,如果你需要一个构造函数,你需要将宏GENERATED_BODY()改变为GENERATED_UCLASS_BODY(),如果你做了这一步你需要提供一个构造函数,否则你的项目将不能编译。
所以如果使用的是宏GENERATED_UCLASS_BODY(),你的代码看起来应该是像这样的:
AMyActor.h UCLASS() class TESTCONSTRUCTOR_API AMyActor : public AActor { GENERATED_UCLASS_BODY() }; /*********************************/ // MyActor.cpp #include "MyActor.h" AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { // Class constructor/initialization code如果你是用的是宏GENERATED_UCLASS_BODY(),这就是你需要提供的构造函数定义
//如果你使用的是宏GENERATED_BODY(),则不需要在源文件是提供任何构造函数;
}
其实如果使用的是宏GENERATED_BODY(),也可以使用构造函数,但是你需要重新声明和定义一个构造函数
AMyActor.h UCLASS() class TESTCONSTRUCTOR_API AMyActor : public AActor { GENERATED_BODY()
public:
AMyActor() //如果没有添加这里红色代码,直接在.cpp文件中定义AMyActor(),将不能通过编译,编译错误大概是没有找到重载的成员函数
}; /*********************************/ // MyActor.cpp
#include "MyActor.h"
AMyActor::AMyActor()
{
}
红色部分为需要添加的代码
除此之外,GENERATED_BODY()标识的类成员是private的,GENETATED_CLASS_BODY()标识的类成员是public的
类修饰符:这些修饰符在以下头文件中声明的
\Engine\Source\Runtime\CoreUObject\Public\UObject\ObjectMacros.h
// These are used for syntax highlighting and to allow autocomplete hints namespace UC { // valid keywords for the UCLASS macro--对于UCLASS()宏可以使用的关键字 enum { /// This keyword is used to set the actor group that the class is show in, in the editor.
//这个宏被用来设置这个类显示在编辑器中哪个Actor分组下,用法 classGroup = GroupName classGroup, /// Declares that instances of this class should always have an outer of the specified class. This is inherited by subclasses unless overridden. Within, /* =OuterClassName */ /// Exposes this class as a type that can be used for variables in blueprints
//表明这个类在蓝图中可以作为一种变量类型使用
BlueprintType, /// Prevents this class from being used for variables in blueprints NotBlueprintType, /// Exposes this class as an acceptable base class for creating blueprints. The default is NotBlueprintable, unless inherited otherwise. This is inherited by subclasses. Blueprintable, /// Specifies that this class is *NOT* an acceptable base class for creating blueprints. The default is NotBlueprintable, unless inherited otherwise. This is inherited by subclasses. NotBlueprintable, /// This keyword indicates that the class should be accessible outside of it's module, but does not need all methods exported. /// It exports only the autogenerated methods required for dynamic_cast<>, etc... to work. MinimalAPI, /// Prevents automatic generation of the constructor declaration. customConstructor, /// Class was declared directly in C++ and has no boilerplate generated by UnrealHeaderTool. /// DO NOT USE THIS FLAG ON NEW CLASSES. Intrinsic, /// No autogenerated code will be created for this class; the header is only provided to parse metadata from. /// DO NOT USE THIS FLAG ON NEW CLASSES. noexport, /// Allow users to create and place this class in the editor. This flag is inherited by subclasses. placeable, /// This class cannot be placed in the editor (it cancels out an inherited placeable flag). notplaceable, /// All instances of this class are considered "instanced". Instanced classes (components) are duplicated upon construction. This flag is inherited by subclasses. DefaultToInstanced, /// All properties and functions in this class are const and should be exported as const. This flag is inherited by subclasses. Const, /// Class is abstract and can't be instantiated directly. Abstract, /// This class is deprecated and objects of this class won't be saved when serializing. This flag is inherited by subclasses. deprecated, /// This class can't be saved; null it out at save time. This flag is inherited by subclasses. Transient, /// This class should be saved normally (it cancels out an inherited transient flag). nonTransient, /// Load object configuration at construction time. These flags are inherited by subclasses. /// Class containing config properties. Usage config=ConfigName or config=inherit (inherits config name from base class). config, /// Handle object configuration on a per-object basis, rather than per-class. perObjectConfig, /// Determine whether on serialize to configs a check should be done on the base/defaults ini's configdonotcheckdefaults, /// Save object config only to Default INIs, never to local INIs. defaultconfig, /// These affect the behavior of the property editor. /// Class can be constructed from editinline New button. editinlinenew, /// Class can't be constructed from editinline New button. noteditinlinenew, /// Class not shown in editor drop down for class selection. hidedropdown, /// Shows the specified categories in a property viewer. Usage: showCategories=CategoryName or showCategories=(category0, category1, ...) showCategories, /// Hides the specified categories in a property viewer. Usage: hideCategories=CategoryName or hideCategories=(category0, category1, ...) hideCategories, /// Indicates that this class is a wrapper class for a component with little intrinsic functionality (this causes things like hideCategories and showCategories to be ignored if the class is subclassed in a Blueprint) ComponentWrapperClass, /// Shows the specified function in a property viewer. Usage: showFunctions=FunctionName or showFunctions=(category0, category1, ...) showFunctions, /// Hides the specified function in a property viewer. Usage: hideFunctions=FunctionName or hideFunctions=(category0, category1, ...) hideFunctions, /// Specifies which categories should be automatically expanded in a property viewer. autoExpandCategories, /// Specifies which categories should be automatically collapsed in a property viewer. autoCollapseCategories, /// Clears the list of auto collapse categories. dontAutoCollapseCategories, /// Display properties in the editor without using categories. collapseCategories, /// Display properties in the editor using categories (default behaviour). dontCollapseCategories, /// All the properties of the class are hidden in the main display by default, and are only shown in the advanced details section. AdvancedClassDisplay, /// A root convert limits a sub-class to only be able to convert to child classes of the first root class going up the hierarchy. ConversionRoot, /// Marks this class as 'experimental' (a totally unsupported and undocumented prototype) Experimental, // Marks this class as an 'early access' preview (while not considered production-ready, it's a step beyond 'experimental' and is being provided as a preview of things to come) EarlyAccessPreview, }; }
下面介绍几种常用的类修饰符:
BlueprintType:
Exposes this class as a type that can be used for variables in Blueprints.
表明使用BlueprintType类修饰符修饰的类 可以在蓝图中作为一种变量类型来使用.
NotBlueprintType:
Prevents this class from being used for variables in blueprints
表明使用NotBlueprintType类修饰符修饰的类 不可以在蓝图中作为一种变量类型来使用.
Blueprintable:
Exposes this class as an acceptable base class for creating Blueprints. The default is NotBlueprintable, unless inherited otherwise. This is inherited by subclasses.

浙公网安备 33010602011771号