Finite State Machine

Can you post your example games with FSM. Thanks. Dont direct me to tutorial website I have read it but its better to see multiple examples

@GriffinC Don’t know if you’re still looking for an example, but here’s a very simplistic one. How you actually do this depends on the program you write because there’s no “one way fits all” way of doing it.

EDIT: Normally you would set state to whatever you want the next state to be, but here I set the variable nextState and used touched just for the example.


--# Main

function setup()
    state=menu
end

function draw()
    background(40, 40, 50)
    fill(255)
    state()
end

function menu()
    text("menu page",WIDTH/2,HEIGHT/2)
    text("do whatever you want in the menu",WIDTH/2,300)
    text("tap screen",WIDTH/2,200)
    nextState=state1
end

function state1()
    text("state page 1",WIDTH/2,HEIGHT/2)
    text("do whatever you want in state 1",WIDTH/2,300)
    text("tap screen",WIDTH/2,200)
    nextState=state2
end

function state2()
    text("state page 2",WIDTH/2,HEIGHT/2)
    text("do whatever you want in state 2",WIDTH/2,300)
    text("tap screen",WIDTH/2,200)
    nextState=menu
end

function touched(t)
    if t.state==BEGAN then
        state=nextState
    end
end

@juce I included the tab identifier just to see how it goes.

@dave1707,
Worked for me! :slight_smile:
Thanks.