Page 1 of 1

Save a map

Posted: Sun Feb 28, 2016 11:13 pm
by Manyrio
Hey ! I am working on a game and I want to save my map but I don't know how ...
here's how I create my map and draw it

Code: Select all

function love.load()
	map = {}
	map.width = 10
	map.height = 10
	for x = 0, map.width do
		map[x] = {}
		for y = 0, map.height do
			if y < 5 then
				map[x][y] = 1
			elseif y == 5 then
				map[x][y] = 3
			elseif y == 5+1 then
				map[x][y] = 3
			elseif y == 5+2 then
				map[x][y] = math.random(2,3)
			else
				map[x][y] = 2
			end
		end
	end
end
function love.draw()
	love.graphics.setColor(255,255,255,255)
	for x = 0, map.width do
		for y = 0, map.height do
			if map[x][y] == 3 then
				love.graphics.rectangle("line",x*32,y*32,32,32)
			elseif map[x][y] == 2 then
				love.graphics.rectangle("fill",x*32,y*32,32,32)
			end
		end
	end
end
function mapCollide(x, y)
	local checkMap = map[math.floor(x/32)][math.floor(y/32)]
	return checkMap
end
function love.update(dt)
	local mapX = math.floor(love.mouse.getX()/32)
	local mapY = math.floor(love.mouse.getY()/32)
	if love.mouse.isDown(1) and mapCollide(love.mouse.getX(), love.mouse.getY()) == 1 then
		map[mapX][mapY] = 2
	end
	if love.mouse.isDown(2) and mapCollide(love.mouse.getX(), love.mouse.getY()) < 5 then
		map[mapX][mapY] = 1
	end
end
I made this with LÖVE 0.10.1. I tried to save my map with filesystem but I failed
Please help :(

Re: Save a map

Posted: Mon Feb 29, 2016 3:20 am
by Sosolol261
You can either translate the table to a string and then store it onto a file and read it afterward, or you can use a lib. Not sure what the name was but there was a library that stores tables onto files for you.

Re: Save a map

Posted: Mon Feb 29, 2016 6:45 am
by Robin
You could use bitser for example, like this:

Code: Select all

local bitser = require "bitser"
...

function SaveGame(filename)
    bitser.dumpLoveFile(filename, map)
end

function LoadGame(filename)
    map  = bitser.loadLoveFile(filename)
end

Re: Save a map

Posted: Mon Feb 29, 2016 9:10 am
by Manyrio
Thank you I am testing this lib but is there a tutorial for bister ? I didn't find one ...

Re: Save a map

Posted: Mon Feb 29, 2016 10:24 am
by Robin
There isn't really a tutorial yet, but I don't know if it needs one. The snippet I gave above really has all you need.

Re: Save a map

Posted: Mon Feb 29, 2016 10:45 am
by ivan
While binser and ser are great libraries,
with maps I think it's more useful to save the table as a string/txt,
so you can inspect the result visually.
The obvious limitation is that
you have to use single characters for
each map value (instead of numbers).
If you insist on using numbers,
you can convert using string.byte(c)/string.char(n)

Something like:

Code: Select all

-- dump map to string
-- considerably less convenient since you have map[x][y] instead of map[y][x]
function map2string(map)
  local sz = ''
  for y = 1, h do
    local row = {}
    for x = 1, w do
      local c = map[x][y]
      c = (c ~= nil) and c or ' ' -- optional: replace nil with space
      if not tostring(c):len() == 1 then
        error("non-ANSI character on:" .. x .. ',' .. y)
      end
      row[x] = c
    end
    sz = sz .. table.concat(row) .. '\n'
  end
  return sz
end
Load map from string:

Code: Select all

function string2map(sz)
  local map = {}
  local x, y = 1, 1
  for l in string.gmatch(sz, "(.-)\n") do
    -- for each character
    map[x] = map[x] or {}
    for c in string.gmatch(l, ".") do
      map[x][y] = c
      x = x + 1
    end
    x, y = 1, y + 1
  end
  return map
end

Re: Save a map

Posted: Mon Feb 29, 2016 12:54 pm
by Manyrio
Thank you it's work :D