Page 1 of 1

sim.packFloatTable return table instead of string.

Posted: 22 Jul 2024, 09:03
by DrRiften
Function synopsis of sim.packFloatTable from document is

Code: Select all

buffer data = sim.packFloatTable(float[] floatingNumbers, int startFloatIndex = 0,  int floatCount = 0)
It seems that the returned data should be a buffer (string in lua) instead of table. But currently data is a table of bytes. If I want to unpack it or write it to file, I need to concat it by myself through table.concat(sim.packFloatTable(float_table)). Is that a bug or a designed behavior?

CoppeliaSim Version: EDU 4.7.0 rev0

Re: sim.packFloatTable return table instead of string.

Posted: 22 Jul 2024, 15:25
by fferri
That's normal behavior.

Starting from 4.7.0 a 'buffer' is not a string, but a table with special metamethods, that behaves like a object/class.

The value you get in return is a 'buffer' object (you can check by reading its metatable, or with the dedicated function isbuffer()):

Code: Select all

> buf = sim.packFloatTable({0.1, 0.2, 0.3})
> type(buf) -- this type indication is not very specific in Lua
'table'
> isbuffer(buf) -- looks into the table's metatable
true
> #buf -- length op is overridden (and many others)
12
> buf[1] -- gets the first byte as string
> string.byte(buf, 1) -- get the first byte value (8 bit int)
205
> isbuffer(buf .. buf)
true
it implements most metamethods of string, so you don't have to use table.concat, but simply use it as if it was a string (e.g. pass it around as it is, concatenate it using the .. operator, use tostring(buf) to convert it to a string if needed (probably not), etc...)

e.g.:

Code: Select all

@@
> printBytes(sim.packFloatTable({0.1, 0.2, 0.3}))
cd cc cc 3d cd cc 4c 3e 9a 99 99 3e
More info on the manual page: Buffers.