From dd0bdc8904f88aded2b824ab77c48e541c5b8b0d Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Wed, 8 Apr 2020 01:46:57 +0700 Subject: [PATCH] Add events for a `Singleton` Add `OnCreated()` and `OnDestroyed` event functions to provide a more simple and safe way to handle these events. --- sources/Singleton.uc | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/sources/Singleton.uc b/sources/Singleton.uc index 5271cbc..2bb945d 100644 --- a/sources/Singleton.uc +++ b/sources/Singleton.uc @@ -20,10 +20,10 @@ * You should have received a copy of the GNU General Public License * along with Acedia. If not, see . */ -class Singleton extends Actor +class Singleton extends AcediaActor abstract; -// Default value of this variable will store one and only existing version\ +// Default value of this variable will store one and only existing version // of actor of this class. var private Singleton activeInstance; @@ -44,8 +44,14 @@ public final static function bool IsSingletonCreationBlocked() return default.blockSpawning; } +protected function OnCreated(){} +protected function OnDestroyed(){} + // Make sure only one instance of 'Singleton' exists at any point in time. -// If you overload this function in any child class - +// Instead of overloading this function we suggest you overload a special +// event function `OnCreated()` that is called whenever a valid `Singleton` +// instance is spawned. +// If you absolutely must overload this function in any child class - // first call this version of the method and then check if // you are about to be deleted 'bDeleteMe == true': // ____________________________________________________________________________ @@ -57,6 +63,7 @@ public final static function bool IsSingletonCreationBlocked() // |___________________________________________________________________________ event PreBeginPlay() { + super.PreBeginPlay(); if (default.blockSpawning || GetInstance() != none) { Destroy(); @@ -64,6 +71,22 @@ event PreBeginPlay() else { default.activeInstance = self; + OnCreated(); + } +} + +// Make sure only one instance of 'Singleton' exists at any point in time. +// Instead of overloading this function we suggest you overload a special +// event function `OnDestroyed()` that is called whenever a valid `Singleton` +// instance is destroyed. +// If you absolutely must overload this function in any child class - +// first call this version of the method. +event Destroyed() +{ + super.Destroyed(); + if (self == GetInstance()) + { + OnDestroyed(); } }