Components in Unreal Engine

What components to select to fit the needs? How they differ each other and for what purpose are they intended?

Working with Components in C++

  • Components supports attachment - there's possible to stack different components into each other. By that way, embedded components follow the movement of their parent component and the RootComponent while still have freedom of their own movement within their range
  • Any component can become a RootComponent
  • Within header .h file, Components variables belong to private part (if they need to be accessible just right within that class).
  • Constructing components

    Components can be created with the following code: CreateDefaultSubobject<Type>(TEXT("Name")). As a part of CreateDefaultSubobject, UParticleSystemComponent for smoke trails and other effects can be created and attached to the root as well.
    • UCapsuleComponent example:
      // .h file
      UPROPERTY(EditAnywhere)
      UCapsuleComponent* CapsuleComp;
      // .cpp file
      #include "Components/CapsuleComponent.h"
      AExampleActor::AExampleActor(){ // process right on compile
          CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
      }
      • Type of root component? - USceneComponent* Root;
    • Setting the component as the Root component

      RootComponent = CapsuleComp; or alternate way SetRootComponent(CapsuleComp);
    • Attaching the component into another component

      AExampleActor::AExampleActor(){ 
          // General StaticMesh Example
          SomeMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
          SomeComponent->SetupAttachment(ParentComponent);
          // particles example
          TrailParticles = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Smoke"));
          TrailParticles->SetupAttachment(RootComponent);
      }
  • Getting components

    Components can be get with the following code: GetDefaultSubobjectByName(TEXT("Name"));

List of selected components and their properties

RootComponent

  • Default RootComponent is DefaultSceneRoot, but it may be type any component - USceneComponent, UCapsuleComponent...
  • DefaultSceneRoot component has no visual representation (no mesh, it's invisible). It serves as a wrapper for other components.
  • It's part of any component derived from Actor.
  • Transform of the RootComponent is representation of Actor location.
  • RootComponent is scene component. Scene components have their own transform.

In the list below, I work with Inheritance Hierarchy, which basically means that each inherited class just extends a functionality of its parent class for own features.

  • UActorComponent
    • USceneComponent

      • Defines SetupAttachment() feature for attachment components into each other
      • Has a transform for location declaration
      • Has no visual representation (no mesh)
      • Usecase: spawnpoints (e.g. for a projectile - we need define location, rotation of where to spawn the bullet)
      • UPrimitiveComponent

        It extends USceneComponent for features that contain or generate some sort of geometry, generally to be rendered or used as collision data.

        • It has ability to generate Hit events
        • UMeshComponent
        • UShapeComponent
          • UCapsuleComponent

            • Variable type: UCapsuleComponent, e.g. UCapsuleComponent* CapsuleComp;
            • Handles collisions
            • Example component: Capsule Collision
      • UCameraComponent

        Often used as a child of USpringArmComponent.

    • UInputComponent

      A componend designed for handling inputs. It's part of Pawn and its derived classes. It's part of the override function and may be used as following:

      void ASamePawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
      {
          // preocess parents' classes functionality
          Super::SetupPlayerInputComponent(PlayerInputComponent);
          // Process own functionality
          PlayerInputComponent->BindAxis(TEXT("MoveName"), this, &ASamePawn::Move);
      }