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

Return tables

Started by Xfel, 14 March 2012 - 08:13 PM
Xfel #1
Posted 14 March 2012 - 09:13 PM
Hi,

I noticed that you cannot return tables or functions from a peripheral method implemented in java. I don't need functions, but the lack of tables is really bad. luaj contains a huge conversion algorithm java <=> lua, but for some reason, ComputerCraft uses an own one which can only handle strings, integers, doubles, and null.

So, I would suggest using the luaj java <=> lua conversion for the peripherals.
Casper7526 #2
Posted 14 March 2012 - 11:02 PM
You could always serialize them and unserialize them.
Espen #3
Posted 15 March 2012 - 01:27 AM
@Xfel:

function pack( ... )
  return arg
end
pack() takes an arbitrary amount of parameters, which is then accessible via the table 'arg' (which is then simply returned).
It's basically the opposite of what Lua's unpack() does.

Here's how you'd use the above function:

local periph = peripheral.wrap("right")
local tValues = pack( periph.getMultipleReturnValues() )
After the last line, 'tValues' will be a table with all the values that periph.getMultipleReturnValues() returned.

Cheers :D/>/>
Xfel #4
Posted 15 March 2012 - 03:43 PM
@Espen:
Yes, but that doesn't work if you want nested tables, dictionaries, or metatables

@Casper7526:
I'm currently using this solution, but it is more complicated than needed.