who handles the events-loop in the background?

Dear friends, I want to use event in c++, so I write the following lines:


//part 1.
class myClassName : public EventReceiver{

public:
~myClassName ();
myClassName ();

void print_hello(Event * aEvent);
}

Void myClassName::print_hello(Event * aEvent){
cout<<“Hello”<<endl;
}
PT(EventReceiver) myListener;
myListener = new myClassName();

//part 2.
PT(EventHandler) myHandler;
myHandler = EventHandler::get_global_event_handler();
myHandler->add_hook(“print_hello”, myClassName::print_hello);

//part3.
PT(event) = myEvent;
myEvent = new event(“print_hello”, myListener);

//sending directly
myHandler->dispatch_event(myEvent);

:unamused:
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?
:unamused:

Thank you in advance. :confused:

I have gotten the answer.

Thank God! :smiley:

Thank You! :slight_smile:

So for common interest: what was the answer?

BTW. where is Panda events management in C++ documented
how to get the equivalent of base.accept, acceptOnce, ignore and the whole messenger stuff?

it is PandaFramework :smiley:

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().

David

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().

David

okay thanks drwr

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.

David

thanks, I rewrote my class declaration for GameObject and it works now. I guess I just had some typos or something in there.

I am now having trouble adding the hook.

void GameObject::print_hello(const Event * aEvent)
{
	std::cout<<"Hello"<<endl;
} 


GameObject::GameObject()
{
 PT(EventHandler) myHandler;
 myHandler = EventHandler::get_global_event_handler();
 myHandler->add_hook("print_hello", this->print_hello); 
}

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]

Declare the print_hello() method to be “static” in the class definition, e.g.:

class GameObject {
public:
  static void print_hello(const Event *aEvent);
};

David

Thanks David that did it.

It is now working hooray!
Minor note: Eventhandler didn’t like the PT() pointer method so I used a standard pointer declaration instead.

Here is the working constructor for general interest:

GameObject::GameObject()
{
 EventHandler* myHandler;
 myHandler = EventHandler::get_global_event_handler();
 myHandler->add_hook("print_hello", this->print_hello); 
}

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.

This is the standard way to solve this particular problem in C++. You can see the code in pview.cxx does this too.

David