sim.packFloatTable return table instead of string.

Report crashes, strange behaviour, or apparent bugs
Post Reply
DrRiften
Posts: 8
Joined: 06 Jun 2024, 13:36

sim.packFloatTable return table instead of string.

Post 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

fferri
Posts: 1303
Joined: 09 Sep 2013, 19:28

Re: sim.packFloatTable return table instead of string.

Post 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.

Post Reply