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

[FIXED] Cannot serialize type function?

Started by ReBraLaCC, 25 August 2016 - 11:27 AM
ReBraLaCC #1
Posted 25 August 2016 - 01:27 PM
So I'm making an API for the map i'm making so it would be a bit simpler to do everything :)/>/>/>
But I ran in to this error

Cannot serialize type function

And I have no idea why this happens

So if you guys could see what I'm doing wrong :)/>/>/>

API: – called Utils..
Spoiler

buttons = {}
local len = string.len
local sub = string.sub
local width,height = term.getSize()
function reset()
    buttons = {}
end
function drawButtons()
    if len(textutils.serialise(buttons)) > 0 then
	    for k,v in pairs(buttons) do
		    term.setTextColor(v.textC)
		    term.setBackgroundColor(v.backC)
		    term.setCursorPos(v.pos.x,v.pos.y)
		    term.write(" "..v.text.." ")
	    end
    end
end
function addButton(label,name,px,py,func,tC,bC,r) -- Button Management
    if px == "mid" then
	   a = width-len(" "..name.." ")
	   px = a/2
    end

    local tries = 0
    defLabel = label
    while buttons[label] ~= nil and not r do
	    tries = tries+1
	    label = defLabel..""..tries
    end

    buttons[label] = {
	    ["text"] = name,
	    ["func"] = func,
	    ["pos"] = {
		    ["x"] = px,
		    ["y"] = py,
	    },
	    ["textC"] = tC,
	    ["backC"] = bC,
    }

    return label
end
function removeButton(label)
    found = false
    for k,v in pairs(buttons) do
	    r = 1
	   
	    if k == label then
		    found = true
	    end
	   
	    r = r+1
	   
	    if found then
		    buttons[label] = nil
		    break
	    end
    end

    return found
end
function checkClick(x,y)
    for k,v in pairs(buttons) do
	    if x >= v.pos.x and x <= v.pos.x+len(v.text)+1 and y == v.pos.y then
		    v.func()
	    end
    end

end

function clear(c)
    c = c or colors.black
    term.setBackgroundColor(c)
    term.clear()
end

Program itself:
Spoiler

os.loadAPI("Utils")
test = function()
    print("works")
end
---------------- Label	  name	  x    y  function  textcolor   backcolor
Utils.addButton("clrScrn", "Clear", "mid", 3, test, colors.white, colors.purple)
Utils.drawButtons()
while true do
    _, button, x, y = os.pullEvent("mouse_click")
    Utils.checkClick(x, y)
end

I've added the thing at Utils.addButton (in the program not API) because i think somewhere these is the problem :(/>/>/>

Thanks already for taking a look

– ReBraLa
Edited on 25 August 2016 - 11:52 AM
KingofGamesYami #2
Posted 25 August 2016 - 01:46 PM
You can't use textutils.serialize on a table containing a function.
ReBraLaCC #3
Posted 25 August 2016 - 01:51 PM
well i can understand that but where does it say that?


– FIXED
with drawbuttons 0-0
Edited on 25 August 2016 - 11:51 AM