Page 1 of 1

(SOLVED) reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 6:17 pm
by aihe
Hello !

So I'm new with LÖVE and I'm trying to make a simple game first.

So I have a file that contains the informations about a map, like this :

Code: Select all

001000000010
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
001000000000
And I'm trying to put the numbers in a matrix by reading the file with love.filesystem.lines() :

Code: Select all

    map = {}
    for i=1,12 do
        map[i] = {}
        for j=1,12 do
            for line in love.filesystem.lines("maps/salon.map") do
                table.insert(map[i], string.sub(line,j,j))
            end
        end
    end
But when I try to print the matrix I have this instead of the thing above :

Image

The print function looks like this :

Code: Select all

    x = 30
    y = 30
    for i=1,12 do
        for j=1,12 do
            love.graphics.print( {{255, 255, 255}, map[i][j]}, x,y)
            x = x + 15
        end
        y = y + 15
        x = 30
    end
Thanks for your help (and sorry for my english) !

Re: reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 7:02 pm
by kikito
The 'string.sub(line,j,j)' part seems to be wrong. Surely there must be an "i" somewhere on that expression?

Re: reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 7:17 pm
by Jasoco
kikito wrote:The 'string.sub(line,j,j)' part seems to be wrong. Surely there must be an "i" somewhere on that expression?
I don't think so. The "i" part would be implied from the "line" string. Since "lines" is the string of the current line being read.

I think some of the fors might just be out of order.

Switch the "for j" line and the "for line" line.

Edit: Well, that solves part of it. lol

Here's your fixed code. I dumped the map lines into their own table before running over them:

Code: Select all

function love.load()
	local lines = {}
    for line in love.filesystem.lines("map.map") do
    	lines[#lines+1] = line
    end

    map = {}
    for i=1,12 do
        map[i] = {}
        for j=1,12 do
            table.insert(map[i], string.sub(lines[i],j,j))
        end
    end
end

function love.update(dt)

end

function love.draw()
    x = 30
    y = 30
    for i=1,12 do
        for j=1,12 do
            love.graphics.print( {{255, 255, 255}, map[i][j]}, x,y)
            x = x + 15
        end
        y = y + 15
        x = 30
    end
end
It can be done more compact I'm sure. But this works.

Edit AGAIN!: Here's a better version, replace the whole love.load function code with this:

Code: Select all

function love.load()
    map = {}
    local i = 1
    for line in love.filesystem.lines("map.map") do
        map[i] = {}
        for j=1,12 do
            table.insert(map[i], string.sub(line,j,j))
        end
        i = i + 1
    end
end
It gets rid of the "for i" part all together and replaces it with the "for line" part and just increment the "i" manually. This works and takes less code.

Re: reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 8:21 pm
by zorg
The above code can still be made even more generic, though; by counting how many characters the line has (don't remember but one might need to chop off whatever carriage return/line break combo there might be on the end, since iirc it's also included within the lines.)
Edit: yes, that's what i meant :P

Re: reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 9:09 pm
by Positive07
Improvement onJasoco's last code snippet, this one doesn't have fixed line length of 12 instead it reads as many characters as there are in the line. I think this is what zorg said. Also it would drop any character that is not a number so carriage returns for example would be ignored. You would be limitted to numbers from 0 to 9 though, if you need to allow more characters then define a different "valid" function, a regex that checks if the character is a carriage return should be good enough most of the time

Code: Select all

local map, valid = {}, tonumber
for line in love.filesystem.lines("maps/salon.map") do
   local row = {}
   for j=1, #line do
      table.insert(row, valid(line:sub(j,j)))
   end
   table.insert(map, row)
end

Re: reading a file with love.filesystem.lines

Posted: Fri Jan 20, 2017 10:36 pm
by aihe
Wow, thank you really much for your help guys ! ♥