Save a map

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
Manyrio
Prole
Posts: 29
Joined: Sat Feb 06, 2016 10:12 am

Save a map

Post 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 :(
function love.load() end
function love.update(dt) end
function love.draw() end
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Re: Save a map

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Save a map

Post 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
Help us help you: attach a .love.
Manyrio
Prole
Posts: 29
Joined: Sat Feb 06, 2016 10:12 am

Re: Save a map

Post by Manyrio »

Thank you I am testing this lib but is there a tutorial for bister ? I didn't find one ...
function love.load() end
function love.update(dt) end
function love.draw() end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Save a map

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

Re: Save a map

Post 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
Manyrio
Prole
Posts: 29
Joined: Sat Feb 06, 2016 10:12 am

Re: Save a map

Post by Manyrio »

Thank you it's work :D
function love.load() end
function love.update(dt) end
function love.draw() end
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 14 guests