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

Author Topic: Transferring data between states  (Read 756 times)

0 Members and 1 Guest are viewing this topic.

lafoniz

  • Newbie
  • *
  • Posts: 19
    • View Profile
Transferring data between states
« on: June 16, 2016, 08:33:26 pm »
Hello folks. Many people here are aware that using finite state machine is a good way to handle switching between different stages in game like title screen, menu screen and so on.

I have used this idea few times successfully implementing this kind of functionality to my games, however I'm still not 100% sure how to solve one particular problem.

Till now my game projects used to be very simple, so I didn't have much information to send between different stages, but now I'm working on a game which is much more heavily depending on information transfer.

For example I have stage which is responsible for character creation and another one which is using that information from creator. This isn't the only thing I need to exchange, that's why I'm wondering, how should stages communicate in case I would have more complicated cases?

I was thinking about something similar in a way to a TCP socket. So I could safely exchange packets of important information from one stage to another, should I follow this idea?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
AW: Transferring data between states
« Reply #1 on: June 16, 2016, 11:12:30 pm »
There are many ways to go about it. Depending how you've designed your state classes, I tend to create the states inside another state. This allows to pass whatever you need to the state's constructor.
For things tha need to be passed to most or all states, you could also create a context class/struct or similar.

Keep in mind that if states (or classes in general) depend too much on each other, you might want to rethink your design a bit.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/