Page 1 of 1

ticks delays by name

Posted: Sat Dec 19, 2015 5:25 pm
by decompile
Hey,

Is there something like ticks_delays.delay_by_name where you can give the delay a name so you can cancel it when something happens eg. cvar change or he disconnects?

for example

Syntax: Select all

ticks_delay.delay(60, "player_hudcodereset_", self.userid, resetHUDCode)


Syntax: Select all

@Event('server_cvar')
def server_cvar(self):
ticks_delay.cancel_delay("resetHUDcode_, self.userid)


This example was just freestyled :p

I heard you can do a dict and cancel them kinda, can someone might do an example how it should look like if theres no other way

Posted: Sat Dec 19, 2015 7:38 pm
by iPlayer
I'm afraid there's no such example when it can't be avoided.
It's all about objects. You can store them, pass them back and forth, and it's beautiful.

Posted: Sat Dec 19, 2015 8:05 pm
by L'In20Cible
There is no naming implementation for the simple fact that names would be shared between plugins which can cause issues. That, and that we try to prone OOP as much as we can.

However, you could create a simple class inheriting of a dictionary:

Syntax: Select all

class NamedDelays(dict):
def delay(self, name, *args, **kwargs):
self[name] = tick_delays.delay(*args, **kwargs)

def cancel_delay(self, name):
tick_delays.cancel_delay(self[name])
del self[name]

named_delays = NamedDelays()

# ...later in code...
named_delays.delay('some_name', ...)

# ...then....
named_delays.cancel_delay('some_name')

Posted: Sun Dec 20, 2015 4:29 pm
by satoon101
If you're going to do that, you might want to make sure the name doesn't exist already. You can either cancel the original delay if it does, or raise a warning/exception. Also, you can use self[name].cancel() to cancel the delay instead of tick_delays.cancel_delay.

Posted: Sun Dec 20, 2015 6:44 pm
by decompile
I didnt tested it yet, but what will actually happen if you try to delete a "name" which doesnt exist? Just curious

Posted: Sun Dec 20, 2015 6:51 pm
by satoon101
If you try to delete a key in a dictionary, you'll get a KeyError. Actually, that brings up a good point. You should probably check the dictionary prior to canceling the delay and removing the key. Maybe something like:

Syntax: Select all

class NamedDelays(dict):
def delay(self, name, *args, **kwargs):
# Cancel the old delay, if it exists
self.cancel_delay(name)

# Create the delay
self[name] = tick_delays.delay(*args, **kwargs)

def cancel_delay(self, name):
# Does the delay not exist?
if name not in self:
return

# Cancel the delay
self[name].cancel()

# Remove the delay from the dictionary
del self[name]

named_delays = NamedDelays()

# ...later in code...
named_delays.delay('some_name', ...)

# ...then....
named_delays.cancel_delay('some_name')

Posted: Sun Dec 20, 2015 11:33 pm
by decompile
Thanks! :)