Page 1 of 1
Require inside require isn't working!
Posted: Wed Aug 18, 2010 4:33 pm
by RawArkanis
Hai!
This is my first post here.
I'm brazillian and my english is a bit poor. Hope you can understand me. =)
I have a file (utils.lua) with a code for padded imagens that i have found here in the forum.
In another file I add utisl.lua with a require(). This second file (essentials.lua) is for put all common code of the game.
Code: Select all
require("lib/oo/middleclass")
require("lib/graphics/utils")
But adding essentials.lua in other files, like in my start.lua, the function newPaddedImage that is in utils.h can't be found. Generating this errors:
Error
src/states/start.lua:18: attempt to call global 'newPaddedImage' (a nil value)
Traceback
src/state/start.lua:18: in function 'initialize'
.\lib/oo/middleclass.lua:22: in function 'new'
main.lua:15: in function 'load'
[C]: in function 'xpcall'
If I put 'require("lib/graphics/utils")' in start.lua, its work.
How can i fix it?
Re: Require inside require isn't working!
Posted: Wed Aug 18, 2010 6:45 pm
by kikito
It sounds like you are using functions before requiring them.
I'm sorry if I'm not more specific; I can't without seeing your folder structure and requires.
Re: Require inside require isn't working!
Posted: Wed Aug 18, 2010 7:23 pm
by RawArkanis
kikito wrote:It sounds like you are using functions before requiring them.
I'm sorry if I'm not more specific; I can't without seeing your folder structure and requires.
Folder Structure:
Code: Select all
lib
--graphics
----utils.lua <-- where newPaddedImage code is in
--oo
----middleclass.lua
--states
----sm.lua
----state.lua
--essentials.lua <-- where utils.lua is required, and dont work
--game.lua
res <-- resource folder (images, etc)
src
--states
----start.lua <-- where i require essentials and newPaddedImage dont work. if i put require("lib/graphics/utils") here, works
main.lua
config.lua
utils.lua code
Code: Select all
function newPaddedImage(filename)
local source = love.image.newImageData(filename)
local w, h = source:getWidth(), source:getHeight()
-- Find closest power-of-two.
local wp = math.pow(2, math.ceil(math.log(w)/math.log(2)))
local hp = math.pow(2, math.ceil(math.log(h)/math.log(2)))
-- Only pad if needed:
if wp ~= w or hp ~= h then
local padded = love.image.newImageData(wp, hp)
padded:paste(source, 0, 0)
return love.graphics.newImage(padded)
end
return love.graphics.newImage(source)
end
essentials.lua code
Code: Select all
require("lib/oo/middleclass")
require("lib/graphics/utils")
start.lua code
Code: Select all
require("lib/essentials")
require("lib/states/state")
StartState = class("StartState", State)
function StartState:initialize ()
super.initialize(self)
Logo = newPaddedImage("res/img/logo.png")
alpha = 255
stick = 0
tick = 0
end
...
To work, i put require("lib/graphics/utils") in this last file.
Re: Require inside require isn't working!
Posted: Wed Aug 18, 2010 9:02 pm
by kikito
I don't see any obvious issue.
When I reach that point, I just try putting lots of prints. For example, try adding this to start.lua:
Code: Select all
print('requiring essentials')
require("lib/essentials")
print('requiring state')
require("lib/states/state")
And then on essentials.lua:
Code: Select all
-- at the top of the file
print('essentials.lua is being read')
print('requiring middleclass')
require("lib/oo/middleclass")
print('requiring utils')
require("lib/graphics/utils")
And so on with graphics/utils, etc. Activate the console and see if the calls are being made properly.