Tasks on member function or single functions

Hello!..
I was studying the Task system and all examples used on the manual are not object oriented, so I was thinking there are two ways to realize tasks…

  1. static member functions
  2. non-member functions

I guess in my opinion OOP is the way to create games (at least an easy way)
static member functions is not a problem at all, the problem is (I guess) when you manipulate many NodePaths in your task, all those NodePaths should be static too… Is this ok?..

Non-member functions is fine too, but that breaks OOP and I need to pass my NodePath as reference to the function…

What is the way you experimented guys use?.. I’m very new and ignorant in how P3D callback Tasks.

Thank you!

I’ll confess that I’m not all that familiar with the C++ side of Panda’s usage, but have you tried using a member function for a task? I don’t see why it wouldn’t work, even if it’s not used as an example.

yes!.. member functions works fine, but all NodePath used inside the member function task must be static too… so I was thinking if this is a good practice, I never used private components marked as static at least… and not sure if it is fine…

Wait, why would a NodePath used inside a (non-static, to be clear) member function need to be static?

Nodepath should be static only inside a static member function…

I do recognise that–let me try to express what I’m getting at this way: Why need the member function be static? Why not have a non-static member function?

OK, now I understand.
In fact you are right and should be the that way, but I could not get the right argument to pass to…

&class::taskfunct. Only works with static function, I tried to create a delegate with no success…

Hmm… That I don’t know about, I’m afraid! Perhaps one of the members who uses the C++ side more than I will know why that is.

1 Like

There are two interfaces to create custom tasks using the C++ API: AsyncTask and GenericAsyncTask.

For AsyncTask, you create a class that derives from AsyncTask, and override the do_task() method to implement the functionality of your task. You can store whatever you want on your MyTask object, such as the NodePaths it needs to operate on, or some game object manager it can retrieve stuff from.

Example:

#include "asyncTask.h"
#include "genericAsyncTask.h"

class MyObject;

class MyTask : public AsyncTask {
public:
  MyTask(MyObject *obj) :
    AsyncTask("My Task"),
    _object(obj) {
  }
protected:
  virtual AsyncTask::DoneStatus do_task() {
    // do stuff
    return AsyncTask::DS_cont;
  }
private:
  MyObject *_object;
};

class MyObject {
public:
  void start_the_task() {
    _the_task = new MyTask(this);
   AsyncTaskManager::get_global_ptr()->add(_the_task);
  }
private:
  PT(MyTask) _the_task;
};

For GenericAsyncTask, you don’t derive from it, but you implement a static callback function that will be called every time your task needs to execute. One of the parameters to GenericAsyncTask is void *data, which will be passed to your static callback function. You can store whatever you want here, and just downcast it to the appropriate type in your callback function. What I generally do for GenericAsyncTask is create the task under some object, and pass the this pointer as the user data, then implement the callback function as a static method under the class of my object, and downcast the user data pointer to the type of my object. That way I can access all of the non-static methods and members of the object that the task is operating on.

Example:

#include "genericAsyncTask.h"
#include "asyncTaskManager.h"

class MyObject {
public:
  void start_the_task() {
    _the_task = new GenericAsyncTask("My Object's Task", &do_the_task, this);
    AsyncTaskManager::get_global_ptr()->add(_the_task);
  }
private:
  static AsyncTask::DoneStatus do_the_task(GenericAsyncTask *task, void *data) {
    MyObject *self = (MyObject *)data;
    // do stuff
    return AsyncTask::DS_cont;
  }
private:
  PT(GenericAsyncTask) _the_task;
};

Hopefully this is clear and shows you how you can utilize the C++ task system.

2 Likes

!that_is_a_bad_example

Thank you!

1 Like