0

Let's say I have the following flatbuffer IDL file:

table Monster {
  mana:short = 150;
  inventory:[ubyte];  // Vector of scalars.
}

And that I want to serialize an array of 2 Monster objects in a buffer.

Apparently it is possible to create the following memory layout for the overall buffer while serializing the data:

ArrayOfUBytesForInventoryOfMonster1|ArrayOfUBytesForInventoryOfMonster2|Monster1Data|Monster2Data

Which means that now all the inventory fields lay in a contiguous memory location.

However is it possible to also do this on the mana field? ie I want to serialize my objects with this memory representation:

ArrayOfUBytesForInventoryOfMonster1|ArrayOfUBytesForInventoryOfMonster2|Monster1ManaValue|Monster2ManaValue|Monster1Data|Monster2Data.

Which has the effect of transforming all the "mana" values into a raw array in memory.

Is it possible to do this with Flatbuffers? It seems that fields can be only be serialized after the start of the object itself

lezebulon
  • 7,607
  • 11
  • 42
  • 73

1 Answers1

0

Neither will work in the way you indicated. Scalar fields like mana are always inline in the table, so will never be contiguous with similar fields. Even vectors like inventory are prefixed by a size field, so their elements are not contiguous, even though they can be adjacent since they are not inline.

If you want contiguous data, you'll explicitly have to write out a single vector of such values.

Aardappel
  • 5,559
  • 1
  • 19
  • 22