HUMP - yet another set of helpers

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by Robin »

Nooo!

FIFY:

Code: Select all

state.world:setCallbacks(
            function() state:addCollision() end,
            function() state:persistCollision() end,
            function() state:remCollision() end,
            function() state:resultCollision() end)
Help us help you: attach a .love.
User avatar
Elvenrix
Prole
Posts: 26
Joined: Sat Aug 28, 2010 6:52 pm

Re: HUMP - yet another set of helpers

Post by Elvenrix »

I couldn't figure this myself, so i need professional help.

i'll use several gamestates, and to keep it organized, i want to put the callbacks for each gamestate on a different lua file.
But when i do that, i get an error.
Lets go to some example code:

Code: Select all

titlescreen = Gamestate.new()
in_game = Gamestate.new()

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

function titlescreen.draw()
love.graphics.print("G A M E   S E L E C T", 300, 100)
end

function in_game.draw()
love.graphics.print("I N   G A M E", 300, 100)
end

function love.keypressed(key)
	if key == "1" then Gamestate.switch(titlescreen) end
	if key == "2" then Gamestate.switch(in_game) end
end
If i place the function titlescreen.draw() in a different file and require it, i get an error message.
So, is there a way to do it?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by Robin »

So how would the code look if you put it in another file and require it?
Help us help you: attach a .love.
User avatar
Elvenrix
Prole
Posts: 26
Joined: Sat Aug 28, 2010 6:52 pm

Re: HUMP - yet another set of helpers

Post by Elvenrix »

This is the main.lua

Code: Select all

require "gamestate.lua"
require "titlescreen.lua"

titlescreen = Gamestate.new()
in_game = Gamestate.new()

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

function in_game.draw()
love.graphics.print("I N   G A M E", 300, 100)
end

function love.keypressed(key)
   if key == "1" then Gamestate.switch(titlescreen) end
   if key == "2" then Gamestate.switch(in_game) end
end
And the titlescreen.lua

Code: Select all

function titlescreen.draw()
love.graphics.print("G A M E   S E L E C T", 300, 100)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by Robin »

Exactly what I thought. Let's walk through it to see why it won't work:

Code: Select all

main.lua:1: require "gamestate.lua" -- I have to load the file gamestate.lua
gamestate.lua:1: function titlescreen.draw() -- Which means:
titlescreen.draw = function() ... -- What the hell is titlescreen? It is nil, you can't index nil! Error!
The solution is to begin your main.lua thusly:

Code: Select all

titlescreen = Gamestate.new()
in_game = Gamestate.new()

require "gamestate.lua"
require "titlescreen.lua"
Help us help you: attach a .love.
User avatar
Elvenrix
Prole
Posts: 26
Joined: Sat Aug 28, 2010 6:52 pm

Re: HUMP - yet another set of helpers

Post by Elvenrix »

I tried that before posting here, but the error gets to another file instead :death:
"main.lua:1:attempt to index global 'Gamestate' (a nil value)"
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: HUMP - yet another set of helpers

Post by nevon »

Because I'm too lazy to read your code, I'm just going to show you an example of how it's done.

main.lua:

Code: Select all

require("gamestate") --this is the HUMP gamestate module

require("intro") -- This is our intro state
require("game") -- and our game state

function love.load()
    -- First we connect all the callbacks to the Gamestate module
    Gamestate.registerEvents()
    -- And then we switch to the intro state
    Gamestate.switch(Gamestate.intro)
end
intro.lua

Code: Select all

Gamestate.intro = Gamestate.new()
local state = Gamestate.intro

function state:update(dt)
    -- Here we can do stuff! Wee!
end

function state:draw()
    -- Let's draw something too!
end

function state:keypressed()
    -- If someone presses a button, let's go to the game
    Gamestate.switch(Gamestate.game)
end
game.lua

Code: Select all

Gamestate.game = Gamestate.new()
local state = Gamestate.game

function state:update(dt)
    -- It's all fun and games
end
User avatar
Elvenrix
Prole
Posts: 26
Joined: Sat Aug 28, 2010 6:52 pm

Re: HUMP - yet another set of helpers

Post by Elvenrix »

allright, so the solution is to move the Gamestate.new() lines out of the main.lua
its working perfectly (at least for now)
thanks
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by Robin »

Elvenrix wrote:I tried that before posting here, but the error gets to another file instead :death:
"main.lua:1:attempt to index global 'Gamestate' (a nil value)"
Err, right. My mistake, it's exactly what you did wrong, but for a different reason.

It's about dependency flow. game.lua depends on the code

Code: Select all

titlescreen = Gamestate.new()
in_game = Gamestate.new()
, which depends on gamestate.lua.

But nevon is smart, listen to him.
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: HUMP - yet another set of helpers

Post by TechnoCat »

I'm trying to use the Interpolater, but it keeps returning a boolean instead of a number.

Code: Select all

local fader = Interpolator(5, function(fraction) return fraction*255 end)
table.insert(squares,1,newSquare(function(self,dt) self.alpha = fader(dt) end))
learnHUMP.love
(12.43 KiB) Downloaded 329 times
EDIT: And shouldn't these be

Code: Select all

function Gamestate.update(self, dt)
instead of

Code: Select all

function Gamestate.update(dt)
Last edited by TechnoCat on Sat Jan 15, 2011 9:08 pm, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests