Search code examples
lualove2d

"function arguments expected near 'levelc'" when using LOVE


I'm currently trying to make a level loading system for a game.

function love.filedropped(file)
ofile=io.open(file:getFilename(),"r")
io.input(ofile)
file:close
levelc=io.read()
for i=1,levelc do
levels[i]=io.read()
print levels[i]
end

levelc should be the first line of the file, and file:getFilename is the file to open (path included) the project gives an error message on startup, and i've used a similar structure before, but for an output. The error is at line 30, which is the levelc=io.read(). I've tried changing the name of the file pointer (it was "f" before, now "ofile") and i've tried using io.read("*l") instead of io.read() but same result. EDITS: -this is a love.filedropped(file) -i need to open other files from a .txt later and i don't really understand how do do that


Solution

  • The parameter given by love.filedropped is a DroppedFile.

    In your case helpful could be File:lines().

    For example:

    function love.filedropped(file)
        -- Open for reading
        file:open("r")
        
        -- Iterate over the lines
        local i = 0
        for line in file:lines() do
            i = i + 1
            levels[i] = line
            print(i, levels[i]) -- Notice the parentheses missing in your code
        end
        
        -- Close the file
        file:close()
    end
    

    Notice that love2d usually only allows reading/writing files within the save or working directory. Dropped files are an exception.

    Unrelated to this answer but things I noticed in your code:

    • Use locals, oFile should be local
    • file:close() required parentheses as its a function call
    • Same for the print
    • The filedropped callback has no end

    You mentioned reading other files too, to do so, you can either: