Jump to content
  • Advertisement
Sign in to follow this  
3DModelerMan

Fastest event system architecture

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

I'm working on an event system for my game engine, and I've been thinking about what the architecture should look like. I want event types to be registered with the event system before they can be used. That way scripts can create event types. I also want listeners to be able to register for what kind of events they want to receive. Would it be better to have a vector filled with vectors of event listeners, and the first vector is indexed with the event type? The problem I can see here is that although it's fast to send an event, I have duplicated pointers. Or would it be better to store the listeners, and then the event types they want to listen for?

Share this post


Link to post
Share on other sites
Advertisement
Check out Game Coding Complete, it has a good event system implementation. It pretty much has a vector of listeners for each event. Eventypes are just hashed strings so they don't need to be compiled as enums or anything which works great for scripts. Event types are first registered when you first add a listener for one (sending events that are not listened for are just ignored as far as I can remember). One listener can listen for many events so you do have alot of duplication as you say. Every event has a vector of listeners.

Anyway, great book and the event system is definately worth a look.

Share this post


Link to post
Share on other sites
I would do it with Boost.Signals. If your event types are in an enum, use std::vector<boost::signal(void (EventData))>; if they are arbitrary strings, std::tr1::unordered_map<std::string, boost::signal<void (EventData)>>.

Share this post


Link to post
Share on other sites
I'm actually using hashed strings because that's my favorite game programming book and I really liked the event system in it. I want to be able to only send events of certain types to listeners that registered for that type. For example: if listener "a" registered to receive game over events but not player move, then it would not get OnEvent called for player move, only game over.

Share this post


Link to post
Share on other sites
If you're implementing an event system for fun, then keep thinking about it.

If you're implementing an event system because you just need one, then follow alvaro's advice and use Boost::Signals. You're reinventing the wheel.

Share this post


Link to post
Share on other sites
I'll check out the game coding complete architecture in a little more detail. I don't really have a deadline on this system, it's an upgrade I've been on the side, my current main project is using a different engine and I wanted to write something a little more flexible than I had last time. Thanks for the help, I'll look into boost.signals, but ultimately I think I'll end up going with the game coding complete system.

Share this post


Link to post
Share on other sites
If you want a high-performance event system, then throw out the design of "anything can raise an even that anything can catch". Instead, actually find the pairs of objects that need to communicate with each other and plug them together with a direct communication line, cutting out the need for a dispatcher system.

Share this post


Link to post
Share on other sites
if you want an easy to understand example of something that uses a system like Hodgman describes look at how .Net(like in winforms or wpf) handles events through delegates.
An object can register a method to be invoked as long as it has the same signature as the handler. When an event is raised - as long as the handler as atleast one subrciber those methods are called. Similarly done in iOS and probably many many systems.

Share this post


Link to post
Share on other sites
If performance is important, don't use Boost::signals. It's well known for not good performance.
There are a lot of alternates to implement signal/slot system in C++, just Google it.

Share this post


Link to post
Share on other sites

If you want a high-performance event system, then throw out the design of "anything can raise an even that anything can catch". Instead, actually find the pairs of objects that need to communicate with each other and plug them together with a direct communication line, cutting out the need for a dispatcher system.


Absolutely.

In my quest for pushing AS3 to the absolute limits, I always take the most direct route possible. It's a must if you prioritise performance.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!