task: Fix re-adding task object removing extraArgs (#1132)

Fixes #1097
This commit is contained in:
Timothy Paustian 2023-02-13 03:58:02 -06:00 committed by GitHub
parent 518d4777fa
commit 50de135641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -410,8 +410,10 @@ class TaskManager:
return task
def __setupTask(self, funcOrTask, name, priority, sort, extraArgs, taskChain, appendTask, owner, uponDeath):
wasTask = False
if isinstance(funcOrTask, AsyncTask):
task = funcOrTask
wasTask = True
elif hasattr(funcOrTask, '__call__') or \
hasattr(funcOrTask, 'cr_await') or \
isinstance(funcOrTask, types.GeneratorType):
@ -427,8 +429,14 @@ class TaskManager:
if hasattr(task, 'setArgs'):
# It will only accept arguments if it's a PythonTask.
if extraArgs is None:
extraArgs = []
appendTask = True
if wasTask:
extraArgs = task.getArgs()
#do not append the task to an existing task. It was already there
#from the last time it was addeed
appendTask = False
else:
extraArgs = []
appendTask = True
task.setArgs(extraArgs, appendTask)
elif extraArgs is not None:
self.notify.error(

View File

@ -0,0 +1,28 @@
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from panda3d.core import Vec2
def test_task_arg():
def test(ship, flood, task):
ship.y += flood
return task.done
ship = Vec2(2.2, 2)
flood = 1
base = ShowBase(windowType='none')
task = base.addTask(test, 'test_task', extraArgs=[ship, flood], appendTask=True)
base.taskMgr.step()
assert ship.y == 3
base.remove_task(task)
task = base.addTask(task)
base.taskMgr.step()
assert ship.y == 4
task = base.taskMgr.add(test, 'test_task', extraArgs=[ship, flood], appendTask=True)
base.taskMgr.step()
assert ship.y == 5
base.remove_task(task)
task = base.taskMgr.add(task)
base.taskMgr.step()
assert ship.y == 6