1

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'
3
  • 1
    require searches in configured paths. dofile doesn't. dofile uses the processes working directory. Commented Dec 3, 2015 at 20:04
  • Can't reproduce this. Minimal code I tried: In main.lua: function love.load() dofile "foo.lua" dofile "foo/bar.lua" end; in foo.lua: print "foo works"; in foo/bar.lua: print "foo/bar works"; running this will print "foo works" and "foobar works". What version of love2d are you on? Commented Dec 3, 2015 at 20:07
  • Ah, yes – running from a different directory means it fails. Commented Dec 3, 2015 at 20:10

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.