Page 1 of 1

Help with Delay

Posted: Mon May 23, 2016 1:28 am
by decompile
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?

Re: Help with Delay

Posted: Mon May 23, 2016 9:06 am
by Ayuto
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.

Re: Help with Delay

Posted: Mon May 23, 2016 6:34 pm
by decompile
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?

Re: Help with Delay

Posted: Mon May 23, 2016 6:42 pm
by Ayuto
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

Re: Help with Delay

Posted: Mon May 23, 2016 10:41 pm
by decompile
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?

Re: Help with Delay

Posted: Mon May 23, 2016 11:43 pm
by satoon101
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.

Re: Help with Delay

Posted: Tue May 24, 2016 7:42 am
by Ayuto
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!')