Appearance
Components & Composition
While your GameObject class defines the custom logic (the "brain"), Components provide the data and capabilities (the "body").
By attaching specific components, you determine how the engine treats your object. For example:
Rendering: Pairing a MeshComponent with a TransformComponent automatically tells the engine this object is visible.
Interaction: An InputComponent enables the object to listen for and react to player input events.
Adding Components
You can attach components internally (within the class itself) or externally (from other objects).
Inside the Class (Internal)
cpp
AddComponent(MyComponent{});WARNING
If you want a component's data to be saved to and loaded from a scene file or be visible in the editor, you must add it in the Constructor.
Why? The engine loads scene data and creates or overrides respective components after the Constructor runs but before BeginPlay. If you add a component in BeginPlay, you are creating a fresh, empty component effectively overwriting or ignoring any data the engine tried to load from the file.
From Outside (External)
You can also add components to other objects dynamically. For example, a parent object adding a component to a child:
cpp
childObject -> AddComponent(MyComponent{});Accessing Components
At any point during the object's lifecycle, you can check if a component exists and retrieve it to modify its data.
cpp
// Check if the component exists first to avoid crashes
if (childObject->HasComponent<MyComponent>())
{
// Retrieve and modify
auto& comp = childObject->GetComponent<MyComponent>();
// comp.data = ...
}