Separate state files

General discussion about LÖVE, Lua, game development, puns, and unicorns.
erol
Prole
Posts: 3
Joined: Tue Feb 21, 2017 6:52 pm

Separate state files

Post 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.
User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Separate state files

Post by joedono »

Not familiar with Phaser, but this sounds like what you're looking for: http://hump.readthedocs.io/en/latest/gamestate.html
erol
Prole
Posts: 3
Joined: Tue Feb 21, 2017 6:52 pm

Re: Separate state files

Post by erol »

Thanks for the answer. Can I create different files for each state by using hump?
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Separate state files

Post by zorg »

Sure you can.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
erol
Prole
Posts: 3
Joined: Tue Feb 21, 2017 6:52 pm

Re: Separate state files

Post by erol »

Any links to an example?
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Separate state files

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Separate state files

Post by Positive07 »

I should recommend ScreenManager
Last edited by Positive07 on Wed Feb 22, 2017 2:43 am, edited 1 time in total.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Separate state files

Post 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,
  ...
}

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Separate state files

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Separate state files

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

Who is online

Users browsing this forum: No registered users and 17 guests