中EN

Objects in Unreal Engine

  • The base class for objects in Unreal is UObject. The UCLASS macro can be used to tag classes derived from UObject so that the UObject handling system is aware of them.
  • 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. ... The UCLASS for an Object instance can be accessed at any time using GetClass().
  • A UObject class can include native-only properties that are not marked for reflection ... However, only functions and properties that are marked with specifier macros will get listed within their corresponding UCLASS.
  • UObjects do not support constructor arguments. ... the engine calls their default constructor. If there is no default constructor, your UObject will not compile.
  • UObject constructors should be lightweight and only used to set up default values and subobjects ... For Actors and Actor Components, initialization functionality should be put into the BeginPlay() method instead.
  • UObjects should only be constructed using NewObject at runtime, or CreateDefaultSubobject for constructors. ... UObjects should never use the new operator. All UObjects are memory managed by Unreal Engine and garbage collected.
  • Functionality: Garbage collection; Reference updating; Reflection; Serialization; Automatic updating of default property changes; Automatic property initialization; Automatic editor integration; Type information available at runtime; Network replication. Most of these benefits apply to UStructs ... UStructs are considered value types and are not garbage collected.
  • The Unreal Header Tool: a preprocessing step needs to be run on the header files for these types to collate the information it needs. This preprocessing step is performed by the UnrealHeaderTool, or UHT.
  • #include "MyObject.generated.h" is expected to be the last #include directive in the file. MYPROJECT_API is necessary if MyProject wishes to expose the UMyObject class to other modules. GENERATED_BODY() ... is required for all UCLASS and USTRUCT.
  • Unreal Header Tool supports a minimal set of C++. When wrapping parts of a UCLASS with custom #ifdefs macros, UHT will ignore macros that do not contain WITH_EDITOR or WITHEDITORONLY_DATA macros.
  • Updating Objects: Actors and ActorComponents have their Tick functions called automatically when registered to do so, however, UObjects do not possess any built-in update ability ... can be added by inheriting from the FTickableGameObject class.
  • Destroying Objects: handled automatically by the garbage collection system when an Object is no longer referenced. This means that no UPROPERTY pointers, engine containers, TStrongObjectPtr, or class instances should have any strong references to them. Weak Pointers have no impact on whether an Object is garbage collected or not.
  • Object->MarkPendingKill() has been replaced with Obj->MarkAsGarbage() ... If gc.PendingKillEnabled=true then objects marked as PendingKill will be automatically nulled and destroyed by Garbage Collector.
  • You can replace Strong pointers with weak pointers and dereference them once during a gameplay operation as garbage collection only runs between frames.
  • IsValid() is for checking if it's null or garbage ...
  • If PendingKill() is disabled, MarkGarbage() will flag to the owner of the object that it wants the object destroyed, but the object itself will not get garbage collected until all references to it are released.
  • In the case of Actors, even if the Actor had Destroy() called on them, and they were removed from the level, then it will not be Garbage Collected until all references to it are released.
  • Using MarkPendingKill() to force expensive objects to garbage collect will no longer work. Existing checks for nullptr should be replaced with IsValid() calls if you're not clearing them manually.
  • Smart pointers are not intended to be used with UObjects.

引用这一章的笔记

输入关键词开始搜索

滚轮或双指缩放 · 拖动平移 · 双击放大 / 还原