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.
Separate state files
Re: Separate state files
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
Thanks for the answer. Can I create different files for each state by using hump?
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Separate state files
Sure you can.
Me and my stuff True 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.
Re: Separate state files
Any links to an example?
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Separate state files
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 True 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.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Separate state files
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Separate state files
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
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
}
Code: Select all
return {
draw = function()
-- draw game...
end,
update = function (dt)
-- update game...
end,
...
}
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Separate state files
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Separate state files
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.s-ol wrote:i have my own on github too but I think you really dont need it.
Who is online
Users browsing this forum: No registered users and 10 guests