Text File Help?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

Text File Help?

Post by bbdude95 »

So I know how to read a text file but if I wanted to do a tile based game how would I go about it?
if I had a file like this:
1,1,1,1,1
1,2,2,2,1
1,2,3,2,1
1,2,2,2,1
1,1,1,1,1
How could I get the the 2nd line down and 3rd row over?
User avatar
BulbaMander
Citizen
Posts: 65
Joined: Sat Dec 15, 2012 7:00 pm

Re: Text File Help?

Post by BulbaMander »

I dont know why you wouldn't just put the map in an array... You could do

Code: Select all

map={ -- I think it;s called a two dimensional array.
{1,1,1,1},
{1,0,0,1},
{1,0,0,1},
{1,1,1,1}
}
and then loop through the tables with two for loops.

Code: Select all

for i=1,#map do -- loop through the rows 
  for ii=1,#map[i] do -- loop through the values in each row 
    --draw a square or whatever, 
    if map[i][ii] == 0 then -- check the tile you are on to see if its a 0
      love.graphics.rectangle("fill",(ii-1)*32,(i-1)*32,32,32)
    end
  end
end
I've been thinking about it, and It really doesnt make a ton of sense to me why you wouldnt use tables and use a textfile. I mean if I HAD to use a text file I would read the lines into a table, the only other way I can think of is using strings, and formating them but thats way more complicated than tables.
It takes an idiot to do cool things. Thats why they're cool. :emo:
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

Re: Text File Help?

Post by bbdude95 »

I wanted to use a text file that way people can make their own maps will a program I wrote.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Text File Help?

Post by Robin »

You could make that program produce Lua code as output. That way you can simply load the level with love.filesystem.load()().

The alternative is writing code that parses your text file. It's not very difficult, but the simpler and easier solution is the first one I mentioned.
Help us help you: attach a .love.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Text File Help?

Post by Germanunkol »

Robin wrote: The alternative is writing code that parses your text file. It's not very difficult, but the simpler and easier solution is the first one I mentioned.
I agree of course that getting it to work is easier, but making it work right (i.e. protecting the player from faulty code, for example) is much, much harder.

bbdude, this code should do what you want:

Code: Select all

function loadTableFromFile( filename )
	tbl = {}

	width = 0
	height = 0
	for line in io.lines(filename) do

		-- make sure line ends with comma:
		if line:sub(#line, #line) ~= "," then
			line = line .. ","
		end

		-- remove all whitespace:
		line = string.gsub(line, " ", "")
		line = string.gsub(line, "\t", "")

		height = height+1
		tbl[height] = {}	-- create a table for each line
		for field in string.gmatch(line, "[^,]*,") do
			-- remove comma from end:
			field = field:sub(1, #field-1)

			-- add at the end of tbl[i]:
			tbl[height][#tbl[height]+1] = field
			if #tbl[height] > width then
				width = #tbl[height]
			end
		end
	end

	return tbl, height, width
end

And here's an example usage:

Code: Select all

function love.load()
	t,h,w = loadTableFromFile( "table.txt" )
	print("h,w: ", h, w)

	-- print value at (2,3)
	print(t[3][2])

	-- print whole line:
	for k,v in ipairs(t) do
		line = ""
		for j,v2 in ipairs(v) do
			line = line .. v2 .. "\t"
		end
		print(line)
	end
end
Note: the function will remove any whitespace from your file. I think this can be useful for formatting the level in a more readable way in case you need more than numbers as fields, and some field entries are longer than the other -> in this case, just add spaces to the shorter field names.

Also, values should be seperated by commas. It works with the example file you posted.
BUT you need to be careful: after using this function, your table will be indexed like this:
t[y][x]
to get the value at coordinates (x,y). This is a little counter-intuitive, but if you just switch the x and y values, you should be fine.
It can be changed, of course, to work like:
t[x][y]
If you need it this way, try it yourself and if you get stuck just ask us.

One thing this code does NOT make sure is that the lines are all the same length, i.e. the first line could have 4 elements and the second one three. If you need this to be true, you can add a check to the end of the function: just check if the length of each line #tbl[y] is the same as width, otherwise give out an error.

Hope this helps, happy coding!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
spectralcanine
Citizen
Posts: 65
Joined: Sat Dec 22, 2012 8:17 am

Re: Text File Help?

Post by spectralcanine »

Using JSON will remove the security issues.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Text File Help?

Post by Robin »

spectralcanine wrote:Using JSON will remove the security issues.
I'm sorry, no, not in this case. Anyone who has enough access to the LÖVE save directory to craft malicious maps, also has enough access to replace the game itself. (Unless you only set the identity in main.lua, and don't require or load any module.) And besides, anyone with that access kind of owns you already anyway.

The exception is when you expect people share maps online. But then you can just make a nice little sandbox for those maps (sandboxes are almost trivial in Lua). Sure, malicious maps can fill up your RAM and slow down your CPU and the like, but nothing more dangerous if you don't let them. A having to have a sandbox is a small price to pay for the flexibility Lua offers.
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Text File Help?

Post by raidho36 »

Actually, those kind of hacked maps can ruin your filesystem or otherwisely maliciously infest your computer. This is why instead of loading auto-executed code, you create a loader function to handle specifically formatted files, especially if it wouldn't contain functions to load. But if it's absolutely must-have, then yeah, sandboxing all the way.
spectralcanine
Citizen
Posts: 65
Joined: Sat Dec 22, 2012 8:17 am

Re: Text File Help?

Post by spectralcanine »

I don't expect you to download the game from random sources. Getting maps from other people, however, is quite common and popular. In this case, JSON solves the security issues by simply not allowing (by definition) for any code, just data.
Of course, if your maps need code, sand boxing it is.
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

Re: Text File Help?

Post by bbdude95 »

Okay so its telling me that my "map.txt" file is not in the directory when it actually is. Any other reason it would be saying this?
The file is in both my project directory and my users roaming directory.
Makes me feel kinda stupid.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 63 guests