Hey!
Say you have a finite state machine where states are represented by C++ classes, all derived from a common flow state base class. The FSM is used to model the flow of the game and you need to somehow pass data from one state to another. For example, consider the main menu state where the user selects the level to load from a drop-down menu and presses the start button. At this point a transition is fired which means that you exit the main menu state and enter the loading state. Now, the loading state somehow needs to know which level was selected in the previous state.
How do you (or would you) handle this? I can come up with a few ways (all bad):
1: Make the states read from and write to some global data storage. This is can be broken in so many ways.
2: Add a name-value map to each transition to which the "from-state" writes to and the "to-state" reads from. This is quite flexible but leads to problems where you change parameters and on the sending side and forget to change them on the receiving side. In, short it's not type safe and error prone.
3: Have every state that wants to set values on the next state get a pointer to the next state right before executing the transition, and manually call methods on that state. This can be made type safe and efficient but it introduces dependencies between states that I would like to avoid.
Perhaps using templates one could make a version of number 2 that would be type safe, but that sounds awfully complicated for such a simple thing. Also note that transitions can be made pending, so you would need to allocate temp storage for all data until the transition is executed.
Ideas?