Is it possible to run copyTo in a seperate thread?


I’d like to run Nodepath.copyTo asynchronously (in a seperate Thread). Is there an easy way to achieve this?
I’m prepared to change/add C++ code if necessary, I’m just insecure about how to start this task.
Or, if I can get away with a Python only Solution, I’d be fine with that as well.

The reason I need this is as follows: we have some pretty heavy Actors in our game, and to create a new Actor I copy them from a prototype Actor I loaded earlier. This always makes the game stutter, and profiling showed that the bulk of the time the copy operation takes is in the copyTo function. It takes ~0.04 seconds to copy the Actor. Therefore I’d like to do the copying in the background.

Any ideas?



You can do this easily, especially if you are using Panda version 1.6.x. You can create threads using Python calls (but import direct.stdpy.threading instead of Python’s standard threading module). There is lots of information on the web about creating and using threads.

Panda also has some higher-level threading support. You can use the task manager to run some of your tasks in a separate thread, for instance, although documentation for this feature is not yet very complete. Panda 1.6 introduced the concept of “task chains”, and each chain can run in its own thread if you like. A short example:

# Call this once at application start or wherever
taskMgr.setupTaskChain('background', numThreads = 1)

# Call this each time you want to add a task
taskMgr.add(func, name = 'myTask', taskChain = 'background')

Only thing to watch out for when you start doing threading is avoiding race conditions; be careful that the model you are copying is not referenced by anyone in another thread until it has been fully copied.