Files
sebastian lewicki 695acb1980 Upgrading the CQTest plugin to an Engine module
Introducing the CQTestEnhancedInput plugin as this requires use of the EnhancedInput Engine Plugin (Engine modules cannot make use of the Engine Plugin)
#jira UE-217806
#rb Devin.Doucette, Jerome.Delattre, rob.huyett, sean.sweeney

[CL 36039088 by sebastian lewicki in ue5-main branch]
2024-09-05 10:24:20 -04:00

110 lines
3.6 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "UObject/GCObject.h"
/**
* Helper object for spawning Actors and other object types in the world.
*
* @see FActorTestSpawner, FMapTestSpawner
*/
struct CQTEST_API FSpawnHelper
{
/** Destruct the Spawn Helper. */
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 <typename ActorType>
ActorType& SpawnActor(const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr)
{
return SpawnActorAtInWorld<ActorType>(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 <typename ActorType>
ActorType& SpawnActorAt(FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr)
{
return SpawnActorAtInWorld<ActorType>(GetWorld(), Location, Rotation, SpawnParameters, Class);
}
/**
* Create a new Object.
*
* @return reference to the created object
*/
template <typename ObjectType>
ObjectType& SpawnObject()
{
static_assert(!TIsDerivedFrom<ObjectType, AActor>::IsDerived, "Use SpawnActor to spawn AActors.");
static_assert(TIsDerivedFrom<ObjectType, UObject>::IsDerived, "Objects must derive from UObject.");
ObjectType* const Object = NewObject<ObjectType>();
check(Object != nullptr);
SpawnedObjects.Add(Object);
return *Object;
}
/** Returns a reference to the current world. */
UWorld& GetWorld();
protected:
/**
* Creates a new world.
*
* @returns a newly created world.
*/
virtual UWorld* CreateWorld() = 0;
TArray<TWeakObjectPtr<AActor>> SpawnedActors{};
TArray<TWeakObjectPtr<UObject>> 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 <typename ActorType>
ActorType& SpawnActorAtInWorld(UWorld& World, const FVector& Location, const FRotator& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters(), UClass* Class = nullptr)
{
static_assert(TIsDerivedFrom<ActorType, AActor>::IsDerived, "Provided type does not derive from AActor");
ActorType* const Actor = Class ? World.SpawnActor<ActorType>(Class, Location, Rotation, SpawnParameters) : World.SpawnActor<ActorType>(Location, Rotation, SpawnParameters);
check(Actor != nullptr);
SpawnedActors.Add(Actor);
return *Actor;
}
};