Page 1 of 4

FSM - Finite State Machine

Posted: Fri Nov 18, 2011 2:34 pm
by Xmapa
hchello, i am new there and to the programing at all.
I'd like to start from gamedev and from lua and now from LOVE -_-
first wat i need is to make FSM code for my 1st game, kinda state for main menu, play|pause screens, and some UI.
I need any help.
For now i'm readyng wiki and looking the code of games. Maybe someone know there exactly i should look? any help will be usefull :emo:

Re: FSM - Finite State Machine

Posted: Fri Nov 18, 2011 3:18 pm
by GijsB
http://www.lua.org/pil/
for all your Lua ;D!

Re: FSM - Finite State Machine

Posted: Fri Nov 18, 2011 3:24 pm
by thelinx
vrld's HUMP has a gamestate module, maybe that is what you're looking for?

Re: FSM - Finite State Machine

Posted: Fri Nov 18, 2011 3:33 pm
by Taehl
Hmm... Maybe we should gather a list of Lua and Love documentation and tutorials and stuff, and make a stickied thread?

Re: FSM - Finite State Machine

Posted: Fri Nov 18, 2011 3:54 pm
by Ellohir
I am currently using boolean flags. For everything. Everytime I am going to draw I ask if it's the gamestart menu, the settings menu, the playing scene or whatever. And the same for the input. I should definately take a look at this FMS thing xD

Re: FSM - Finite State Machine

Posted: Sat Nov 19, 2011 7:16 am
by Dilli
For gamestate management I love Pölygamy :)

Re: FSM - Finite State Machine

Posted: Sat Nov 19, 2011 11:24 am
by coffee
Ellohir wrote:I am currently using boolean flags. For everything. Everytime I am going to draw I ask if it's the gamestart menu, the settings menu, the playing scene or whatever. And the same for the input. I should definately take a look at this FMS thing xD
I decided not use any 3rd party State Library since I found that this simple routine used for love.draw and love.update worked so well enough. Why complicate more? :)
(love draw variation here)

Code: Select all

	if(game_state == 'nullState') then drawNull()
	elseif(game_state == 'introState') then drawIntro()
	elseif(game_state == 'selectState') then drawSelect()  
	elseif(game_state == 'playState') then drawPlay()
	elseif(game_state == 'editState') then drawEdit()  
	elseif(game_state == 'deadState' or game_state == "goalState") then drawDebriefing()
	elseif(game_state == 'helpState') then drawHelp()		
	elseif(game_state == 'pauseState') then drawPause()	
	elseif(game_state == 'quitState') then drawQuit()	
	else error('game_state is not valid')
	end
Dilli wrote:For gamestate management I love Pölygamy :)
Did you worked in LUA variant? Last time I checked the Lua docs for Polygamy were rare (or non-existent)

Re: FSM - Finite State Machine

Posted: Sat Nov 19, 2011 1:58 pm
by Robin
coffee wrote:I decided not use any 3rd party State Library since I found that this simple routine used for love.draw and love.update worked so well enough. Why complicate more? :)
I'd use a table here, it's simpler to extend:

Code: Select all

game_states = {nullState = drawNull, introState = drawIntro, selectState = drawSelect, ... }

-- in love.draw
	game_states[game_state]()

Re: FSM - Finite State Machine

Posted: Sat Nov 19, 2011 6:39 pm
by coffee
Robin wrote:
coffee wrote:I decided not use any 3rd party State Library since I found that this simple routine used for love.draw and love.update worked so well enough. Why complicate more? :)
I'd use a table here, it's simpler to extend:

Code: Select all

game_states = {nullState = drawNull, introState = drawIntro, selectState = drawSelect, ... }

-- in love.draw
	game_states[game_state]()
Thank you for the clever suggestion Robin. It would be a pleasure implant that but this way rises a problem because it screws the update routine since that I can't redirect the game_state to both sides at same time (draw/update).

Code: Select all

function love.update()

	if(game_state == 'nullState') then updateNull()
	elseif(game_state == 'introState') then updateIntro()
	elseif(game_state == 'selectState') then updateSelect()  	
	elseif(game_state == 'playState') then updatePlay()  	
	elseif(game_state == 'editState') then updateEdit()  
	elseif(game_state == 'deadState' or game_state == 'goalState') then updateDebriefing()
	elseif(game_state == 'helpState') then updateHelp() 	
	elseif(game_state == 'pauseState') then updatePause() 
	elseif(game_state == 'quitState') then updateQuit() 
	else error('game_state is not valid')
	end -- end if --
			
end 

but perhaps I can do something close like this? (didn't tested yet for syntax validation sorry)

Code: Select all


game_states = {nullState = Null, introState = Intro, selectState = Select, ... }

-- in love.draw
        redirect_function = "draw"..game_state
	game_states[redirect_function]()

-- in love.update
        redirect_state = "update"..game_state
	game_states[redirect_function]()


Re: FSM - Finite State Machine

Posted: Sat Nov 19, 2011 6:55 pm
by TechnoCat
Seems like you are overcomplicating things. This is good news though. right?

Press space to change gamestate.

Code: Select all

local current_state
local game_states = {
    introState = {
        draw = function() love.graphics.print("INTRO!!",0,0) end,
        update = function(dt) end,
        keypressed = function(key) if key == " " then current_state = "selectState" end end },
    selectState = {
        draw = function() love.graphics.print("SELECT!",0,0) end,
        update = function(dt) end,
        keypressed = function(key) end}
  }

function love.load()
    current_state = "introState" --or 1,2,...,#game_states
end

function love.update(dt)
    game_states[current_state].update(dt)
end

function love.draw()
    game_states[current_state].draw()
end

function love.keypressed(key)
    game_states[current_state].keypressed(key)
end
Added a metatable default state to clean it up a bit. Only override the functions you want to this way.

Code: Select all

local current_state

local game_states = {}

local defaultState = {
	update = function(dt) end,
	draw = function() end,
	keypressed = function(key) end
}

local introState = {
	draw = function() love.graphics.print("INTRO!!",0,0) end,
	keypressed = function(key) if key == " " then current_state = "selectState" end end
}
setmetatable(introState, {__index = defaultState})
game_states["introState"] = introState

local selectState = {
	draw = function() love.graphics.print("SELECT!",0,0) end
}
setmetatable(selectState, {__index = defaultState})
game_states["selectState"] = selectState

function love.load()
   current_state = "introState" --or 1,2,...,#game_states
end

function love.update(dt)
  game_states[current_state].update(dt)
end

function love.draw()
   game_states[current_state].draw()
end

function love.keypressed(key)
   game_states[current_state].keypressed(key)
end