Skip to content

Commit f406100

Browse files
committed
Fix for issue #145.
Both Delay and Repeat now allow for the argument cancel_on_map_end to denote whether the object should be cancelled when the current map ends.
1 parent bdc5cad commit f406100

File tree

1 file changed

+30
-5
lines changed
  • addons/source-python/packages/source-python/listeners

1 file changed

+30
-5
lines changed

addons/source-python/packages/source-python/listeners/tick.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# Listeners
2626
from listeners import listeners_logger
2727
from listeners import on_tick_listener_manager
28+
from listeners import OnLevelEnd
2829

2930

3031
# =============================================================================
@@ -108,12 +109,16 @@ def add(self, delay):
108109
class Delay(WeakAutoUnload):
109110
"""Execute a callback after a given delay."""
110111

111-
def __init__(self, delay, callback, *args, **kwargs):
112+
def __init__(
113+
self, delay, callback, cancel_on_map_end=False, *args, **kwargs
114+
):
112115
"""Initialize the delay.
113116
114117
:param float delay: The delay in seconds.
115118
:param callback: A callable object that should be called after the
116119
delay expired.
120+
:param bool cancel_on_map_end: Whether or not to cancel the delay at
121+
the end of the map.
117122
:param args: Arguments that should be passed to the callback.
118123
:param kwargs: Keyword arguments that should be passed to the
119124
callback.
@@ -126,6 +131,7 @@ def __init__(self, delay, callback, *args, **kwargs):
126131
self._start_time = time.time()
127132
self.exec_time = self._start_time + delay
128133
self.callback = callback
134+
self.cancel_on_map_end = cancel_on_map_end
129135
self.args = args
130136
self.kwargs = kwargs
131137
_delay_manager.add(self)
@@ -199,17 +205,20 @@ class RepeatStatus(IntEnum):
199205
class Repeat(AutoUnload):
200206
"""Class used to create and call repeats."""
201207

202-
def __init__(self, callback, *args, **kwargs):
208+
def __init__(self, callback, cancel_on_map_end=False, *args, **kwargs):
203209
"""Store all instance attributes.
204210
205211
:param callback: A callable object that should be called at the
206212
end of each loop.
213+
:param bool cancel_on_map_end: Whether or not to cancel the repeat at
214+
the end of the map.
207215
:param args: Arguments that should be passed to the callback.
208216
:param kwargs: Keyword arguments that should be passed to the
209217
callback.
210218
"""
211219
# Store the base attributes
212220
self.callback = callback
221+
self.cancel_on_map_end = cancel_on_map_end
213222
self.args = args
214223
self.kwargs = kwargs
215224

@@ -268,7 +277,9 @@ def start(self, interval, limit, execute_on_start=False):
268277
self._adjusted = 0
269278

270279
# Start the delay
271-
self._delay = Delay(self._interval, self._execute)
280+
self._delay = Delay(
281+
self._interval, self._execute, self.cancel_on_map_end
282+
)
272283

273284
# Call the callback if set to execute on start
274285
if execute_on_start:
@@ -368,7 +379,9 @@ def resume(self):
368379
self._status = RepeatStatus.RUNNING
369380

370381
# Start the delay
371-
self._delay = Delay(self._loop_time, self._execute)
382+
self._delay = Delay(
383+
self._loop_time, self._execute, self.cancel_on_map_end
384+
)
372385

373386
def extend(self, adjustment):
374387
"""Add to the number of loops to be made.
@@ -465,7 +478,9 @@ def _execute(self):
465478
self.remaining_loops))
466479

467480
# Call the delay again
468-
self._delay = Delay(self._interval, self._execute)
481+
self._delay = Delay(
482+
self._interval, self._execute, self.cancel_on_map_end
483+
)
469484

470485
# Are no more loops to be made?
471486
else:
@@ -572,3 +587,13 @@ def delay_time_elapsed(self):
572587
def _unload_instance(self):
573588
"""Stop the repeat with being unloaded."""
574589
self.stop()
590+
591+
592+
# =============================================================================
593+
# >> HELPER FUNCTIONS
594+
# =============================================================================
595+
@OnLevelEnd
596+
def _cancel_delays_on_level_end():
597+
for delay in _delay_manager:
598+
if delay.cancel_on_map_end:
599+
delay.cancel()

0 commit comments

Comments
 (0)