C++ tasks, can they be methods

I have been playing around a bit with Panda 3D (1.8.0) and C++ lately, and noticed that tasks are done as a function, not as a method in a class.

This is kind of annoying if you got everything else wrapped in classes. I have “sort of” found a workaround, passing the object instance as a parameter to the task, then the task itself can have access to stuff in the object (although I have to make things I need in the class public this way).

Is there any way to have tasks as methods rather than a function, or is this at all planned?

Nope, that just the standard C++ way of implementing a callback function. However, you can use template to let the compiler write the static stub for you. I’ve written some implementations of this technique in the file `p3util/callbackUtil.h’ that can be found here:
github.com/drivird/drunken-octo-robot.git

Thanks, I will have a look at it later on tonight, and thanks for the C++ translation of the Panda3D demos, that is really cool.

Hi

I put tasks in classes all the time as static member functions; you still have to pass the “this” pointer as data, and you can access its private member variables.

I use something like tah mentioned…
I’m using the event_handler in this example, but it’s similar with the use of generic tasks. =)
I didn’t look it at dri’s code yet, but I’d like to thank him for the translation of the Panda’s code to C++! =D

You could make a macro to avoid having to type it “twice”, but I always think whether that is a good a idea =/

class Foo {
   Foo(){
      event_handler->add_hook("test", my_task, this);
   }
   
   void my_task(){
       cout << "Here goes the task..." << endl;
   }

   static void my_task(const *Event, void* data){
      ((Foo*)data)->my_task();
   }
}

Thanks everyone, both dri’s code and using static methods are good solutions. I am using dri’s method at the moment as I quite like it, and that can also be simplified further by using macros as some of his examples do.