This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Waterspark63's profile picture

Saving Table To File With Functions

Started by Waterspark63, 26 November 2018 - 06:31 PM
Waterspark63 #1
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 on 26 November 2018 - 06:33 PM
Jummit #2
Posted 26 November 2018 - 08:35 PM
You will just have to remove the function in the table you want to serialize.
KingofGamesYami #3
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).
Stekeblad #4
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.computercraft.info/forums2/index.php?/topic/28012-tabledump-a-simple-way-to-save-a-table-to-a-file-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 on 27 November 2018 - 04:18 AM