Page 1 of 2

Separate state files

Posted: Tue Feb 21, 2017 7:00 pm
by erol
Is it possible to create game projects with separate files for each states with each having their own callback functions like phaser game engine?

Thanks.

Re: Separate state files

Posted: Tue Feb 21, 2017 8:29 pm
by joedono
Not familiar with Phaser, but this sounds like what you're looking for: http://hump.readthedocs.io/en/latest/gamestate.html

Re: Separate state files

Posted: Tue Feb 21, 2017 8:51 pm
by erol
Thanks for the answer. Can I create different files for each state by using hump?

Re: Separate state files

Posted: Tue Feb 21, 2017 9:05 pm
by zorg
Sure you can.

Re: Separate state files

Posted: Tue Feb 21, 2017 9:07 pm
by erol
Any links to an example?

Re: Separate state files

Posted: Tue Feb 21, 2017 10:15 pm
by zorg
Let me just modify the first example on the page linked above for you:

Code: Select all

-- game.lua

local Gamestate = require "hump.gamestate"

local game = {}

function game:enter()
    Entities.clear()
    -- setup entities here
end

function game:update(dt)
    Entities.update(dt)
end

function game:draw()
    Entities.draw()
end

return game

-----------------------------------------------------------------

-- menu.lua

local Gamestate = require "hump.gamestate"

local game

local menu = {}

function menu:init()
    game = require "game"
end

function menu:draw()
    love.graphics.print("Press Enter to continue", 10, 10)
end

function menu:keyreleased(key, code)
    if key == 'return' then
        Gamestate.switch(game)
    end
end

return menu

-----------------------------------------------------------------

-- main.lua

local Gamestate = require "hump.gamestate"

local menu, game

function love.load()
    menu = require "menu"
    game = require "game" -- This is only needed because of the registerEvents call below; if you're doing stuff manually, then this can be avoided here.
    Gamestate.registerEvents()
    Gamestate.switch(menu)
end

Re: Separate state files

Posted: Wed Feb 22, 2017 12:22 am
by Positive07
I should recommend ScreenManager

Re: Separate state files

Posted: Wed Feb 22, 2017 1:18 am
by s-ol
Positive07 wrote: Wed Feb 22, 2017 12:22 am I should recommend ScreenManager
link is broken, it's supposed to be: https://github.com/rm-code/screenmanager

also, you can use any of the many state managing libraries, but you really dont even need to (i have my own on github too but I think you really dont need it).
What zorg did for hump is basically how you do it, but a very simple solution for doing it without would be this:

main.lua:

Code: Select all

STATE = require "menu" -- start with this state

function load_state(new_state)
  if STATE.exit then STATE.exit() end
  STATE = new_state
  if STATE.enter then STATE.enter() end
end

function love.draw()
  if STATE.draw then STATE.draw() end
end

function love.update(dt)
  if STATE.update then STATE.update(dt) end
end

function love.keypressed(...)
  if STATE.keypressed then STATE.keypressed(...) end
end

-- etc
menu.lua

Code: Select all

local selected_menu_entry
menu_entries = { "start", "leave" }

return {
  enter = function ()
    selected_menu_entry = 0
  end, 
  keypressed = function (key)
    if key == "up" then
      selected_menu_entry = selected_menu_entry - 1
    elseif key == "down"
      selected_menu_entry = selected_menu_entry + 1
    else
      if menu_entries[selected_menu_entry] == "start" then
        load_state(require "game")
      end
    end
  end,
  draw = function ()
    love.graphics.print(menu_entries[selected_menu_entry])
  end
}
game.lua:

Code: Select all

return {
  draw = function()
    -- draw game...
  end,
  update = function (dt)
    -- update game...
  end,
  ...
}

Re: Separate state files

Posted: Wed Feb 22, 2017 2:56 am
by Positive07
The main benefit of ScreenManager and Hump states or other state systems is the stack and how you can easily push, pop and switch, having more than one active state and so on... But yeah implementing your own should be fairly simple

Re: Separate state files

Posted: Wed Feb 22, 2017 8:33 am
by airstruck
s-ol wrote:i have my own on github too but I think you really dont need it.
The thing I like about your solution is the ability to propagate some callbacks upward (drawing) and others downward (input). I'm not sure how the others handle that. If you don't have a lot of layers of stuff, though, maybe just a floating pause menu or something, you don't really even need a stack, you can just have your pause state hold a reference to the game state and have it draw your game state before it draws itself. If you don't need a stack, you probably don't really need a library either. Should be easy enough to have something like "game.activeState" and set that to whatever state instance you want.