How to use Hump Gamestates and modular files?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

How to use Hump Gamestates and modular files?

Post 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

Last edited by hiccupface on Wed May 20, 2015 8:37 pm, edited 1 time in total.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How to use Hump Gamestates and modular files?

Post 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

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
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: How to use Hump Gamestates and modular files?

Post by MadByte »

You can take a look at this:
Module Definition

Good Luck.
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: How to use Hump Gamestates and modular files?

Post 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.
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: How to use Hump Gamestates and modular files?

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

Who is online

Users browsing this forum: Ahrefs [Bot] and 196 guests