Page 1 of 3

How to build a level file format

Posted: Sat Nov 26, 2011 5:27 am
by Lord Uber Dowzen
Hi. I'm currently working on a tile engine for a game I'm making and I was hoping someone might be able to help me out with making a save system. I'm finding the documentation quite confusing and I've been unable to find any tutorials. Basically, at the moment all I want to do is dump the contents of my map table into a file when I press the "s" key and then retrieve it when I press "l". Would anyone be able to give me a few pointers on how to achieve this? Thanks!

Re: How to build a save system

Posted: Sat Nov 26, 2011 7:55 am
by coffee
Saving/Loading have more or less degrees of difficulty in LOVE. Save for ex. highscores is easy, saving all state of the game is complicated but loading/saving maps isn't so hard. As I can see from your code your haven't yet built an "internal" map nor you have a map file to load but ok.

You should first build your tile engine with an internal map and only after starting thinking loading/saving.

But here it's more or less code examples how in my project I load/save a map in a table like this one. So put your map like this one in a "map.lua" file:

Code: Select all

map = {	
{ 1, 1, 1, 4, 5, 5 },
{ 1, 6, 6, 7, 2, 2 },
{ 1, 0, 0, 7, 2, 2 },
}


1 - To load:
This will load the above file and "run"/put the table "map" in "memory".

Code: Select all

chunk = love.filesystem.load( "map.lua" )
local result = chunk()
2 - To save
It's more complicated because you have to string all your map before save it. Can be done more or less like this. Ignore my clumsy style and bad tab formatting please. My saving routine was more complex (multiple layers/tables to save) so I hope didn't wrong editing it to simplify. For sure can help you as reference and if there any mistake is easy to correct.

Code: Select all

local strg = "map = {\n" -- starting the string buffer

		for x = 1, #map do 
	     	strg = strg .. "\t{ "	     -- add to beginning of row the bracket
	           for y = 1, #map[1] do
	        	strg = strg .. tostring(map[x][y]) -- add the map value to string 
	        	if y < #map[1] then strg = strg .. ", " end -- add comma unless it's the last value of map row
	           end
    	       strg = strg .. " },\n" -- end of map row bracket
	     end
	strg = strg .. " \n}" -- end of map table bracket
	     
	file = love.filesystem.newFile( "map.lua" )
	file:open('w')    
 	file:write( strg ) -- write the final string to file
	file:close()

I hope it helps! :)

NOTE: Edited, changed variables a little more and with helping comments in code for understanding of what's is being done.

Re: How to build a save system

Posted: Sat Nov 26, 2011 8:50 pm
by Lord Uber Dowzen
Cool, I'll try and implement that now. Thanks!

Re: How to build a save system

Posted: Sat Nov 26, 2011 9:20 pm
by Lord Uber Dowzen
When you say:
coffee wrote:As I can see from your code your haven't yet built an "internal" map nor you have a map file to load but ok.

You should first build your tile engine with an internal map and only after starting thinking loading/saving.
do you mean have some code which fills in the map table with values? I ask because I've attempted to implement your code and I get this error:

attempt to index field '?' (a nil value)

with the culprit line being:

if map[x][y] == b then

Re: How to build a save system

Posted: Sat Nov 26, 2011 10:57 pm
by Taehl
One sub-problem you're likely to run into is that since you can only write strings to files, you'll need to turn a table into a string. That's called "serialization", and I've made a simple library called TSerial which should be adequate. It has the nice bonus of turning tables into Lua code, which makes various things easier.

Re: How to build a save system

Posted: Sun Nov 27, 2011 2:19 am
by Lord Uber Dowzen
Thanks Taehl, I'm sure that'll be useful when I get to saving my map. I'm having some issues implementing coffee's system though. At the moment I'm able to run a loop that fills the table in map.lua but I'm unable to do the same thing when I define the contents of the table myself in map.lua. Any ideas what I'm doing wrong?

Re: How to build a save system

Posted: Sun Nov 27, 2011 2:28 am
by Taehl
Chances are, the table you make isn't sequential-numeric-ly indexed, which would cause that numeric for loop in his code to give you only part (or possibly none) of your table. The fix, if my guess about your problem is correct, would be to replace the numeric for loops with generic pairs loops.

Re: How to build a save system

Posted: Sun Nov 27, 2011 3:10 am
by Lord Uber Dowzen
OK, I worked it out. Now I just need to work on saving...

Re: How to build a save system

Posted: Sun Nov 27, 2011 10:35 am
by coffee
Lord Uber Dowzen wrote:OK, I worked it out. Now I just need to work on saving...
You shouldn't have much trouble implement my very basic code to your game. It would help if you post your new code. I before tried to debug the first one but your love file simple opened and quit all the times.

As you already noticed one of the problems is that your engine hasn't functional because you hadn't or built a table map to display. Following your code you need to have a map/table like my one with size 60,40 (your mapWidth,mapHeight). You had there this piece of code

Code: Select all

 for x=0,mapWidth do
    map[x] = {}
    for y=0,mapHeight do
      map[x][y] = {}
    end
  end
that could fill all of your map but you actually weren't putting any value in the x,y coordinates. So, you only had 2 tiles for now (0 and 1) and you had to assign map[x][y] = 0 or map[x][y] = 1 to all map instead only map[x][y] = {} that don't assign any value there. Or probably will be easier for now hand code a table with the map.


Please post your new code because It will be hard from now on have to guess what is not working.

Re: How to build a save system

Posted: Tue Nov 29, 2011 12:30 am
by Lord Uber Dowzen
OK sorry for the delay, here's my latest build. Hope it helps!