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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
aihe
Prole
Posts: 2
Joined: Fri Jan 20, 2017 6:05 pm

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

Post 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) !
Last edited by aihe on Fri Jan 20, 2017 11:13 pm, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: reading a file with love.filesystem.lines

Post by kikito »

The 'string.sub(line,j,j)' part seems to be wrong. Surely there must be an "i" somewhere on that expression?
When I write def I mean function.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: reading a file with love.filesystem.lines

Post 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.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: reading a file with love.filesystem.lines

Post 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
Last edited by zorg on Fri Jan 20, 2017 11:39 pm, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: reading a file with love.filesystem.lines

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
aihe
Prole
Posts: 2
Joined: Fri Jan 20, 2017 6:05 pm

Re: reading a file with love.filesystem.lines

Post by aihe »

Wow, thank you really much for your help guys ! ♥
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], Semrush [Bot] and 30 guests