Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This is a slightly vague one but I am currently looking at a couple of areas of my current framework, such as spatial partitioning and UI based menus, and with UI menus it makes sense to have an event based system so you can tell if the user has clicked on it etc... its like your own small version of winforms.

However for some other areas it got me thinking that I could expose a lot of evens for other things, such as when an object moves so it could be re-allocated in a spatial tree of some kind if needed, or if it needed to calculate a collision it could return an event.

Anyway I quite like using events everywhere as it decouples you from having to know about who needs to know what, however I was wondering if there were any hidden gotchas with using events on the Xbox/Phone. I read a few older posts that mentioned that there are performance penalties with events on non PC platform (i.e http://blogs.msdn.com/b/shawnhar/archive/2007/07/09/delegates-events-and-garbage.aspx)

So is this still an issue or has XNA and the .net implementation on these machines improved in this area, as it would be nice to add events of all kinds to high level entities, like Player.HealthChanged, Creature.AnimationStateChanged etc, but I dont want to go exposing these for usage if there is a major performance overhead with doing so...

share|improve this question
    
Have you considered that this article about delegates garbage is 5 years old? –  Gustavo Maciel Jan 19 '12 at 13:45
    
"I am not aware of any way to subscribe to an event without causing memory allocations. Raising events does not necessarily allocate, though, so you'll be fine as long as you can hook everything up ahead of time while loading your levels." Basically the whole articles is telling you not to subscribe/unsubscribe on the fly. –  ClassicThunder Jan 19 '12 at 15:40
    
@Gtoknu Thats why I said " I read a few older posts that mentioned ..." I know its old, thats why I am asking here to see if it still applies. You can read documentation about IO in .net which is over 5 years old and still applies. –  Grofit Jan 19 '12 at 16:53
add comment

1 Answer 1

up vote 2 down vote accepted

On all platforms there is a small overhead to calling a function though an event compared to just calling the function. This overhead is pretty small and its only related to calling the event so the longer the event the less significant the impact.

A smart use of events can actually help performance as you are only working when necessary. Its the usual cost/balance issue you will always run into with programming. If you keep you events low in number and have them do a significant amount of work it should actually help performance. There are a couple of gotchas though.

  • Pileup

You might needs to make a way to prevent events such as a button press from getting spammed. I know when I get frustrated I keep smashing what ever button button didn't do what I want.

  • Inconsistent running speed

If you have an event that triggers something hefty, maybe run an A* algorithm, you can make you game stutter assuming the workload varies enough to be noticed by the user. Though with XNAs 60hz update speed you have to exceed the time of a cycle to cause this.

Also making the events asynchronous with BeginEvoke and EndEvoke can get you another performance gain, it takes some intentional designing around but might be worth it in your case.

I've worked with the XBox but not WP7; however, looking at the most resent Performance Considerations from Microsoft I see nothing about events being particularly slow.

share|improve this answer
    
Thanks for the info, I am more than happy with it allocating the normal amount of memory, as it has to store the subscribers somewhere. The thing that I was worried about was if I were to start adding events to trigger other processes like you say, I didnt want it to cause any extra overhead, so from the sounds of it, I can just use them as I would expect and like you say hopefully get performance improvements and decouple my components further. –  Grofit Jan 19 '12 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.