How to do it...

  1. Create a custom SceneComponent called ActorSpawnerComponent:

  1. Make the following changes to the header:
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "ActorSpawnerComponent.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CHAPTER_04_API UActorSpawnerComponent : public USceneComponent
{
GENERATED_BODY()

public:
// Sets default values for this component's properties
UActorSpawnerComponent();

// Will spawn actor when called
UFUNCTION(BlueprintCallable, Category=Cookbook)
void Spawn();

UPROPERTY(EditAnywhere)
TSubclassOf<AActor> ActorToSpawn;

protected:
// Called when the game starts
virtual void BeginPlay() override;

public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;


};
  1. 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);
}
}
  1. Compile and open your project. Drag an empty Actor into the scene and add your ActorSpawnerComponent to it. Select your new Component in the Details panel and assign a value to ActorToSpawn.

Now, whenever Spawn() is called on an instance of your component, it will instantiate a copy of the Actor class that's specified in ActorToSpawn.