Attaching components to create a hierarchy

When creating custom Actors from components, it is important to consider the concept of Attaching. Attaching components together creates a relationship where transformations applied to the parent component will also affect the components that are attached to it.

How to do it...

  1. Create a new class based on Actor using the editor, and call it HierarchyActor.
  2. Add the following properties to your new class:
    UPROPERTY()
    USceneComponent* Root;
    UPROPERTY()
    USceneComponent* ChildSceneComponent;
    UPROPERTY()
    UStaticMeshComponent* BoxOne;
    UPROPERTY()
    UStaticMeshComponent* BoxTwo;
  3. Add the following code to the class constructor:
    Root = CreateDefaultSubobject<USceneComponent>("Root"); ChildSceneComponent = CreateDefaultSubobject<USceneComponent>("ChildSceneComponent"); ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.