Sign in to follow this  

Event Handler - How?

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,
I have implemented a minesweeper game. The game prints everything in text based. Now I want to put a gui (using GTK+) for my game but i didn't want the game logic to know anything about the gui. So, I have implemented an event handler to make connection between game elements (logic and gui) and make my logic independent of drawing stuff (so, the same game can be text based, gui based, or even opengl based without changing anything in the game logic). However, after trying to actually use it in my game I got into couple of problems, so I decided to share my event handler design with you and ask some questions about it.

Design:

Event Class -> Stores event_id and stored parameters
EventContainer Class -> Stores all the events
EventHandler Class -> Stores the functions to handle events. Has an update function which calls the functions. Sends/Receives the events to/from the container.

There will be one global EventContainer and EventHandler for each game element. This gives me an option where none of the game elements "know" about each other because all the game elements send events to the container and updates themselves by taking the events from the container.

Q#1: My problem is the update function. When the application uses an infinite loop (like a basic 3d shooter) the design works because the event updates on every frame. However, what if I use a game that updates only when I click on something (like a minesweeper game or 2D chess where there are only two game elements: logic and gui). How should I handle it? Where should I put the event handler update functions?

Q#2: Should I use integers or strings for events unique ids?

Q#3: Right now the game is just a minesweeper. What if I will use that event system for a basic strategy game (my 2nd goal)? Is it an efficient system? As looking at my own design, all elements are updated from one event container. Will that be a bottleneck?

Please tell me anything unclear on my questions (Im bad at english, so sometimes my sentences are a little bit unclear. sorry about that) or if you want me to post the full source.

Thanks,
Kasya

Share this post


Link to post
Share on other sites
Q1, you may try observer (or listener/dispatcher) pattern. The GUI is the dispatcher, the logic is the listener. When clicking, GUI dispatch the event directly to listeners.
So it works without a update loop.

Q2, integer may be better performance. You can still use string name to register the integer IDs. See how windows registers customized message.

Also, try read something about signals/slots, boost::signals, or libsigc++, you may find something helpful to implement an event system.

Share this post


Link to post
Share on other sites
Thanks for the help. I am reading about signals and observer pattern. Sorry for that late response also. Just have a very limited access to internet these days :(

Share this post


Link to post
Share on other sites

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this