Page 4 of 15
Re: HUMP - yet another set of helpers
Posted: Tue Nov 16, 2010 8:54 pm
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)
Re: HUMP - yet another set of helpers
Posted: Fri Nov 26, 2010 10:43 pm
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?
Re: HUMP - yet another set of helpers
Posted: Fri Nov 26, 2010 10:56 pm
by Robin
So how would the code look if you put it in another file and require it?
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 12:38 am
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
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 12:00 pm
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"
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 7:38 pm
by Elvenrix
I tried that before posting here, but the error gets to another file instead
"main.lua:1:attempt to index global 'Gamestate' (a nil value)"
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 9:13 pm
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
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 9:43 pm
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
Re: HUMP - yet another set of helpers
Posted: Sat Nov 27, 2010 9:43 pm
by Robin
Elvenrix wrote:I tried that before posting here, but the error gets to another file instead
"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.
Re: HUMP - yet another set of helpers
Posted: Sat Jan 15, 2011 8:38 pm
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))
EDIT: And shouldn't
these be
Code: Select all
function Gamestate.update(self, dt)
instead of