How to build a level file format

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.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

How to build a level file format

Post 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!
Attachments
the awesome tile system.love
(1.47 KiB) Downloaded 276 times
Last edited by Lord Uber Dowzen on Wed Nov 30, 2011 12:56 am, edited 1 time in total.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How to build a save system

Post 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.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: How to build a save system

Post by Lord Uber Dowzen »

Cool, I'll try and implement that now. Thanks!
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: How to build a save system

Post 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
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How to build a save system

Post 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.
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+.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: How to build a save system

Post 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?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How to build a save system

Post 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.
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+.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: How to build a save system

Post by Lord Uber Dowzen »

OK, I worked it out. Now I just need to work on saving...
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How to build a save system

Post 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.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: How to build a save system

Post by Lord Uber Dowzen »

OK sorry for the delay, here's my latest build. Hope it helps!
Attachments
tile engine.love
(12.22 KiB) Downloaded 275 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest