Skip to main content

04 - Components

Demonstrates the low-level component syntax (component UType "Name") and constructor wiring with SetupAttachment / AddLocalRotation.

Full source​

include "CoreMinimal.h"
include "Components/StaticMeshComponent.h"
include "Components/BoxComponent.h"

uclass AExampleComponents : AActor meta: api=EXAMPLES_API
{
component UBoxComponent "Trigger"
component UStaticMeshComponent "Mesh"

method public virtual void BeginPlay() override {
Super::BeginPlay();
}

ctor {
Trigger = CreateDefaultSubobject<UBoxComponent>(TEXT("Trigger"));
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));

RootComponent = Trigger;
Mesh->SetupAttachment(Trigger);
Mesh->AddLocalRotation(FRotator(0, 90.0f, 0));
}
}

Line by line​

  • component UBoxComponent "Trigger" - declares a subobject member. The name is quoted (required in low-level style). TPScript emits the member and its UPROPERTY.
  • component UStaticMeshComponent "Mesh" - a second subobject.
  • ctor { ... } - the constructor. This is where you create the subobjects (they're declared but not yet constructed):
    • CreateDefaultSubobject<UBoxComponent>(TEXT("Trigger")) assigns to Trigger.
    • CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh")) assigns to Mesh.
  • RootComponent = Trigger; - the box is the root.
  • Mesh->SetupAttachment(Trigger); - the mesh is a child of the box.
  • Mesh->AddLocalRotation(FRotator(0, 90.0f, 0)); - rotates the mesh locally (e.g. to align an imported pivot).

Generated header​

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UBoxComponent* Trigger;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
UStaticMeshComponent* Mesh;

High-level equivalent​

The same box+mesh in high-level style:

class my_prop extends actor meta: api=MYGAME_API
{
component trigger: box
component mesh: static_mesh
}

High-level component name: type auto-creates the subobject in the constructor (no manual CreateDefaultSubobject needed), but you lose the explicit attachment/rotation control.

When to use which​

  • High-level component x: type - fastest, when you just need the subobject present.
  • Low-level component UType "Name" + ctor - when you need exact construction, attachment parent, transforms, or multiple components.

Next: 05 - Low-Level Door.