Jump to content




Saving Table To File With Functions


  • You cannot reply to this topic
3 replies to this topic

#1 Waterspark63

  • Members
  • 13 posts

Posted 26 November 2018 - 07:31 PM

Probably a stupid question, but I was trying to save a table to file, but it has a function in it, and it gave me a 'Cannot serialize type function' error, is there anyway I an rewrite this in a way for it to work?

local file = fs.open("/system/software/commands/corecmds.lua","w")
commands = {}
commands.help = {
["Name"]='help',
["Usage"]="[ Type 'Help' For A List Of Commands ]",
["Runtime"]= function() printCentered(math.floor(h/2) -3+i, '[ Listing Commands... ]') for i=1,#placeholder do printCentered(math.floor(h/2) -2+i, "Placeholder") end end,
}
file.write(textutils.serialize(commands))
file.close()

Edited by Waterspark63, 26 November 2018 - 07:33 PM.


#2 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 26 November 2018 - 08:35 PM

You will just have to remove the function in the table you want to serialize.

#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 26 November 2018 - 10:29 PM

The reason that error occurs is because there is no way to create a string from a function that is guaranteed to work the same after converting it back to a function.

As you are writing the function in question, just write it as a string in the first place. You can later use loadstring and pass arguments to it using varargs (as is done with programs).

#4 Stekeblad

  • Members
  • 62 posts
  • LocationSweden

Posted 27 November 2018 - 05:10 AM

I have made an API for saving a table to a file for easy viewing. It can handle functions and nested tables.http://www.computerc...le-and-view-it/

The API is not made to make a file that easily can be converted back to a table, but if that is what you want you may still find something usefull by looking at the code.

Edited by Stekeblad, 27 November 2018 - 05:18 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users