From 27529eb3a6b58d11ae58521c26a814feefba95d1 Mon Sep 17 00:00:00 2001 From: Marco Cawthorne Date: Sat, 27 Aug 2022 11:21:11 -0700 Subject: [PATCH] NSTimer: Add SetupTimer(entity, void(), float, bool) and RunTimer() methods. --- src/shared/NSTimer.h | 7 +++++++ src/shared/NSTimer.qc | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/shared/NSTimer.h b/src/shared/NSTimer.h index 29a0de53..21dfbc93 100644 --- a/src/shared/NSTimer.h +++ b/src/shared/NSTimer.h @@ -26,6 +26,13 @@ NSTimer:NSEntity virtual void(void) _TimerThink; virtual void(void) _TempTimerThink; + + /* creates and sets up a new timer, starts immediately */ static NSTimer(entity, void(), float, bool) ScheduleTimer; + /* self garbage collecting version of the above */ static NSTimer(entity, void(), float, bool) TemporaryTimer; + + /* when you want to set up a timer ahead of time, but only run it manually */ + virtual void(void) RunTimer; + static NSTimer(entity, void(), float, bool) SetupTimer; }; \ No newline at end of file diff --git a/src/shared/NSTimer.qc b/src/shared/NSTimer.qc index 81381d91..e4478e2f 100644 --- a/src/shared/NSTimer.qc +++ b/src/shared/NSTimer.qc @@ -99,4 +99,30 @@ NSTimer::TemporaryTimer(entity receiver, void() call, float interval, bool repea ret.think = NSTimer::_TempTimerThink; ret.nextthink = time + interval; return ret; +} + +void +NSTimer::RunTimer(void) +{ + think = NSTimer::_TimerThink; + nextthink = time + m_flTime; +} + +NSTimer +NSTimer::SetupTimer(entity receiver, void() call, float interval, bool repeats) +{ + NSTimer ret; + + if (this) + ret = this; + else { + ret = spawn(NSTimer); + this = ret; + } + + ret.m_eReceiver = receiver; + ret.m_flTime = interval; + ret.m_vFunc = call; + ret.m_bRepeats = repeats; + return ret; } \ No newline at end of file