How to build a level file format
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
How to build a level file format
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.
Re: How to build a save system
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:
1 - To load:
This will load the above file and "run"/put the table "map" in "memory".
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.
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.
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()
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()
NOTE: Edited, changed variables a little more and with helping comments in code for understanding of what's is being done.
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
Re: How to build a save system
Cool, I'll try and implement that now. Thanks!
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
Re: How to build a save system
When you say:
attempt to index field '?' (a nil value)
with the culprit line being:
if map[x][y] == b then
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: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.
attempt to index field '?' (a nil value)
with the culprit line being:
if map[x][y] == b then
- 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
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
Re: How to build a save system
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?
- 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
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
Re: How to build a save system
OK, I worked it out. Now I just need to work on saving...
Re: How to build a save system
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.Lord Uber Dowzen wrote:OK, I worked it out. Now I just need to work on saving...
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
Please post your new code because It will be hard from now on have to guess what is not working.
-
- Prole
- Posts: 18
- Joined: Wed Sep 29, 2010 3:02 am
Re: How to build a save system
OK sorry for the delay, here's my latest build. Hope it helps!
- Attachments
-
- tile engine.love
- (12.22 KiB) Downloaded 275 times
Who is online
Users browsing this forum: Google [Bot] and 1 guest