FSM - Finite State Machine

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Xmapa
Prole
Posts: 2
Joined: Fri Nov 18, 2011 2:27 pm

FSM - Finite State Machine

Post 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:
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: FSM - Finite State Machine

Post by GijsB »

http://www.lua.org/pil/
for all your Lua ;D!
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: FSM - Finite State Machine

Post by thelinx »

vrld's HUMP has a gamestate module, maybe that is what you're looking for?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: FSM - Finite State Machine

Post by Taehl »

Hmm... Maybe we should gather a list of Lua and Love documentation and tutorials and stuff, and make a stickied thread?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: FSM - Finite State Machine

Post 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
User avatar
Dilli
Prole
Posts: 5
Joined: Sat Oct 15, 2011 10:11 am

Re: FSM - Finite State Machine

Post by Dilli »

For gamestate management I love Pölygamy :)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: FSM - Finite State Machine

Post 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)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: FSM - Finite State Machine

Post 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]()
Help us help you: attach a .love.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: FSM - Finite State Machine

Post 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]()

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: FSM - Finite State Machine

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest