Help with Delay

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

Help with Delay

Postby decompile » Mon May 23, 2016 1:28 am

Hey,

Im currently thinking about writing up a script which prints the time until mapchange


Syntax: Select all

mp_timelimit = 0
mapStartTime = 0
@OnLevelInit
def OnMapStart(mapName):
global mapStartTime
mapStartTime = time.time()
startGamethreads()

def startGamethreads():
value = float(mp_timelimit)
endTime = mapStartTime + 60 * value
timeleft = endTime - time.time()


named_delays.cancel_delay("EndMapAnnouncer")
named_delays.delay("EndMapAnnouncer", timeleft-120, announceEndMap, 120)
named_delays.delay("EndMapAnnouncer", timeleft-60, announceEndMap, 60)
named_delays.delay("EndMapAnnouncer", timeleft-30, announceEndMap, 30)
named_delays.delay("EndMapAnnouncer", timeleft-10, announceEndMap, 10)

def announceEndMap(timeleft):
#do anything

@Event("server_cvar")
def server_cvar(GameEvent):
global mp_timelimit
if GameEvent["cvarname"] == "mp_timelimit":
mp_timelimit = int(GameEvent["cvarvalue"])
startGamethreads()


It seems like the "named delays" from this thread viewtopic.php?p=6622#p6622 are not realy canceling the Delay after the mp_timelimit has been changed, so How do I properly use here the Delay function?
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Help with Delay

Postby Ayuto » Mon May 23, 2016 9:06 am

I honestly don't know why you are using named delays. There is absolutely no need to ever use them. However, the problem with your code is that you cancel the delay "EndMapAnnouncer" and then create a new one called "EndMapAnnouncer". That's fine, but then you overwrite the delay with three more delays! So, when you cancel "EndMapAnnouncer" again, only the last created delay will be canceled. You can easily work around that issue by enumerating your delays.

Code: Select all

    named_delays.cancel_delay("EndMapAnnouncer1")
    named_delays.cancel_delay("EndMapAnnouncer2")
    named_delays.cancel_delay("EndMapAnnouncer3")
    named_delays.cancel_delay("EndMapAnnouncer4")
    named_delays.delay("EndMapAnnouncer1", timeleft-120, announceEndMap, 120)
    named_delays.delay("EndMapAnnouncer2", timeleft-60, announceEndMap, 60)
    named_delays.delay("EndMapAnnouncer3", timeleft-30, announceEndMap, 30)
    named_delays.delay("EndMapAnnouncer4", timeleft-10, announceEndMap, 10)
But I highly recommend to not use named delays!

PS: I'm assuming you have updated the code from the other thread to use the Delay class, because tick_delays doesn't exist anymore.
decompile
Senior Member
Posts: 417
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Help with Delay

Postby decompile » Mon May 23, 2016 6:34 pm

Yes I Have.

I was using the eventscripts version of the named gamethreads, and it worked fine there.
And yes I updated it abit.

If I shouldnt use named delays, how would that look like?
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Help with Delay

Postby Ayuto » Mon May 23, 2016 6:42 pm

It looks really similar.

Syntax: Select all

from listeners.tick import Delay

def my_delay_callback(x, y):
print('CALLED', (x, y))

# Create a delay
my_delay = Delay(5, my_delay_callback, 'Hello', 'World!')

# Cancel a delay
my_delay.cancel()

See also: viewtopic.php?f=8&t=1036
decompile
Senior Member
Posts: 417
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Help with Delay

Postby decompile » Mon May 23, 2016 10:41 pm

Mhm, it seems like when you put them outside of a function, they get called instantly on load.

How do you store them properly for the script in the first post?
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Re: Help with Delay

Postby satoon101 » Mon May 23, 2016 11:43 pm

You could always use a repeat:
http://builds.sourcepython.com/job/Sour ... TickRepeat

Or, if you really just want to use Delay, you could always use functools.partial.
Image
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Help with Delay

Postby Ayuto » Tue May 24, 2016 7:42 am

decompile wrote:Mhm, it seems like when you put them outside of a function, they get called instantly on load.

Works fine for me. How does your test code look like?

Syntax: Select all

from listeners.tick import Delay

def my_delay_callback(x, y):
print('CALLED', (x, y))

my_delay = Delay(5, my_delay_callback, 'Hello', 'World!')

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 100 guests