Creating a custom Scene Component

Scene Components are a subclass of Actor Components that have a transform, that is, a relative location, rotation, and scale. Just like Actor Components, Scene Components aren't rendered themselves, but can use their transform for various things, such as spawning other objects at a fixed offset from an Actor.

How to do it...

  1. Create a custom SceneComponent called ActorSpawnerComponent. Make the following changes to the header:
    UFUNCTION()
    void Spawn();
    UPROPERTY()
    TSubclassOf<AActor> ActorToSpawn;
  2. Add the following function implementation to the cpp file:
    void UActorSpawnerComponent::Spawn() { UWorld* TheWorld = GetWorld(); if (TheWorld != nullptr) { FTransform ComponentTransform(this->GetComponentTransform()); TheWorld->SpawnActor(ActorToSpawn,&ComponentTransform); ...

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.