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

Problem with textutils.unserialize

Started by MaximumFrank, 21 May 2016 - 04:26 PM
MaximumFrank #1
Posted 21 May 2016 - 06:26 PM
Hey all, I am getting a weird error when trying to use textutils.unserialize(table) in my program.

I am beginning to make an OS, and to get around the screen blinking issues that come with redrawing the screen every time, I am programming in a video buffer of sorts, so that it only draws the few pixels that are actually updated (if you're curious, I have a table that holds the information for the last drawn frame, and when it goes to the render function, it checks against that frame to see if anything is different; if it is, then redraw the changed pixels).

I ran into a slight problem with the programming (things were refreshing when they shouldn't have been), and as part of that, I threw in this line of code (for debugging purposes):


--frameBuffer is defined in _G and resolves to a string if printed or tested with type()
frameBuffer = textutils.unserialize(frameBuffer);

The above code throws an error, "textutils:295: attempt to concatenate string and table"

If I print out frameBuffer by itself, it prints just fine, and if I test it in type() it returns string, so that shouldn't be an issue. Here's the kicker, though: if I run this code, it returns a table:


--this returns a table
print(textutils.unserialize(frameBuffer));

Even more shocking to me, if I change the original code to this, the error goes away:


--original, error
--frameBuffer = textutils.unserialize(frameBuffer);
--modified, no error
tframeBuffer = textutils.unserialize(frameBuffer);

For your ease, here is the pertinent code in textutils:


--textutils 294 through 303
function unserialize( s )
	local func = load( "return "..s, "unserialize", "t", {} )
	if func then
		local ok, result = pcall( func )
		if ok then
			return result
		end
	end
	return nil
end

So my question is, why does it error out on only that given variable name? (For the record, that phrasing has worked everywhere else in my program.) I would be happy to provide more code if needed. Thank you for your help!
Bomb Bloke #2
Posted 22 May 2016 - 02:35 AM
So you're saying:

_G.frameBuffer = "someStringRepresentingATable"

print(textutils.unserialize(frameBuffer));          --> Works fine

tframeBuffer = textutils.unserialize(frameBuffer);  --> Works fine

frameBuffer = textutils.unserialize(frameBuffer);   --> Error claims frameBuffer is already a table

I can't reproduce that behaviour myself, though I suspect that some really weird scoping rules are applicable (the "local function syntax sugar" section of this guide feels related - my bet is you've somehow got multiple "frameBuffer" variables existing within different scopes, and the assignment changes which one gets passed to the unserialise function).

Tricky to pinpoint the exact mechanism at work if I can't replicate it though. Full source and the version of ComputerCraft you're using would make it a lot easier.
MaximumFrank #3
Posted 22 May 2016 - 03:11 AM
I figured it was a scoping issue, as it explains a few other weird issues I am having (specifically with the flickering screen, which when I debug, frameBuffer has incorrect values), and I think the link that you gave explains why it was doing what it was doing. I am going to do a complete rewrite of the whole video buffer portion of my OS so that I can address any possible errors.

And to answer your initial questions, _G.frameBuffer is initialized to a 2D table, which is serialized upon return. If I unserialize it into print or a fresh variable, it works fine, but serializing it into itself (or so I would try to do) returns that it is a table already, yes. I'll update here in a few when I do the rewrite, making sure this time to watch very carefully what I am doing.

Thanks for the link and for the input!

EDIT: Alright, I rewrote the whole thing (this time I didn't even use the serialize / unserialize functions, not sure why I was in the first place). No more screen flashing, no more bugs I can see. Thank you so much!
Edited on 22 May 2016 - 05:40 AM