ticks delays by name

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

ticks delays by name

Postby decompile » Sat Dec 19, 2015 5:25 pm

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
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Sat Dec 19, 2015 7:38 pm

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.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sat Dec 19, 2015 8:05 pm

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')
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Dec 20, 2015 4:29 pm

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.
Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Sun Dec 20, 2015 6:44 pm

I didnt tested it yet, but what will actually happen if you try to delete a "name" which doesnt exist? Just curious
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Dec 20, 2015 6:51 pm

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')
Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Sun Dec 20, 2015 11:33 pm

Thanks! :)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 136 guests