How to load map from 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
Verfin
Prole
Posts: 2
Joined: Mon Feb 06, 2012 6:31 pm

How to load map from txt file

Post by Verfin »

I'm making this tilemap thingie when the problem struck me. I used this code to save the map

Code: Select all

for y = 1 , mapheight do
		file:write("\n")
	for x = 1, mapwidth do
		if x == 1 then
		file:write(map[x][y][0])
		else
		file:write("," .. map[x][y][0] )
		end
	end
end
and the text it generates looks like this: 0,0,1,4,2 etc...
So the problem is, how I can tell the program to read the items in the lines ie. read one item, store it to the map table read the next etc..
I'm so new to the lua :(
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to load map from txt file

Post by Jasoco »

If you save it with the correct format for tables in standard Lua syntax:

Code: Select all

map = {
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
You should be able to use:

Code: Select all

love.filesystem.load("map1.lua")()
To load it. The extra () at the end makes sure it's executed like Lua.

I think you can alternatively replace that map = with return and then just use

Code: Select all

map = love.filesystem.load("map1.lua")
Without the extra () at the end to return it as a table into a variable? Maybe? I haven't tested the second method.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: How to load map from txt file

Post by Ellohir »

Add a "return {" at the beggining and a "}" at the end. That way you can simply execute the file and get the matrix.

If you want to store it as a string I use this magic piece of code that I don't understand from kikito ( https://github.com/kikito/love-tile-tutorial/wiki ):

Code: Select all

    tileTable = {}
    local width = #(tileString:match("[^\n]+")) 
    for x = 1,width,1 do tileTable[x] = {} end

    local rowIndex,columnIndex = 1,1
    for row in tileString:gmatch("[^\n]+") do
      assert(#row == width, 'Map is not aligned: width of row ' .. tostring(rowIndex) .. ' should be ' .. tostring(tiles.w) .. ', but it is ' .. tostring(#row))
      columnIndex = 1
          for character in row:gmatch(".") do
              tiles.tileTable[columnIndex][rowIndex] = character
              columnIndex = columnIndex + 1
          end
       rowIndex=rowIndex+1
    end
It reads a string 'tileString' and makes it into a matrix 'tileTable'. It doesn't use commas, though.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How to load map from txt file

Post by coffee »

Good help already but also my complete load/save routine could help

viewtopic.php?f=4&t=4582#p41171
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How to load map from txt file

Post by Taehl »

Or use TSerial, or LON / JSON... This kind of problem is called serialization, and you will find no shortage of solutions for it.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Verfin
Prole
Posts: 2
Joined: Mon Feb 06, 2012 6:31 pm

Re: How to load map from txt file

Post by Verfin »

Thank you for these replies! Guess I just had wrong keywords when I tried to search.
waraiotoko
Prole
Posts: 33
Joined: Thu Oct 06, 2011 6:08 pm

Re: How to load map from txt file

Post by waraiotoko »

You could save everything to a .lua file and just load it with the built in Lua functions.

EDIT: Nevermind, didn't read enough replies. TSerial seems like what you need.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests