Sign in to follow this  
3DModelerMan

Storing event types?

Recommended Posts

I'm doing an event system for my game right now, and I came across the problem of event types. What should I store them as? I thought about an enum, but then it would have to be recompiled for every event. Would a simple string be a good idea? Or does that seem to slow and use too much memory? I've also thought about requiring event types to be added to a std::map that maps the string event names, to an integer value generated by a hash algorithm. That way, the string's hash value could be used for comparisons, instead of the string itself.

Share this post


Link to post
Share on other sites
You should be able to delete your own thread (as long as there are no replies) by clicking edit on the first post, checking the delete this post box near the top, and then hitting the button ("Make Modifications"?). I'm replying to this one since the other one still has no replies for obvious reasons [lol]

Share this post


Link to post
Share on other sites
The usual advice applies: make it work, then make it right, then make it fast. Use strings first. If you find they're too slow, then try hashing.

As an aside, I think the "can't use enums because everything has to be recompiled" argument is a little bogus, at least for hobby projects. Unless your project takes 20 minutes to rebuild, and your enum is included in every file, and you're going to be adding message types like crazy, then compile times just aren't going to be that big a deal.

Share this post


Link to post
Share on other sites
You'll probably have to recompile with the string thing too unless I'm misunderstanding what you're doing, and I probably am.

Do you have any code samples or examples or whatever?

Share this post


Link to post
Share on other sites
It's at least possible to do less recompiling with strings, because new modules can support new types just by adding a few lines of processing code. Others can just ignore the new possibilities, and don't need to be touched. Also, it's easier to make a more data-driven system with strings: no string-to-enum translation phase. If you keep them reasonably short, it probably won't be a big problem, especially since comparisons are likely to return after going through just a couple characters.

Share this post


Link to post
Share on other sites
Yeah, I'm going to do a LUA scripting system for my project. That's another reason why enums won't work. Would it be very difficult to switch a string for a hashed string? You would only have to do a few things differently in the events and event manager. The thing I'm worried about, is building the string based one. Then having a difficult time switching it to be hash based, because the event handling code needs to change.

Share this post


Link to post
Share on other sites
All you'd need to do is have your messaging system hash message strings before performing the lookup in the listener registry, and possibly cache the results. It shouldn't be difficult to change if you need.

Share this post


Link to post
Share on other sites

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