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
.
Create a custom
SceneComponent
calledActorSpawnerComponent
. Make the following changes to the header:UFUNCTION() void Spawn(); UPROPERTY() TSubclassOf<AActor> ActorToSpawn;
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); } }
Verify your code against this snippet:
ActorSpawnerComponent.h #pragma once #include "Components/SceneComponent.h" #include "ActorSpawnerComponent.generated.h" ...