My questions are:
1.Do I use the classes correctly?
2.Can we post an event to the default EventHandler?
3.What should we do next to process the event?
4.Who handles the events-loop in the background?
Yes. See the API docs for EventHandler for more info. You can use PandaFramework.get_event_handler() to retrieve a pointer to this handler (which is also, usually, the same as the global EventHandler).
PandaFramework adds a task called “event” that processes the event queue on its event handler.
You can use the throw_event() function defined in throw_event.h to add a new event to the event queue. To listen for an event, use event_handler.add_hook().
I am trying to do the same thing, I tried using your code in the first post but when I tried to create a pointer to a EventListener
“PT(EventListener) myListener”
it gave me an error saying that EventListener doesn’t have a ref() function.
when I tried to use
EventReciever* myListener
it gave me error: LNK2001: unresolved external symbol.
I basically want to have game objects that have their own custom variables like health and such, can react to events, and can tinker with nodepaths. I think extending EventReciever seems like the best way to do this but I’m new to Panda, does anyone have any suggestions?
EventReceiver is perhaps a bit misnamed; the class doesn’t actually receive events, but is rather allowed to be associated with each event sent.
If you want to receive events, you don’t need to use EventReceiver. You just add a hook to the global EventHandler for each event you want to receive. You need to look at EventHandler::add_hook().
So I could really extend any class to create my game objects, or just create my own class, it just needs to have an #include for EventHandler and NodePath.
Do you know which class is best to inherit from? perhaps MemoryBase? or TypedObject?
I also need to be able to get a pointer to whatever class it is somehow. Does Visual C++ have a problem with declaring pointers like this
GameObject* Player1;
??
I’m asking because it gave me that error earlier, I’m used to using MinGW and Codeblocks.
There is no one best class to inherit from in all cases. You should inherit from MemoryBase if you care about interfacing with Panda’s memory-management system. You should inherit from TypedObject if you can about using Panda’s dynamic typing system. If you don’t know or care about either of these, you shouldn’t inherit from them. My advice would be not to inherit from these classes unless you understand them fully.
That is a standard C++ way to declare a pointer. It’s certainly legal in Visual Studio as well as any C++ (or C for that matter) compiler.
1>.\GameObject.cpp(19) : error C3867: ‘GameObject::print_hello’: function call missing argument list; use ‘&GameObject::print_hello’ to create a pointer to member
I can’t figure out how to pass the second parameter to add_hook.
Thanks in advance.
[/code]
Alright, I realized that I actually want to have the events call functions on individual instances, not just static functions for the class. So I used the optional third parameter in add_hook to pass a “this” pointer. Then in the static function, I typecast the pointer back to GameObject and call a non-static function on it.
GameObject::GameObject()
{
EventHandler* myHandler;
myHandler = EventHandler::get_global_event_handler();
myHandler->add_hook("print_hello", GameObject::print_hello, this);
}
//static function called by event
void GameObject::print_hello(const Event * aEvent,void* pvoid)
{
GameObject* pObject = static_cast<GameObject*>(pvoid);
if (pObject)
pObject->print_individual();
}
//non-static function
void GameObject::print_individual()
{
std::cout<<"Hello Event called successfully"<<endl;
}
this is working, but is there a better way to accomplish the same result?
I plan to have a lot of instances of subclasses of GameObject that respond to events like “OnLoop” “OnCollision” “OnKeyboardPress” etc.