In this lesson, you will learn how to shift your mindset from building rigid object hierarchies to using Unreal Engine’s flexible "composition" model to build complex game objects.
The Old Way: Monolithic Inheritance
In many traditional programming patterns, you might be tempted to build a "god object" using deep inheritance. For example, if you wanted a car that could also fly and shoot lasers, you might try to create a FlyingShootingCar class that inherits from Car, which inherits from Vehicle, which inherits from Actor.
This is monolithic inheritance. It becomes a nightmare quickly: what if you want a Turret that isn’t a car, or a Plane that isn't a vehicle? You end up with broken code, duplicated logic, and a rigid class tree that is impossible to maintain as your game grows.
The Unreal Way: Composition
Unreal Engine uses Actor Composition. An AActor is essentially a blank container. Instead of baking functionality into the class itself, you fill that container with Components.
Think of an AActor like a Lego brick, and Components as the specific functions you snap onto it. If you want a character that can fly, you don't inherit from a FlyingCharacter class; you simply add a UProjectileMovementComponent or a UFloatingPawnMovement to your existing character.
In code, this looks like declaring a pointer to a component and initializing it in the constructor:
// Within your Actor's constructor
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualMesh"));
RootComponent = MyMesh;
MyMovement = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("FlightComponent"));
By adding these components, your Actor gains functionality (like movement or collision) without changing its underlying identity. You can mix and match these components on any Actor, regardless of what that Actor inherits from. This makes your code modular, reusable, and much easier to debug because each component is responsible for only one specific behavior.
Key Takeaway: Never build "everything" into one parent class; instead, design lightweight Actors and assemble their behavior using modular, reusable Components.