Jump to content




Question about textutils.serialize() and textutils.unserialize()

help

3 replies to this topic

#1 eastar

  • Members
  • 19 posts

Posted 28 July 2020 - 06:05 PM

Hi!

The scenerio:

I'm using CC1.5. I create a program that has nested tables in it. Something like:
t = {1,2,3,{true,false},"cat"}

I want to save this table for later use so I do this:

local file = fs.open(fn,"w")
file.write(textutils.serialize(t))
file.close()

I decide to turn off the game and have dinner. After a hearthy meal I turn on my computer again and start playing again.
My map loads up along with my CC program. It will get to the part in the code where it says:

local file = fs.open(fn,"r")
local data = file.readAll()
file.close()
t = textutils.unserialize(data)

Will the values in the nested table be available for me? As far as I know (at least when I do a print(t) on my serialized table) only the table's reference is saved. Am I thinking it wrong? If not, how do I save the whole table so I can get back all the values?

Thanks in advance!

#2 Lupus590

  • Members
  • 2,010 posts
  • LocationUK

Posted 28 July 2020 - 08:01 PM

print doesn't serialise for you, if you want to print what saves to the file then you need to print the serialised version of the table

you should get nested tables from the serialise and unserialise, the only exception to this is recursive tables and tables with non-serialisable values in them

#3 eastar

  • Members
  • 19 posts

Posted 28 July 2020 - 09:43 PM

 Lupus590, on 28 July 2020 - 08:01 PM, said:

print doesn't serialise for you, if you want to print what saves to the file then you need to print the serialised version of the table

you should get nested tables from the serialise and unserialise, the only exception to this is recursive tables and tables with non-serialisable values in them

Right, I meant to write, do print() on my unserialized table.
Anyway it gives back the reference. What happens if I restart my game between saving the serialized table (that has nested tables in it) and the load it back up and unserialize it? Will the nested tables still have their value?

Thank you!

#4 Lupus590

  • Members
  • 2,010 posts
  • LocationUK

Posted 29 July 2020 - 12:06 AM

 eastar, on 28 July 2020 - 09:43 PM, said:

 Lupus590, on 28 July 2020 - 08:01 PM, said:

you should get nested tables from the ... unserialise

What happens if I restart my game between saving the serialized table (that has nested tables in it) and the load it back up and unserialize it? Will the nested tables still have their value?


Edited by Lupus590, 29 July 2020 - 12:06 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users