About the event manager

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • About the event manager

      Hi there!

      I have a question about the basics, fundaments of using events.
      What are the major advantages of using events?

      OK, I know it should split some parts and make easier the programming, and other things... well, I want to know your opinion, and of course mike's too!

      I never developed a "big game", only very little ones, and for example, in the update method of the main character is where I used to check the user input...
      Something like this...

      Source Code

      1. MainCharacterUpdate() {
      2. // fancy stuff here
      3. ...
      4. dx = vel * key.right - key.left;
      5. jump = key.space;
      6. ...
      7. // more fancy stuff over here too
      8. ...
      9. if(jump) {
      10. ...
      11. }
      12. x += dx;
      13. y += dy;
      14. ...
      15. }
      Display All


      But now, using the event manager we would have something like this... (right?)
      Checking input...

      Source Code

      1. if(key.jump) {
      2. Event::send('keyManager', 'jump');
      3. }
      4. if(key.left) {
      5. Event::send('keyManager', 'moveLeft');
      6. }
      7. ...

      Updating game logic...

      Source Code

      1. if(bullet.collision(player)) {
      2. Event::send('game', 'target');
      3. }
      4. ...


      Why should we do things like that?

      I also wonder if the "event traffic" is much too overload for checking the game logic. Is it worthable? // Besides, of course, of having to develop a Event Manager class...
      The thing is we will have hundreds of events per frame, right?

      If this paradigm is being used in "big games" like Ultima and other AAA titles, is because this is a good way to go!, but I would like the real reasons, as I didn't really get it reading the book...

      Sorry for the n00b question ^^ (and my poor English)
      Thanks in advance!
    • Events are extremely helpful for decoupling systems. For example, in The Sims Medieval, we have a class called DeathManager that manages all the sim deaths that occur. When a sim dies, the DeathManager is responsible for locking the sim in the Expire interaction, summoning Death (the grim reaper), and then dealing with the final clean up of destroying the Sim object. Right before the sim object is destroyed, it sends an event that anyone can listen to called kSimDied. That way, any system that cares can handle the death appropriately. If the sim was a professional sim, the profession manager can remove him, if the sim was a role sim or quest sim, the role system can remove him and choose another to take his place.

      Without events, the death manager would have to know about every single system that cares about sim death. It really doesn't need to care about households, lots, family inventory, the bouncer system, the role system, the profession system, etc. If we decided to rip out the role system entirely or refactor it, we'd have to update the DeathManager as well. With events, we don't have to touch it. We simply stop listening for the death event, or perhaps have someone else listen for it. The point is, the DeathManager class is now decoupled from the rest of the code.

      Of course, this comes at the price of obfuscation. Sometimes it's not clear what caused a particular event to fire or why someone perhaps didn't respond to it. Since events are often saved and processed elseware (like in EventMgr::VUpdate()), you can't easily trace the call stack back.

      Another nice thing about events is that you can distribute processing across multiple frames. For example, let's say you have 100 events in your queue. You can loop through those events and keep processing them until some set amount of time has elapsed, then bail and continue processing the events next frame. You have to be careful that you don't starve your event system and lose events to slow frame rate.

      -Rez
    • Thanks for that useful reply! :D

      But then you have to have some kind of "threshold" that decide which action should be processed through an event and which one doesn't need it.

      You told me an example (dead of a sim) of an event triggered once in a big while. But what about other actions (like pressing a key to move the main character), does it generate an event or is better to process the action without events?

      I think that's up to each programmer right? But with a little tip I will be grateful ^^
    • Game Programming is 2 parts art, 1 part science, and 1 part luck. Your question falls within the art side. You learn what needs events and what doesn't through instinct and trial & error, mostly. There's no hard & fast rule.

      Regarding key presses, I do send an event for each keypress and mouse movement in my personal engine. I did something very similar on Rat Race back at Super Ego Games. I'm not sure how The Sims Medieval does, but I *think* we send events as well (I'm an AI programmer so I don't really have to deal with the input system).

      If you think about it, input events are pretty rare too. A game runs at about 30 frames per second. How often is a keypress going to really change? Even if the player is constantly moving the mouse, that's still only one event per frame. In my personal engine, I have a limit. If the player simultaneously generates more input events than that limit, I ignore the extra ones.

      The other nice thing about using input events is that they can be saved off along with the game time and random number generator seeds. By using this data, you've just created a simple little replay system.

      -Rez

      (edit: Spellcheck + Grammercheck = Win)

      The post was edited 1 time, last by rezination ().

    • Rez seems to me game programming is 2 parts art, 2 parts science, 3 parts hard labor, 2 parts frantic debugging, and about 6 parts luck.

      :)
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Hahaha! Fair enough, that's probably a bit more accurate. Some part of that is also dealing with stupid software. If I get that "Visual Studio is busy, please be patient" pop one more time, someone is going to have to die. ;)

      By the way, where did all my stars go? Shouldn't I have stars under my screenname? Did I overrun the star variable so now I have negative stars?

      -Rez