Component

From UE4: Community Wiki
Jump to: navigation, search


This article is a preliminary version, it is subject to change.


Introduction[edit]

Components are a core part of any Component Based Development, they are Objects which define modular reusable behavior. In Unreal Engine 4 Actors are composed of components, every Actor has a base Scene Component which is referred to as the Root Component of that Actor and provides its world transform.

Component Types[edit]

Actor Component[edit]

UActorComponent is the base type for any Component which will extend behaviors of an Actor, including Scene Components.

Scene Component[edit]

USceneComponent has a transform allowing it to be physically present in the game world at a certain location. This also means that all components allowing attachment are either of USceneComponent type or inherit from it.

Primitive Components[edit]

These types of Components are subclasses of Scene Component and add some form of physical representation in the game world. Examples include UStaticMeshComponent, USkeletalMeshComponent, and UTextRenderComponent.

Physics Components[edit]

For more information on Physics Components, including Collision Components please see Physics. Examples include UCapsuleComponent.

Visualization Components[edit]

These types of Components are only visible in Editor Builds, they are not relevant for gameplay purposes.

Creating Components[edit]

We assume you are familiar with Creating Classes.

YourActor.h

UCLASS()
class AYourActor : public AActor
{
	GENERATED_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* CameraBoom;
};

Default Components[edit]

YourActor.cpp in the constructor of your Class.

AYourActor::AYourActor()
{
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
}

Runtime[edit]

Very similar to creating any other Object at runtime except you have to make sure you call Register Component.

	auto *InstanceMesh = NewObject <UInstancedStaticMeshComponent>(Master);
	InstanceMesh->CreationMethod = EComponentCreationMethod::Instance;
	InstanceMesh->SetMobility(EComponentMobility::Type::Stationary);
	InstanceMesh->AddInstance(FTransform(Rotation, Location, FVector(1.0f, 1.0f, 1.0f)));
	InstanceMesh->RegisterComponent();
	InstanceMesh->SetStaticMesh(Mesh);
UActorCompon ... 	AddComponent
(
    FName TemplateName,
    bool bManualAttachment,
    const FTransform & RelativeTransfor...,
    const UObject * ComponentTempla...
)
CreateComponentFromTemplate
(
    UActorComponent * Template,
    const FName InName
)

Editor[edit]

You may want a Component which is only used in the Editor, this also gives you the ability to have an editor tickable component if you require it to update. YourActor.h

#if WITH_EDITOR	
    /** Camera boom positioning the camera behind the character */
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Camera", meta=(AllowPrivateAccess = "true"))
    USpringArmComponent* CameraBoom;
    FORCEINLINE USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
#endif

In the Constructor of YourActor.CPP

#if WITH_EDITOR	
    // Create a camera boom (pulls in towards the player if there is a collision)
    CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
    CameraBoom->SetupAttachment(RootComponent);
    CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character	
    CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
#endif

New Components Types[edit]

YourComponent.h

UCLASS(HideCategories = ("Rendering", "Input", "HLOD", "Sockets", "Collision"), meta = (BlueprintSpawnableComponent))
class UYourComponent : public UActorComponent
{
	GENERATED_BODY()
}

Attaching Components[edit]

Most of the base functionality for working with Components is defined in the Actor Class as we work with Components a majority of the time from the Actor scope.

Detaching Components[edit]

Finding Components[edit]

AActor::FindComponentByClass()AActor::GetComponents()

Replicating Components[edit]

Physics Components[edit]

Ticking Components[edit]

In-Editor Ticking[edit]

This is just a matter of setting the correct variable that is defined in Actor Component.

	/** Should this component be ticked in the editor */
	uint8 bTickInEditor:1;


Further Reading[edit]