>>.... CPP Source....
//-------------------------------------------------------------------------------------------------------------------
// This is a simple timer example. This asset will simply spawn a number of "Orbs" around the origin of the game -
// world. Once the selected number of assets is completed it will unbind the timer handle and wait to be removed by -
// the end play event. You can freely use this example in your unreal engine projects! Just let me know if you -
// have any questioms. -
// -
// Nicholas Mallonee -
// Unreal Engine 4.7.6 -> 4.10.1 -
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
// Libraries and Includes -
//-------------------------------------------------------------------------------------------------------------------
#include "AI_Playground.h" // The project this asset is in.
#include "Engine.h"
#include "OrbSpawner.h" // This classes header file.
//-----------------------------------------------------------------------------------------------------------------
// Constructor -
//-----------------------------------------------------------------------------------------------------------------
AOrbSpawner::AOrbSpawner()
{
PrimaryActorTick.bCanEverTick = false;
}
//-----------------------------------------------------------------------------------------------------------------
// Engine Events -
//-----------------------------------------------------------------------------------------------------------------
void AOrbSpawner::BeginPlay()
{
Super::BeginPlay();
GetWorld()->GetTimerManager().SetTimer(spawnTimerHandler, this, &AOrbSpawner::handleSpawn, 1.25f, true);
spawnOrb();
}
void AOrbSpawner::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AOrbSpawner::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
}
//-----------------------------------------------------------------------------------------------------------------
// Spawning Methods and Timer Handles -
//-----------------------------------------------------------------------------------------------------------------
void AOrbSpawner::handleSpawn() // -- Handles the logic tree -- //
{
if (spawnerCount > 0) // If we can still spawn something
spawnOrb(); // Spawn the object otherwise clean up
else
cleanUpTimer();
}
void AOrbSpawner::spawnOrb()
{
// Make sure the world exists
UWorld* const world = GetWorld();
// if the world exists try to spawn the object
if (world)
{
// Generate an X to use for the x and y axis.
float x = FMath::RandRange(-270, 270);
// generate a vector to use.
FVector loc = FVector(x, x, 500.f);
// set the spawn params
FActorSpawnParameters spawnParams;
spawnParams.Owner = this;
spawnParams.Instigator = Instigator;
// Spawn the actor
GetWorld()->SpawnActor<AOrb>(ourOrb, loc, FRotator::ZeroRotator, spawnParams);
}
// reduce the count from the spawn counter
spawnerCount--;
}
void AOrbSpawner::cleanUpTimer()
{
// Clear up this timer on this object
GetWorld()->GetTimerManager().ClearTimer(spawnTimerHandler);
}
>>....Code Sample....Sample Class, Timer Creation, and Timer Clean-Up
>>.... Header File .....
//-------------------------------------------------------------------------------------------------------------------------------
// This is a simple timer example. This asset will simply spawn a number of "Orbs" around the origin of the game -
// world. Once the selected number of assets is completed it will unbind the timer handle and wait to be removed by -
// the end play event. You can freely use this example in your unreal engine projects! Just let me know if you -
// have any questioms. -
// -
// Nicholas Mallonee -
// Unreal Engine 4.7.6 -> 4.10.1 -
//--------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
// Pragmas and defines -
//-------------------------------------------------------------------------------------------------------------------
#pragma once
//-------------------------------------------------------------------------------------------------------------------
// Libraries and Includes -
//-------------------------------------------------------------------------------------------------------------------
#include "GameFramework/Actor.h" // Inheirt from the actor class
#include "Resources/StructExamples/Orbs/Orb.h" // This is the path to the object we are going to spawn
#include "OrbSpawner.generated.h" // This classes header file. Generated by the engine
//-------------------------------------------------------------------------------------------------------------------
// Class Information -
//-------------------------------------------------------------------------------------------------------------------
UCLASS()
class AI_PLAYGROUND_API AOrbSpawner : public AActor
{
GENERATED_BODY()
// -- Public information -- Constructor and engine events -- //
public:
AOrbSpawner(); // Class Constructor
virtual void BeginPlay() override; // Begin Play Event
virtual void Tick( float DeltaSeconds ) override; // Frame Tick Event
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; // End Play Event
// -- Private Information -- Orb Spawning -- //
private:
UFUNCTION()
void handleSpawn(); // Handles the Spawning logic tree
UFUNCTION()
void cleanUpTimer(); // Cleans up the timer and unbinds it
UFUNCTION()
void spawnOrb(); // Spawns the orb into the game world
// -- Private Information -- Timer Handling Variables -- //
private:
UPROPERTY(EditDefaultsOnly, Category = "Run Amount")
int32 spawnerCount = 3; // Holds the number of actors to spawn
// -- Private Information -- Orb to Spawn -- //
private:
UPROPERTY(EditDefaultsOnly, Category = "Basic Orb")
TSubclassOf<AOrb> ourOrb; // The thing we are going to spawn
// -- Private Information -- Timer Handle -- //
private:
FTimerHandle spawnTimerHandler; // The timer handle, use this for 4.7.2 on
};
Nicholas Mallonee