Saving a table to a savegame file?

So I am trying to figure out how to save my whole array/table to a save file. I tried using the JSON encode function and similar concepts around the forums but none of them work because of how I set up my tables. I have a grid in the game which is the users inventory and I want to save the inventory as it’s own file. Here is how I set up the grid:

[lua]local grid = {}

for i=1, gridWidth do
grid[i] = {}
for j=1, gridHeight do
grid[i][j] = {}
grid[i][j].data1 = blahblah
grid[i][j].data2 = blah
grid[i][j].bg = display.newImageRect(“image.png”, 10, 10)
grid[i][j].data3 = blahblah2
end
end[/lua]

I set up the table this way because it is VERY easy to access anything I need by using “grid[xCoord][yCoord].data” I want to have the ability to save/load the entire “grid” table. Does anyone have any insight on this? [import]uid: 63320 topic_id: 24606 reply_id: 324606[/import]

You can try this to write:

path = system.pathForFile( "filename", system.DocumentsDirectory )  
file = io.open( path, "w" )  
if file then  
 contents = json.encode( grid )  
 file:write( contents )  
 io.close( file )  
end  

And this to read:

path = system.pathForFile( "filename", system.DocumentsDirectory )  
file = io.open( path, "r" )  
if file then  
 contents = file:read( "\*a" )  
 grid = json.decode( contents )  
 io.close( file )  
end  

I’ve only done it with a one dimensional array, but it’s the first thing I would try if I were in your shoes…

(My original post worked for the simulator, but not the device. You can’t write your file into the directory where your source files are. I’ve updated this to work on the device.) [import]uid: 5540 topic_id: 24606 reply_id: 99645[/import]

I already tried that but it throws an error. I can’t find any info on saving multidimensional arrays! This is the error:

“Runtime error
Encoding of function: 0x142b4cf0 unsupported
stack traceback:”
[import]uid: 63320 topic_id: 24606 reply_id: 99676[/import]

I admit to being a Corona noob, but it looks like it can save and load a multidimensional array:

local json = require("json")  
  
local grid = {}  
counter=1  
for i=1, 3 do  
 grid[i] = {}  
 for j=1, 4 do  
 grid[i][j] = {}  
 grid[i][j]=counter  
 counter=counter+1  
 end  
end  
  
for i=1, 3 do  
 for j=1, 4 do  
 print (grid[i][j])  
 end  
end  
  
path = system.pathForFile( "filename", system.DocumentsDirectory )  
file = io.open( path, "w" )  
if file then  
 contents = json.encode( grid )  
 file:write( contents )  
 io.close( file )  
end  
  
grid=nil  
print (grid)  
  
path = system.pathForFile( "filename", system.DocumentsDirectory )  
file = io.open( path, "r" )  
if file then  
 contents = file:read( "\*a" )  
 grid = json.decode( contents )  
 io.close( file )  
end  
  
for i=1, 3 do  
 for j=1, 4 do  
 print (grid[i][j])  
 end  
end  

Maybe it doesn’t like something about “display.newImageRect(“image.png”, 10, 10)”?

That’s about as far as I’m willing to demonstrate my stupidity!

Good luck!
[import]uid: 5540 topic_id: 24606 reply_id: 99683[/import]

I don’t understand, that is the same code with an extra few print statements. How will that change the former code?

EDIT: Well it looks like your right about the newImageRect thing. I’m able to encode a multidimensional array of just strings but that doesn’t really help my situation because my code needs to have the images inserted into the table. Surely there has to be a solution! [import]uid: 63320 topic_id: 24606 reply_id: 99687[/import]

Don’t store the images in the array because that would just be a huge waste of memory since you still have the original images in your source directory anyway. Store the *file names* of the images in the array instead. Then you can just do something like

display.newImageRect(grid[i][j], 10, 10)  

and you’re in business.

And yes, I’m making this up off the top of my head without testing! Hope it helps anyway… [import]uid: 5540 topic_id: 24606 reply_id: 99689[/import]

Thanks Pinback! I would think that would work. I guess I would just have to create the images again once the data is reloaded right? [import]uid: 63320 topic_id: 24606 reply_id: 99690[/import]

Correct. Seems to me that would be the way to do it! Hopefully if more experienced folks have a better idea they will speak up! [import]uid: 5540 topic_id: 24606 reply_id: 99693[/import]

Pinback,

wow thats a amazing idea, never thought about storing the filename instead of the images.

[import]uid: 17701 topic_id: 24606 reply_id: 103303[/import]

See: http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

JSON will encode multi-dimensional tables too, so this will take care of your needs.

[import]uid: 19626 topic_id: 24606 reply_id: 103314[/import]

See: http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

JSON will encode multi-dimensional tables too, so this will take care of your needs.

[import]uid: 19626 topic_id: 24606 reply_id: 103316[/import]

Yes that is what you need to do,

You can’t save userdata to a file, so basically you cannot save a table which contains functions to a file.

ie:

grid[i][j].bg = display.newImageRect("image.png", 10, 10)  

Wont work, this will

grid[i][j].bg = "image.png"  
grid[i][j].size = {10, 10}  
  
--display it  
display.newImageRect(grid[i][j].bg, grid[i][j].size[1], grid[i][j].size[2])  

Hope that helps ! [import]uid: 84637 topic_id: 24606 reply_id: 103317[/import]