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

Textutils:166: Cannot Serialize Type Function? No idea how to fix

Started by KeyFrame, 31 March 2014 - 05:21 PM
KeyFrame #1
Posted 31 March 2014 - 07:21 PM
I am making this code for a normal button program but im getting a wierd error whenever i try to run it. I have tracked it down to occur when the computer tries to call a function wich is linked in the button table. No idea what is causing this. My other tests whit calling functions trough tables went fine but this is acting up for some reason. i Have rewritten this code twice using and im still Getting the Error. Any ideas? Thanks in advance.


currPage = 1
m=peripheral.wrap("right")
mx, my = m.getSize()
b={}
local function addB(label, id, sx, sy, w, h, page, func, state)
b[id] = {}
b[id]["label"] = label
b[id]["sx"] = sx
b[id]["sy"] = sy
b[id]["w"] = w
b[id]["h"] = h
b[id]["page"] = page
b[id]["func"] = func
b[id]["state"] = state
end
local function saveB()
file = fs.open("Bsave", "w")
file.write(textutils.serialize(B)/>/>)
file.close()
end
local function loadB()
if fs.exists("Bsave") then
  file = fs.open("Bsave", "r")
  tab = file.readAll()
  file.close()
  if tab == not nil then
   tab = textutils.unserialize(tab)
   for name1, data1 in pairs(tab) do
	for name2, data2 in pairs(B)/>/> do
	 if name1 == name2 then
	  data2["state"] = data1["state"]
	 end
	end
   end
  end
end
end
local function drawB(id)
for name, data in pairs(B)/>/> do
  if name == id then
   label = data["label"]
   sx = data["sx"]
   sy = data["sy"]
   w = data["w"]
   h = data["h"] - 1
   state = data["state"]
  end

  m.setBackgroundColor(colors.lime)
  m.setTextColor(colors.white)
  for px = sx, sx+w do
   for py = sy, sy+h do
	m.setCursorPos(px, py)
	m.write(" ")
   end
  end

  lx = ((w-#label)/2) + sx
  ly = math.floor(h/2) + sy
  m.setCursorPos(lx, ly)
  m.write(label)
end
end
local function chPage(id)
if id == "pg+" then
  currPage = currPage + 1
elseif id == "pg-" then
  currPage = currPage - 1
  if currPage < 1 then
   currPage = 1
  end
else
  print "Error. Id is not a valid page changer"
end
m.setBackgroundColor(colors.black)
m.clear()
for name, data in pairs(B)/>/> do
  if data["page"] == currPage or data["page"] == 0 then
   drawB(name)
  end
end
end
local function testF()
print "hi world!"
end

local function chClick(cx, cy)
for name, data in pairs(B)/>/> do
  if data["page"] == currPage then
   if data["sx"] < cx and data["sx"] + data["w"]+1 > cx then
	if data["sy"] < cy and data["sy"] + data["h"]+1 > cy then
	 click(name)
	 data["func"](name)
	end
   end
  end
end
end
local function run(nr)
if nr == 0 then
  while true do
   event, side, ix, iy = os.pullEvent("monitor_touch")
   chClick(ix, iy)
  end
else
  while nr > 0 do
   event, side, ix, iy = os.pullEvent("monitor_touch")
   chClick(ix, iy)
  end
end
end

local function fillTableB()
addB("test", 0, 10, 2, 8, 3, 1, testF, false)
end


local function startup()
fillTableB()
loadB()
saveB()
chPage("pg-")
end
startup()
run(0)
Lyqyd #2
Posted 31 March 2014 - 08:05 PM
Your saveB function calls textutils.serialize. The serialization function cannot handle functions in the table being serialized and throws that error.
KeyFrame #3
Posted 31 March 2014 - 09:19 PM
Thank you! Did some changes and it work perfectly now.
apemanzilla #4
Posted 01 April 2014 - 01:56 PM
Thank you! Did some changes and it work perfectly now.
If you really want to save the functions, I'd recommend you use string.dump(theFunction) to convert it to a string, then save it, and to read it later, use loadstring(theSavedString). It's not very pretty at all, but it's probably the only feasible way.
KeyFrame #5
Posted 01 April 2014 - 05:23 PM
Thank you! Did some changes and it work perfectly now.
If you really want to save the functions, I'd recommend you use string.dump(theFunction) to convert it to a string, then save it, and to read it later, use loadstring(theSavedString). It's not very pretty at all, but it's probably the only feasible way.

All i needed to save really was the state of the button. Wich means i didn't even have to store the functions at all. But thanks for the tip.