// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "Engine/World.h" #include "GameFramework/Actor.h" #include "UObject/GCObject.h" #define UE_API CQTEST_API /** * Helper object for spawning Actors and other object types in the world. * * @see FActorTestSpawner, FMapTestSpawner */ struct FSpawnHelper { /** Destruct the Spawn Helper. */ UE_API virtual ~FSpawnHelper(); /** * Spawn an Actor in the world. * * @param SpawnParameters - Struct of optional parameters used to assist with spawning. * @param Class - Class of the object to be spawned. * * @return reference to the spawned object * * @note Method guarantees that the Actor returned is valid, will assert otherwise. */ template ActorType& SpawnActor(const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr) { return SpawnActorAtInWorld(GetWorld(), FVector::ZeroVector, FRotator::ZeroRotator, SpawnParameters, Class); } /** * Spawn an Actor in the world. * * @param Location - Location to spawn the Actor in the world. * @param Rotation - Rotation to spawn the Actor in the world. * @param SpawnParameters - Struct of optional parameters used to assist with spawning. * @param Class - Class of the object to be spawned. * * @return reference to the spawned object * * @note Method guarantees that the Actor returned is valid, will assert otherwise. */ template ActorType& SpawnActorAt(FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr) { return SpawnActorAtInWorld(GetWorld(), Location, Rotation, SpawnParameters, Class); } /** * Create a new Object. * * @return reference to the created object */ template ObjectType& SpawnObject() { static_assert(!TIsDerivedFrom::IsDerived, "Use SpawnActor to spawn AActors."); static_assert(TIsDerivedFrom::IsDerived, "Objects must derive from UObject."); ObjectType* const Object = NewObject(); check(Object != nullptr); SpawnedObjects.Add(Object); return *Object; } /** Returns a reference to the current world. */ UE_API UWorld& GetWorld(); protected: /** * Creates a new world. * * @returns a newly created world. */ virtual UWorld* CreateWorld() = 0; TArray> SpawnedActors{}; TArray> SpawnedObjects{}; UWorld* GameWorld{ nullptr }; private: /** * Spawn an Actor in the world. * * @param World - World to spawn the Actor. * @param Location - Location to spawn the Actor in the world. * @param Rotation - Rotation to spawn the Actor in the world. * @param SpawnParameters - Struct of optional parameters used to assist with spawning. * @param Class - Class of the object to be spawned. * * @return reference to the spawned object * * @note Method guarantees that the Actor returned is valid, will assert otherwise. */ template ActorType& SpawnActorAtInWorld(UWorld& World, const FVector& Location, const FRotator& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr) { static_assert(TIsDerivedFrom::IsDerived, "Provided type does not derive from AActor"); ActorType* const Actor = Class ? World.SpawnActor(Class, Location, Rotation, SpawnParameters) : World.SpawnActor(Location, Rotation, SpawnParameters); check(Actor != nullptr); SpawnedActors.Add(Actor); return *Actor; } }; #undef UE_API