Page 1 of 1

How to use Hump Gamestates and modular files?

Posted: Wed May 20, 2015 8:18 pm
by hiccupface
Hi all. I'm using HUMP's gamestates library and my code is getting a little unruly in it's length. I have certain functions isolated in their own .lua files to keep things organized, which is a start, but I'm wondering how I can keep individual gamestates in their own files as well to keep things tidy and to save time hunting around my code. Fore example, how can I isolate menu and game into their own .lua files?

Code: Select all

Gamestate = require "gamestate"

local menu = {}
local game = {}

function love.load()
		Gamestate.registerEvents()
		Gamestate.switch(menu)
end

function menu:enter()
-- menu load out
end

function menu:update(dt)
-- many lines of code
end

function menu:draw()
-- many lines of code
end

function game:enter()
-- many lines of code
end

function game:update(dt)
-- many lines of code
end

function game:draw()
-- many lines of code
end


Re: How to use Hump Gamestates and modular files?

Posted: Wed May 20, 2015 8:28 pm
by s-ol
There is no specific way to do it, it's exactly the same as in every project:

main.lua

Code: Select all

Gamestate = require "gamestate"
local game = require"states.game"
local menu = require"states.menu"

function love.load()
      Gamestate.registerEvents()
      Gamestate.switch(menu)
end
game.lua

Code: Select all

local game = {}

function game:enter()
-- many lines of code
end

function game:update(dt)
-- many lines of code
end

function game:draw()
-- many lines of code
end

return game

Re: How to use Hump Gamestates and modular files?

Posted: Wed May 20, 2015 8:31 pm
by MadByte
You can take a look at this:
Module Definition

Good Luck.

Re: How to use Hump Gamestates and modular files?

Posted: Wed May 20, 2015 8:50 pm
by hiccupface
Weird - that's what I thought, but it does't seem to be working for me. My game loads fine, and after my first keypress which transitions me from menuscreen to instructions screen I get the error below. (fyi, I'm testing by creating instructions.lua as a separate file)

gamestate.lua:42: attempt to index local 'to' (a boolean value)

Traceback
gamestate.lua: 84 in function '__index'
gamestate.lua: 42 in function 'switch'
main.lua:111: in function <main.lua:109> <--- this is where I'm requesting the scene switch


I have instructions = require'instructions' in my main.lua file and all of my instructions functions (enter, update,draw) in instructions.lua, as well, local instructions = {} at the very top.

I could share my code, though it will probably burn your eyes, haha.

Re: How to use Hump Gamestates and modular files?

Posted: Wed May 20, 2015 9:27 pm
by hiccupface
Update! Resolved!

Once again foiled by my beginneriness. I forgot to add return state at the bottom of my individual files.

Time to clean up some code!