TSubclassOf
#include "Templates/SubclassOf.h" Description
A UClass wrapper that enforces a type constraint at edit time and in C++. Use it in UPROPERTYs to restrict what class the user can assign in the editor.
Signature
template<typename T> class TSubclassOf Caveats & Gotchas
- • A null TSubclassOf is valid and common — always null-check before using it to spawn or CDO-access.
- • Assigning a class that doesn't inherit from T at runtime will fail silently in shipping builds but assert in debug.
- • Works with Blueprint classes — if you assign a Blueprint subclass of T, TSubclassOf will hold its UClass.
Example
Spawn an actor of a configurable class C++
UPROPERTY(EditDefaultsOnly, Category="Spawning")
TSubclassOf<AActor> EnemyClass;
// In code:
if (EnemyClass)
{
GetWorld()->SpawnActor<AActor>(EnemyClass, SpawnTransform);
} See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?