Components in Unreal Engine
Within an object represented by class with base DefaultSceneRoot, there is possibility to attach many other components.
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
RootComponentwhile still have freedom of their own movement within their range - Any component can become a
RootComponent
- Within header
.hfile,Components variablesbelong toprivatepart (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 ofCreateDefaultSubobject,UParticleSystemComponentfor smoke trails and other effects can be created and attached to therootas well.UCapsuleComponentexample:// .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;
- Type of root component? -
Setting the component as the Root component
RootComponent = CapsuleComp;or alternate waySetRootComponent(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... DefaultSceneRootcomponent 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. Transformof theRootComponentis representation of Actor location.RootComponentis scene component. Scene components have their owntransform.
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
USceneComponentfor 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
UStaticMeshComponent
- Adds visual representation (mesh)
- UShapeComponent
-
UCapsuleComponent
- Variable type:
UCapsuleComponent, e.g.UCapsuleComponent* CapsuleComp; - Handles collisions
- Example component:
Capsule Collision
- Variable type:
-
-
UCameraComponent
Often used as a child of
USpringArmComponent.
- Defines
-
UInputComponent
A componend designed for handling inputs. It's part of
Pawnand its derived classes. It's part of theoverridefunction 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); }
-