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

Author Topic: Having multiple event loops in a single program  (Read 1212 times)

0 Members and 1 Guest are viewing this topic.

willem640

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Having multiple event loops in a single program
« on: October 02, 2019, 11:05:35 pm »
Hi, I am currently developing a game. The game has an event loop in which all the events related to the game itself are handled (keyboard input, mostly). I would like to add a start menu, with possibly multiple levels of menus where the user can select preferences (avatar, etc.). The game is not running while this is happening and no event for the game should be processed. Additionally, while the game is running, the menu will not be active and events for it should not be processed.

I was thinking about adding a dedicated event loop for the menu, and progressing to the game loop when the user has started the game.
Is this bad practice? If so, what should I do instead? Adding if statements everywhere to poll the state of the program (menu/game) doesn't seem like a very good solution to me.

Thanks!

I'm very sorry if this is the wrong forum, I'm not very familiar with the layout.
« Last Edit: October 02, 2019, 11:11:27 pm by willem640 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Having multiple event loops in a single program
« Reply #1 on: October 03, 2019, 12:42:58 am »
For handling different states it's common to have a sort of state machine with different polymorphic state classes, but that's just one approach.
There at least two approaches for your state classes, have each state class have their own dedicated game loop or have each state implement functions that get called from the state machine's game loop.
With the second approach you could simply have some ProcessEvent(sf::Event event) function and pass along all the events from the one event loop.

Don't forget, the best solution is always the one that makes your code less complex and more maintainable.
Meaning, just because one can add state classes and a state machine, it isn't always the best option, especially when a single if can do it just fine. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

willem640

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Having multiple event loops in a single program
« Reply #2 on: October 03, 2019, 08:52:46 am »
Thanks! I'll try that

 

anything