Objects in Unreal Engine
- The base class for objects in Unreal is
UObject. TheUCLASSmacro can be used to tag classes derived fromUObjectso that the UObject handling system is aware of them. - The UCLASS macro gives the
UObjecta reference to aUCLASSthat describes its Unreal-based type. EachUCLASSmaintains 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 usingGetClass(). - 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
newoperator. 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#includedirective in the file.MYPROJECT_APIis necessary if MyProject wishes to expose the UMyObject class to other modules.GENERATED_BODY()... is required for allUCLASSandUSTRUCT.- Unreal Header Tool supports a minimal set of C++. When wrapping parts of a UCLASS with custom
#ifdefsmacros, UHT will ignore macros that do not containWITH_EDITORorWITHEDITORONLY_DATAmacros. - Updating Objects: Actors and ActorComponents have their Tick functions called automatically when registered to do so, however,
UObjectsdo not possess any built-in update ability ... can be added by inheriting from theFTickableGameObjectclass. - Destroying Objects: handled automatically by the garbage collection system when an Object is no longer referenced. This means that no
UPROPERTYpointers, 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 withObj->MarkAsGarbage()... Ifgc.PendingKillEnabled=truethen objects marked asPendingKillwill 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 withIsValid()calls if you're not clearing them manually. - Smart pointers are not intended to be used with UObjects.