From e3f3296c2d78ee6dbaad0dc6c54dfcf99bed756f Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Sat, 18 Jul 2020 02:30:55 +0700 Subject: [PATCH] Add ability to auto-create `Singleton` Add an optional parameter that allows to auto-spawn `Singleton` with `GetInstance()` call. --- sources/Core/Singleton.uc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sources/Core/Singleton.uc b/sources/Core/Singleton.uc index 2bb945d..c4d61c4 100644 --- a/sources/Core/Singleton.uc +++ b/sources/Core/Singleton.uc @@ -32,11 +32,18 @@ var private Singleton activeInstance; // Only a default value is ever used. var protected bool blockSpawning; -public final static function Singleton GetInstance() +public final static function Singleton GetInstance(optional bool spawnIfMissing) { - if (default.activeInstance != none && default.activeInstance.bPendingDelete) - return none; - return default.activeInstance; + local bool instanceExists; + instanceExists = default.activeInstance != none + && !default.activeInstance.bPendingDelete; + if (instanceExists) { + return default.activeInstance; + } + if (spawnIfMissing) { + return class'Acedia'.static.GetInstance().Spawn(default.class); + } + return none; } public final static function bool IsSingletonCreationBlocked()