Skip to content

Tick Repeat/Delay updates #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Dec 4, 2016
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
satoon101 committed Sep 18, 2016
commit f4061002ab6088f3b01f98af5ac99526bf73cc4b
35 changes: 30 additions & 5 deletions addons/source-python/packages/source-python/listeners/tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Listeners
from listeners import listeners_logger
from listeners import on_tick_listener_manager
from listeners import OnLevelEnd


# =============================================================================
Expand Down Expand Up @@ -108,12 +109,16 @@ def add(self, delay):
class Delay(WeakAutoUnload):
"""Execute a callback after a given delay."""

def __init__(self, delay, callback, *args, **kwargs):
def __init__(
self, delay, callback, cancel_on_map_end=False, *args, **kwargs
):
"""Initialize the delay.

:param float delay: The delay in seconds.
:param callback: A callable object that should be called after the
delay expired.
:param bool cancel_on_map_end: Whether or not to cancel the delay at
the end of the map.
:param args: Arguments that should be passed to the callback.
:param kwargs: Keyword arguments that should be passed to the
callback.
Expand All @@ -126,6 +131,7 @@ def __init__(self, delay, callback, *args, **kwargs):
self._start_time = time.time()
self.exec_time = self._start_time + delay
self.callback = callback
self.cancel_on_map_end = cancel_on_map_end
self.args = args
self.kwargs = kwargs
_delay_manager.add(self)
Expand Down Expand Up @@ -199,17 +205,20 @@ class RepeatStatus(IntEnum):
class Repeat(AutoUnload):
"""Class used to create and call repeats."""

def __init__(self, callback, *args, **kwargs):
def __init__(self, callback, cancel_on_map_end=False, *args, **kwargs):
"""Store all instance attributes.

:param callback: A callable object that should be called at the
end of each loop.
:param bool cancel_on_map_end: Whether or not to cancel the repeat at
the end of the map.
:param args: Arguments that should be passed to the callback.
:param kwargs: Keyword arguments that should be passed to the
callback.
"""
# Store the base attributes
self.callback = callback
self.cancel_on_map_end = cancel_on_map_end
self.args = args
self.kwargs = kwargs

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

# Start the delay
self._delay = Delay(self._interval, self._execute)
self._delay = Delay(
self._interval, self._execute, self.cancel_on_map_end
)

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

# Start the delay
self._delay = Delay(self._loop_time, self._execute)
self._delay = Delay(
self._loop_time, self._execute, self.cancel_on_map_end
)

def extend(self, adjustment):
"""Add to the number of loops to be made.
Expand Down Expand Up @@ -465,7 +478,9 @@ def _execute(self):
self.remaining_loops))

# Call the delay again
self._delay = Delay(self._interval, self._execute)
self._delay = Delay(
self._interval, self._execute, self.cancel_on_map_end
)

# Are no more loops to be made?
else:
Expand Down Expand Up @@ -572,3 +587,13 @@ def delay_time_elapsed(self):
def _unload_instance(self):
"""Stop the repeat with being unloaded."""
self.stop()


# =============================================================================
# >> HELPER FUNCTIONS
# =============================================================================
@OnLevelEnd
def _cancel_delays_on_level_end():
for delay in _delay_manager:
if delay.cancel_on_map_end:
delay.cancel()