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

Unserialize doesn't seem to want to work...

Started by sctjkc01, 27 August 2014 - 01:33 AM
sctjkc01 #1
Posted 27 August 2014 - 03:33 AM
I'm trying to pass commands from a Pocket Computer to a set of Turtles, and I'm having issues telling it where it is in the world.

I've narrowed it down to "textutils.unserialize(message) just doesn't want to work" - here's my evidence:

When I send the update command, the control Pocket Computer sends out a packet to all of the turtles that reads thus:

{
  type = "update",
  program = {
    --snip--
  },
  lines = 88,
}
…and it reads it just perfectly, replaces startup, and reboots itself, launching the new code immediately.

However, when I'm running the setPos command, I send out a packet like this:

{
  for = 4,
  type = "setpos",
  pos = {
    face = 2,
    z = -10,
    y = 0,
    x = 13,
  },
}
…and for some reason it doesn't like it.

I go through the LUA live interpreter, and get both packets from in there. Both times, I run t = textutils.unserialize(msg), where msg is the fourth returned object from os.pullEvent("modem_message"). When msg is the first of the above packets, I can ask t.type and get back the expected "update" - I ask t, I get back the same packet (though oddly the order's a bit different - it ends up lines, program, type). The other packet, not so much - I ask for t, I get nil back. I ask for t.type, I get an error in my face.

I swear that I'm trying to pass a legit serialized object to the turtles, it's just that for some reason textutils just doesn't like it. If necessary, I can pastebin my code for perusal.
Lyqyd #2
Posted 27 August 2014 - 03:38 AM
If necessary, I can pastebin my code for perusal.

Yes, please. It is difficult to diagnose problems with code without the code.
Bomb Bloke #3
Posted 27 August 2014 - 03:50 AM
Though I'd guess it's complaining about your use of the reserved keyword "for". Not that it's an invalid key name, but it does need special handling in order to shove it in a table. For eg, the table constructor you've posted wouldn't work, you'd need to use ["for"] instead. Even then I suspect it'll still trip textutils (though if so, that should be reported as a bug).
sctjkc01 #4
Posted 27 August 2014 - 03:55 AM
If necessary, I can pastebin my code for perusal.

Yes, please. It is difficult to diagnose problems with code without the code.

Controller's end and slave's end.
MKlegoman357 #5
Posted 27 August 2014 - 03:05 PM
As Bomb Bloke said, it is a problem with one of your keys being a reserved Lua keyword (for). This is actually textutils.serialize bug, as it serializes the table wrongly. This probably should be posted on the Bug sub-forum, but I would ask what moderators think about this first.

Currently, the simplest solution would be to rename that key to something else, like "receiver" or "meantFor" or whatever your key is meant to describe.
theoriginalbit #6
Posted 28 August 2014 - 12:05 AM
Depending on the version of ComputerCraft you're using you may just be able to send the table without serialising and unserializing it. I cannot tell you exactly which version it was that added this support, but the later 1.5x and the 1.6x versions support it.