sohomsahaun / SnowState

Finite State Machine for GameMaker
MIT License
140 stars 10 forks source link

Internal variables for states #40

Closed paxer closed 10 months ago

paxer commented 10 months ago

It would be useful to have states' own internal state variables, e.g., the variable that is available only within this state and accessible and can be mutated by all state events.

currently, this will be an error

fsm = new SnowState("walk")

fsm.add("walk", {
    _some_internal_var: 123,

        enter: function() {
            // error Variable _some_internal_var not set before reading it.
           show_debug_message(_some_internal_var)
        }

       step: function() {
           // error Variable _some_internal_var not set before reading it.
           show_debug_message(_some_internal_var)
       }
sohomsahaun commented 10 months ago

It is less likely for state-specific variables to be a thing in SnowState. You have to use instance variables instead.

There is a workaround though, which I highly discourage. You can declare instance variables in the enter event and use it in the state.

fsm = new SnowState("walk");
fsm.add("walk", {
       enter: function() {
           __walk_internal_var = 123;
       },
       step: function() {
           show_debug_message(__walk_internal_var);
       }
}