Task and classes

Hi, Just started experimenting with Panda3D, which I chose to build a prototype for a game. I want to use solely C++. I did the basic Hello World panda tutorial, and everything works and compiles just fine (after figuring out the missing libraries, such as pthread).

As for the style, I need some guidance. All of the samples converted to C++ put all of the code in a class, including the task function (making it a member function). Looks fine, until you realise that in order to put that function in the task manager, you need to send its pointer. C++ forbids using a pointer to a member function. gcc enforces that, but I would assume its not the case of whatever was used to write/build the samples.

So my question was how to people usually use task? Where are they declared and implemented? Also, I would be happy to see some proper example (not the samples) of C++ game with Panda3D using a proper (and legal) style, I have been unsuccessful in finding any such example on Google (although there are heaps for Python).

Thank you!
Tom

The most common approach is to make the task method in question static, and passing the class instance by reference as an optional argument.

MyClass.h

class MyClass
{
    public:
        MyClass();

        static AsyncTask::DoneStatus example_method(GenericAsyncTask *task, void *data);

    private:
        PT(AsyncTaskManager) task_mgr;
        PT(GenericAsyncTask) example_task;
}

MyClass.cpp

MyClass::MyClass()
{
    this->task_mgr = AsyncTaskManager::get_global_ptr();

    this->example_task = new GenericAsyncTask("exampleTask", example_method, this);
    this->task_mgr->add(this->example_task);
}

AsyncTask::DoneStatus MyClass::example_method(GenericAsyncTask *task, void *data)
{
	MyClass *self = (MyClass*)data;

	return AsyncTask::DS_cont;
}

Another way is to have a class that inherits from AsyncTask and implements the virtual DoneStatus do_task(); method.

@ksmit799 Genius! Looks exactly what I was looking for :smile: