Jump to content
  • Advertisement
Sign in to follow this  
bossmonkey666

Game engine event system

This topic is 4960 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

Im looking at starting a C++ OO game engine, this time im going to try to plan all parts from the beginning rather than just coded random stuff that I feel like doing. I am looking at the best way to organise a game, and an event/messaging system appealed to me. But im not entirely sure how to implement one in a game. I found an article on this site which showed a very basic implementation of one but i would like a more in depth article on it. Does any one have any other articles or book suggestions on event/messaging systems? I think I understand that different parts of the engine should be able to register to listen for certain events and when that event is fired off from some other part of the engine that class will be notified and will handle it accordingly. Should everything in the game be based on events?

Share this post


Link to post
Share on other sites
Advertisement
Hi.
I did this myself for an engine and think it's a good idea to have it. You will get a clean design and a good abstraction.

For example:
If you have a collisuion, the collision manager just sends a collisionEvent, then other parts of the system/managers that are interested just listens to the collision-node etc.

I used a basic Node class (like a quadtree) and have a tree based eventsystem, where you can listen on several levels and several points.
Every node can have Events and listener registered to them.
For the events I just use a abstract base class which all eventclasses inherit from.
So every manager or objekt in the system can create nodes, events and listen.

First I had some problems with the performance. Since you could create.. say a hundered events on a single frame, the dynamic allocation is bad for the performance and also will get the memeory fragmented.
I fixed this by Implementing a MemoryPooling system so the object memory gets reused and I have no realy problems with perf and stuff anymore.
You will anyway have good use of a memory system later when dealing with particle systems and things.


/martin

Share this post


Link to post
Share on other sites
Maybe use the window's messaging system if your using that os, thats what im thinking of doing...

anyone reakon that would be a bad idea?

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Maybe use the window's messaging system if your using that os, thats what im thinking of doing...

anyone reakon that would be a bad idea?


Yes. It kills your ability to make things cross-platform.

Share this post


Link to post
Share on other sites
Quote:
Original post by schlinge
I used a basic Node class (like a quadtree) and have a tree based eventsystem, where you can listen on several levels and several points.
Every node can have Events and listener registered to them.
For the events I just use a abstract base class which all eventclasses inherit from.
So every manager or objekt in the system can create nodes, events and listen.


Could you maybe elaborate a bit on this? It sounds very nice.

Share this post


Link to post
Share on other sites
The basic message system is simple, but you have to make a few design choices before you try and lay things out.

You first need to decide how tightly you want to couple your systems; you could actually define very loosely coupled systems that export just the message system. I'd probably only do this for objects outside of the core though, using it to attach entities to the game.

Another choice to make is how you want the messages to be passed around the system; do you want the messages to be propagated down a heirarchy of objects, with the object choosing to react to the message or not? Or do you want the message system to specifically target an object by it's ID? (Perhaps using a signal/slot system).

You should also consider how you'll manage the sequence of the messages, in some instances it's important that messages come in sequence so that you're not trying to revive dead players with health packs and the like. This is especially important if you choose to tie your message system up to your network system.

Other sticky choices I've been facing is how the receiver should be able to respond to the sender. Should it be able to respond directly or should it be able to respond by pushing it's response in the message queue? How about times that you want to propagate messages up the tree from the leafs to the roots? If you will need this, you'll have to design your system accordingly.

You should also look into systems like sigslot, if at least as a way of playing around. I prefer my hand-rolled system to sigslot (I like my message param system) but it could save you a bunch of work in getting up and running.

This is my current area of study, so I apologise if I'm getting carried away ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by schlinge
I used a basic Node class (like a quadtree) and have a tree based eventsystem, where you can listen on several levels and several points.
Every node can have Events and listener registered to them.
For the events I just use a abstract base class which all eventclasses inherit from.
So every manager or objekt in the system can create nodes, events and listen.


Wow, that sounds like my current implementation too. Each 'entity' is effectively a node that can receive messages; depending on the handlers attached to it and it's default relay proc it'll pass the messages down it's tree of attached entities. The main thing I'm building in now is a FSM to allow the user to switch siwtch on/off the responses to these messages depending on the entitiy state.

Share this post


Link to post
Share on other sites
Quote:
Original post by bossmonkey666
I think I understand that different parts of the engine should be able to register to listen for certain events and when that event is fired off from some other part of the engine that class will be notified and will handle it accordingly. Should everything in the game be based on events?


I wouldn't say everything, for game engine itself there probably wont be many besides. Personally i think a message based system is a little too indirect for just the internals of a game/engine i would suggest an event system based on callbacks or observer design pattern and nice idea is to use a signals & slots framework if your using C++ the boost libraries has such a framework you can see that here.

Share this post


Link to post
Share on other sites
i agree with snk_kid.
I am using signals/slots, smart pointers (shared_ptr) and boost's graph system for my vis-engine.
I totally would recommend it to everyone who is programming in C++.

Share this post


Link to post
Share on other sites
Thanks, I think I will try out the boost signals thing. It looks relatively easy to implement so it should save me a lot of time. When I get more comfortable with the idea of an event system and I have a bit more time I might try creating my own but the boost signals look like they will work for me for now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!