The first time the task gets called

If I add a task to task manager, when does it get called the first time, this frame or next one? (I have an event that sets a variable to 1, and I need to reset it back to 0 on the next frame; so, I want a task to do this. If it gets called immediately after it was added to task manager, i.e. on the same frame, then the rest of the code will never see this variable equal to 0).

I don’t think there are guarantees: it might be called this frame, it might be called next frame.

David

Well, then are there any ways to find out whether it is the same frame or not? How can I get the number of the current frame?

globalClock.getFrameCount()

David

Thank you, sir! :smiley:

The task object you pass into your task function contains information about the task being executed. It has the attributes task.frame and task.time that indicate the current frame and time for that task. The following code will reset a variable on the second frame of the task execution:

myVar = "Lolcat"

def myTask(task):
    if task.frame == 1:
        myVar = "Loldog"
    print myVar

taskMgr.add(myTask, 'testTask')

Hmm… Since, as David said, we don’t know whether the task was called on the same frame or on the next frame relative to the frame when the variable was changed by the event, your method doesn’t help, sorry. Your method simply resets the variable back to 0 as soon as it was called (the manual says that “task.frame: An integer that counts how many times this task function has been executed. Count starts from 1.”)

I prefer this:

myVar = "Lolcat"

def myTask(self, frame, task):
    if globalClock.getFrameCount() > frame:
            myVar = "Loldog"
            return Task.done
        else:
            return Task.cont

#frame when variable changed:
frame = globalClock.getFrameCount()

taskMgr.add(myTask, "resetKeys", extraArgs = [frame], appendTask = True) 

Ah… right, didn’t understand the problem correctly.