Saving multiple tables within a table?

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
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Saving multiple tables within a table?

Post by mangadrive »

I've had zero luck with this using Tserial and other serialization libraries/functions.

For example this works (note: there's 2 different types of serialization there I was testing):

Code: Select all


	player = {
		x = 300,
		y = 300,
		width = 64,
		height = 64,
		speed = 100,
	}


function love.keypressed( key, unicode)


	if key == "q" then
		love.filesystem.write("player.sav", table.serialize(player,true,true))
		
	elseif key == "e" then
		player = loadstring(love.filesystem.read("player.sav"))()
		
	end
	
	if key == "z" then
	local file
	file = love.filesystem.newFile( "config.txt" )
	file:open("w")
	file:write( TSerial.pack( player ) )
	file:close()
		
	elseif key == "c" then
	local file
	love.filesystem.setIdentity( "mygame" )
	if love.filesystem.exists( "config.txt" ) then
    file = love.filesystem.newFile( "config.txt" )
    file:open("r")
    player = TSerial.unpack( file:read() )
    file:close()
	end
		
	end

	end

But I need to work out something that would be more like:

Code: Select all

	

player = {
		x = 300,
		y = 300,
		width = 64,
		height = 64,
		speed = 100,
	}

	player2 = {
		x = 300,
		y = 300,
		width = 64,
		height = 64,
		speed = 100,
	}

players = { }
table.insert (players, player1)
table.insert (players, player2)

.... save players table through serialization methods instead of creating a sav file for player one AND player 2 which would obviously work but... ....

I realize it's not working because there is a nil value or something somewhere, but I really don't understand how you save larger amounts of game data stored through tables, classes,etc into one single save file (inventory, multiple party members, enemy locations if saved during a fight, blah, blah) if this system limits me to using a direct table reference?

I'm missing a piece of the puzzle here, and perhaps someone could explain it better, please? :)
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Saving multiple tables within a table?

Post by Ranguna259 »

I'm not getting eny error, try:

Code: Select all

players={a,b}
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Saving multiple tables within a table?

Post by mangadrive »

I don't get errors, but the data simply doesn't reload. It allows the save, but it's like the save is blank... unless i explicitly only use one and only one table as the save data. Perhaps I'm missing something but a table inserted with all your objects is still a table right? Why would that not save?

edit: I see that in the config.txt it IS saving all the strings but I do not understand why the loading is not working. I'm surely missing something in the loading syntax.
Attachments
savetest.love
Here's a test of what I'm running so you can see what I mean. I commented in the save problem.
(2.86 KiB) Downloaded 133 times
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Saving multiple tables within a table?

Post by Ranguna259 »

What lib are you using to save and to load ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Saving multiple tables within a table?

Post by micha »

I have not used Tserialize before, but here are some hints how you can narrow down the line where the error occurs. This is you loading code:

Code: Select all

if love.filesystem.exists( "config.txt" ) then
  file = love.filesystem.newFile( "config.txt" )
  file:open("r")
  player = TSerial.unpack( file:read() )
  file:close()
end
You said you checked that the data is saved, so you know that config.txt exists. I see three potential error sources
1) the if condition is not satisfied, 2) file:read() does not return the correct string 3) TSerial.unpack does not work as expected.

So extend the code as follows:

Code: Select all

print('Checking for existance')
if love.filesystem.exists( "config.txt" ) then
  print('File exists. Opening')
  file = love.filesystem.newFile( "config.txt" )
  file:open("r")
  local content = file:read()
  print(content)
  player = TSerial.unpack( content )
  file:close()
end
In between the individual steps you get feedback in the console about what works and what not.

Edit: I believe the error is the you do not have the same identity in case of loading and saving. In conf.lua you set the identity to "SUIT" and on loading you set it to "SUIT2", so you are searching in the wrong directory. The above code should reveal this, if correct.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Saving multiple tables within a table?

Post by mangadrive »

I'm sorry for late replies, I haven't had internets D:

I appreciate the suggestions, but it wasn't due to the identity. I do realize in my haste to compile the game I didn't change it, but the original problem doesn't stem from that. Every time I serialize a table that contains more than one thing I'll either get no results or an error saying it only works from strings even the text file contains strings and the console text proves it's being done. I haven't been able to create a solution otherwise, which still leaves me with the original question because cause obviously serialization doesn't work as I thought it would.

1) How do you save data if you are using a more robust entities system and need to save one or more of the tables entities are pooled into without saving EACH entity in it's own file (The only thing that works so far)?

2) If there is no way to do such, is making multiple save files for each table actually viable? I know they are really small files, but it seems creating 20+ files to do a save system seems a bit.. not right.

I'm trying to make a save system for a game that would save player data, and a table of a lot of moving parts (and maybe even inventory inevitably). It seems something as simple as above isn't going to work? I still think there is more to the loading of the files than I fully understand.

I do appreciate the help and insight though.
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: Saving multiple tables within a table?

Post by ArchAngel075 »

I am Currently using Tserial myself for sending tables over network with LUBE, ill look at whether i can get it to send successfully the players table(containing p1,p2 tables)

Ill update or repost here the results, though note that i think ive been able to send a table within a table(clients[1],clients[2] etc are tables containing variables and tables like ID and data = { xpos = #,ypos = #}, thus im sure Tserial should work.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Saving multiple tables within a table?

Post by mangadrive »

Thank you, but again my problem isn't saving them. Tserial in fact saves them. Unless you mean your process has a different way of loading them..

I cannot figure out how to redistribute the serialized data from the file back into the game from the files to the appropriate tables, then passed to the variables within them, etc.

SAVE == [Lots of tables (ie: an "objects" table where all entities are inserted)]---- > [ player] [inventory][game data][config][etc]

Right now it's just:

Load == [Lots of tables in string form in the save file] ----> [Player] (ignores the rest)


In Xna and a couple other things I've played around with, they had more direct built-in serializations that were literally "make a box and throw stuff in it to save/load". It seems a tad more complicated here.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests