Page 1 of 1

Reading the lines out of a .txt File

Posted: Wed Jun 12, 2013 8:25 pm
by pilleman
Hello everybody,

i'm trying to read seperated lines out of a txt-file, to insert them as a map into my main.lua

The txt-file looks like this:

Code: Select all

0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0
0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0
0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0
And my loop like this:

Code: Select all

main = {}


function love.load()
	local map = {}
	for line in love.filesystem.lines("test.txt") do
		table.insert(map, line)
		end
	end
end

function love.draw()
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
            	love.graphics.setColor(79,78,20)
                love.graphics.rectangle("fill", x * scale, y * scale, scale, scale)
            else 
            	love.graphics.setColor(245,243,135)
            	love.graphics.rectangle("fill", x * scale+1, y * scale+1, scale-2, scale-2)
            end
        end
    end
end
So is there anybody, who could help me, creating the map with the content from the txt file?
It would be perfect, if i could get rid of the line breaks as well, because they would disturb the structure of the map, i am trying to create with the txt file.


Thanks in advance to everybody.

Re: Reading the lines out of a .txt File

Posted: Wed Jun 12, 2013 9:47 pm
by ivan
I can think of two different approaches.
You can save the map as a lua table and just load it using "require":

Code: Select all

local t = {
  { 0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0 },
  { 0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0 },
  { 0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0 }
}
return t
Another approach is to save the map as a .txt with characters for each tile.
A while ago I wrote a short tutorial about this method.
The idea is to load the map as a string and iterate over each character.
A third method is to save your map as an image and read it pixel by pixel.
The benefit to this is that you can edit your maps using an image editor.

Notice that in your code, love.filesystem.lines returns a string.
You also need to break the line strings horizontally into columns.

Re: Reading the lines out of a .txt File

Posted: Thu Jun 13, 2013 12:44 pm
by pilleman
thanks for your great help, i solved the problem.
regards pilleman

Re: Reading the lines out of a .txt File

Posted: Thu Jun 13, 2013 1:08 pm
by T-Bone
I think the easiest approach is probably to use string:gmatch, which gives an iterator over for example all numbers in a string. I have levels stored in similar .txt-files in one of my projects and I interpret them using

Code: Select all

	local cw = {}
	
	local i = 1
	for line in love.filesystem.lines("world/" .. filename) do
		local vec = {}
		for num in line:gmatch("%d+") do
			vec[#vec + 1] = tonumber(num)
		end
		cw[i] = vec
		i = i + 1
	end
The end result is a matrix-style table called cw (for "current world" in case anybody wonders).

Ivan's solution works very well if you're writing the text files by hand. But if you want to automate it, my solution may be easier. It's really a matter of preference.