Search code examples
erlangfsm

Erlang gen_fsm transition to a new state


I have erlang gen_fsm, my first state:

begin({Nick}, _From, State) ->
            {reply, true, next_state, State}.

Then i have:

next_state(_Event, _From, State) -> 
        io:format("Test \n"),
        {reply, ok, begin, State}.

But i don't seen Test note in shell

How correctly transit to a new state?


Solution

  • First of all, ensure that begin is the actual initial state of your FSM. You specify the initial state of your FSM by returning, in your init function, something like:

    {ok, begin, State}
    

    Where begin is your initial state.

    Also, note that you're defining a Module:StateName/3 function, which will be called any time a gen_fsm:sync_send_event is performed on your FSM. If you're trying to send events to the FSM using gen_fsm:send_event, you should instead define a function Module:StateName/2, which is its asynchronous version.

    Finally, try to debug your modules by tracing them, rather than adding printouts. It's much simpler and it avoids you recompiling your code time after time.

    More information are avilable here.