Reputation: 41
I am making a platformer game in Love2D, and I created my own lua script that processes level files.
I used dofile
in levelprocessor.lua
and in main.lua
, i require()
it and the module sucessfully loads.
However, when I try to process the level (level1.lua
), it says the file isn't found. But the file exists in the same directory as levelprocessor.lua
and main.lua
.
Here is my code:
levelprocesser.lua:
local blocks = {
["1"]=(love.graphics.newImage("blocks/grassdirt/grass.png")) -- singular piece of grass
}
local mod={
}
function mod.placeBlock(blockID, posx, posy)
position = {x=posx*32,y=posy*32}; --blocks are 16x16 and drawn 32x32, and x and y here are in block coordinates,
-- so a block with ID 1, posx 1, and posy=1, would be a grass block 32 pixels x and 32 pixels y.
for key, value in ipairs(blocks) do
-- glad roblox and that book called Beginning Lua Programming made me understand generic for loops
if key == blockID then
love.graphics.draw(key, position.x, position.y, 2,2);
else
error("Error in processing level file")
end
end
end
function mod.processFile(fileDir)
assert(type(fileDir) == "string", "error in function processFile in file levelprocessor.lua: argument 1 must be ")
local level = dofile(fileDir);
for i,v in ipairs(level) do
placeBlock(v.blockID, v.posx, v.posy)
end
end
return mod
main.lua:
levelprocessor = require "levelprocessor" -- remember kids, do require without file extension
function love.load()
healthbar = love.graphics.newImage("ui/Untitled.png")
good_health = love.graphics.newQuad(0, 0, 100, 36, healthbar:getDimensions())
caution_health = love.graphics.newQuad(100,36,102, 36, healthbar:getDimensions())
warning_health = love.graphics.newQuad(102,36,204,36,healthbar:getDimensions())
grass_top_singular = love.graphics.newImage("blocks/grassdirt/grass.png")
end
function love.draw()
love.graphics.draw(grass_top_singular, 0,568,0,2,2); -- 600-32=558... mental math, so im not sure if it is truly correct
levelprocessor.processFile("level1.lua")
end
The error itself is:
cannot open level1.lua: No such file or directory
Traceback
[C]: in function 'dofile'
levelprocessor.lua:22: in function 'processFile'
main.lua:12 in function 'draw'
[C]: in function 'xpcall'
Upvotes: 1
Views: 1962
Reputation: 4264
Replace
dofile( f )
by
assert( love.filesystem.load( f ) )( )
love.filesystem.
* will look in the source directory (and a few other places), not in the working directory.
Upvotes: 2