5454from listeners import OnEntityDeleted
5555from listeners import on_entity_deleted_listener_manager
5656from listeners .tick import Delay
57+ from listeners .tick import Repeat
58+ from listeners .tick import RepeatStatus
5759# Mathlib
5860from mathlib import NULL_VECTOR
5961# Memory
7577# Get a dictionary to store the delays
7678_entity_delays = defaultdict (set )
7779
80+ # Get a dictionary to store the repeats
81+ _entity_repeats = defaultdict (set )
82+
7883# Get a set to store the registered entity classes
7984_entity_classes = WeakSet ()
8085
@@ -92,11 +97,13 @@ def __init__(cls, classname, bases, attributes):
9297
9398 # Set whether or not this class is caching its instances by default
9499 try :
95- cls ._caching = signature (
96- cls .__init__
97- ).parameters ['caching' ].default
100+ cls ._caching = bool (
101+ signature (
102+ vars (cls )['__init__' ]
103+ ).parameters ['caching' ].default
104+ )
98105 except KeyError :
99- cls ._caching = True
106+ cls ._caching = bool ( vars ( cls ). get ( 'caching' , False ))
100107
101108 # Add the class to the registered classes
102109 _entity_classes .add (cls )
@@ -331,14 +338,16 @@ def find_or_create(cls, classname):
331338 return entity
332339
333340 @classmethod
334- def from_inthandle (cls , inthandle ):
341+ def from_inthandle (cls , inthandle , caching = None ):
335342 """Create an entity instance from an inthandle.
336343
337344 :param int inthandle:
338345 The inthandle.
346+ :param bool caching:
347+ Whether to lookup the cache for an existing instance or not.
339348 :rtype: Entity
340349 """
341- return cls (index_from_inthandle (inthandle ))
350+ return cls (index_from_inthandle (inthandle ), caching = caching )
342351
343352 @classmethod
344353 def _obj (cls , ptr ):
@@ -941,7 +950,7 @@ def _set_property(self, name, prop_type, value):
941950 def delay (
942951 self , delay , callback , args = (), kwargs = None ,
943952 cancel_on_level_end = False ):
944- """Execute a callback after the given delay .
953+ """Create the delay which will be stopped after removing the entity .
945954
946955 :param float delay:
947956 The delay in seconds.
@@ -984,6 +993,34 @@ def _callback(*args, **kwargs):
984993 # Return the delay instance...
985994 return delay
986995
996+ def repeat (
997+ self , callback , args = (), kwargs = None ,
998+ cancel_on_level_end = False ):
999+ """Create the repeat which will be stopped after removing the entity.
1000+ :param callback:
1001+ A callable object that should be called at the end of each loop.
1002+ :param tuple args:
1003+ Arguments that should be passed to the callback.
1004+ :param dict kwargs:
1005+ Keyword arguments that should be passed to the callback.
1006+ :param bool cancel_on_level_end:
1007+ Whether or not to cancel the delay at the end of the map.
1008+ :raise ValueError:
1009+ Raised if the given callback is not callable.
1010+ :return:
1011+ The repeat instance.
1012+ :rtype: Repeat
1013+ """
1014+
1015+ # Get the repeat instance...
1016+ repeat = Repeat (callback , args , kwargs , cancel_on_level_end )
1017+
1018+ # Add the repeat to the dictionary...
1019+ _entity_repeats [self .index ].add (repeat )
1020+
1021+ # Return the repeat instance...
1022+ return repeat
1023+
9871024 def get_output (self , name ):
9881025 """Return the output instance matching the given name.
9891026
@@ -1263,13 +1300,22 @@ def _on_entity_deleted(base_entity):
12631300 except ValueError :
12641301 return
12651302
1266- with suppress (KeyError ):
1267- # Loop through all delays...
1268- for delay in _entity_delays [index ]:
1303+ # Get the registered delays for this entity
1304+ delays = _entity_delays .pop (index , ())
12691305
1270- # Cancel the delay...
1271- with suppress (ValueError ):
1272- delay .cancel ()
1306+ # Loop through all delays...
1307+ for delay in delays :
1308+
1309+ # Cancel the delay...
1310+ with suppress (ValueError ):
1311+ delay .cancel ()
1312+
1313+ # Get the registered repeats for this entity
1314+ repeats = _entity_repeats .pop (index , ())
1315+
1316+ # Loop through all repeats...
1317+ for repeat in repeats :
12731318
1274- # Remove the entity from the dictionary...
1275- del _entity_delays [index ]
1319+ # Stop the repeat if running
1320+ if repeat .status is RepeatStatus .RUNNING :
1321+ repeat .stop ()
0 commit comments