Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Switching between event processors  (Read 1327 times)

0 Members and 1 Guest are viewing this topic.

Robostove

  • Newbie
  • *
  • Posts: 4
    • View Profile
Switching between event processors
« on: March 17, 2014, 07:42:00 am »
Is there a best practice for switching between event handling functions?

I'm working on an application with several different modes that I need to switch between, each with different requirements for event processing. (They each have their own "game loop")

Currently, these event processors are member functions of different classes. I flip a boolean inside of an event processor when it's time to move to the next, exiting that game loop. When that loop exits, it sends a code that instantiates an object of the corresponding mode, and the next loop starts.

What I'm worried about is the delay between the different game loops. If it's too long, will the window start "Not Responding"? I'm experiencing some fairly hard to reproduce "not responding" crashes now, and I'm wondering if this is the problem.

Any alternate techniques or critiques of my design will be greatly appreciated. Thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10836
    • View Profile
    • development blog
    • Email
Re: Switching between event processors
« Reply #1 on: March 17, 2014, 08:35:48 am »
I'm not quite sure whether I fully understood you, but it sounds like you're implementing some sort of state machine, right?

What kind of object are you instantiating? The time needed for an object "creation" comes essentially down to memory allocations + init steps. If you're loading many textures from the harddisk, then this might cause some longer lag, since it's a very slow process. If you just initialize some variables, then there shouldn't really be any noticeable delay.

The "not responding" issue occurs when you don't process any events (call pullEvent) for around 10s. Do all your "states" process events?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Robostove

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Switching between event processors
« Reply #2 on: March 17, 2014, 09:11:23 am »
Thanks for clarifying how long it takes to time out due to lack of event processing, eXpl0it3r.

Yeah, my program is a (fairly simple) state machine. In between two states (event processing loops) I was loading a few textures, generating several thousand sprites from them and assigning them random positions and orientations. This delay was quite short (much less than a second), but I feared that might be the issue.

Instead, I found a few places where I was passing bad input to functions, cleaned them up, and I haven't seen a crash yet. The delay definitely wasn't my issue.

Still, I am still curious about other ways that I could switch between game states. I haven't come up with anything better than bools and return codes.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10836
    • View Profile
    • development blog
    • Email
Re: Switching between event processors
« Reply #3 on: March 17, 2014, 09:51:01 am »
There are so many approaches to state machines, for my SmallGameEngine (which needs a good refactor), I've used some a bit more complex general loop that checked for a new state instance by calling the function next(), thus if you want to create a new state, you simply instantiate a new state class and assign it to the m_next member variable and in the next main loop iteration the new state will be used by either replacing the old one or pushing it back on the state stack.

Other implementations I've used, simply returned the next state (if there was one), thus the state itself was fully contained and when the execution got back to the state "manager" the state was official finished.

Those are just two examples and all of them have their pro and cons, just make sure whatever you choose fits your overall application design. :)
« Last Edit: March 17, 2014, 09:56:20 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/