From eba62bce3ebf7ad3cdef61e0fd83320108042a73 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Sat, 13 Aug 2022 04:04:32 +0700 Subject: [PATCH] Change 'god' command to avoid connecting to `OnDamage()` if possible Previously this command connected to `OnDamage()` signal whenever created, even if it wasn't used. Which could've provoked adding unnecessary side-effects. This patch makes it listen to a signal only when god mod is enabled for someone. --- sources/Commands/ACommandGod.uc | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/sources/Commands/ACommandGod.uc b/sources/Commands/ACommandGod.uc index 2cd8932..3650958 100644 --- a/sources/Commands/ACommandGod.uc +++ b/sources/Commands/ACommandGod.uc @@ -29,7 +29,8 @@ struct GodStatus var bool unmovable; }; -var private array godhoodList; +var private bool connectedToSignal; +var private array godhoodList; var private ACommandGod_Announcer announcer; @@ -37,8 +38,9 @@ var private const int TDAMAGE, TMOMENTUM; protected function Finalizer() { - _.memory.Free(announcer); + connectedToSignal = false; _server.kf.health.OnDamage(self).Disconnect(); + _.memory.Free(announcer); super.Finalizer(); } @@ -62,7 +64,6 @@ protected function BuildData(CommandDataBuilder builder) @ "affected by the momentum trasnferred from damaging attacks.")); announcer = ACommandGod_Announcer( _.memory.Allocate(class'ACommandGod_Announcer')); - _server.kf.health.OnDamage(self).connect = ProtectDivines; } protected function ExecutedFor( @@ -157,6 +158,7 @@ private final function MakeGod( newGodStatus.target.NewRef(); godhoodList[godhoodList.length] = newGodStatus; } + UpdateHealthSignalConnection(); } private final function RemoveGod(EPlayer target) @@ -173,6 +175,7 @@ private final function RemoveGod(EPlayer target) announcer.AnnounceRemoveGod(godhoodList[i]); godhoodList[i].target.FreeSelf(); godhoodList.Remove(i, 1); + UpdateHealthSignalConnection(); return; } } @@ -196,6 +199,20 @@ private final function GodStatus BorrowGodStatus(EPlayer target) return emptyStatus; } +private final function UpdateHealthSignalConnection() +{ + if (connectedToSignal && godhoodList.length <= 0) + { + _server.kf.health.OnDamage(self).Disconnect(); + connectedToSignal = false; + } + if (!connectedToSignal && godhoodList.length > 0) + { + connectedToSignal = true; + _server.kf.health.OnDamage(self).connect = ProtectDivines; + } +} + defaultproperties { TDAMAGE = 0