Reading the lines out of a .txt File

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
pilleman
Prole
Posts: 5
Joined: Wed Jun 12, 2013 8:16 pm

Reading the lines out of a .txt File

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Reading the lines out of a .txt File

Post 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.
pilleman
Prole
Posts: 5
Joined: Wed Jun 12, 2013 8:16 pm

Re: Reading the lines out of a .txt File

Post by pilleman »

thanks for your great help, i solved the problem.
regards pilleman
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Reading the lines out of a .txt File

Post 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.
Post Reply

Who is online

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