one question, how to update a field data after create instance of monster in the example.

2,147 views
Skip to first unread message

Tom Chen

unread,
Dec 26, 2014, 4:56:57 AM12/26/14
to flatb...@googlegroups.com
in the C++ example ,
 // Build up a serialized buffer algorithmically:
  flatbuffers::FlatBufferBuilder builder;
  auto vec = Vec3(1, 2, 3);
  auto name = builder.CreateString("MyMonster");
  unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  auto inventory = builder.CreateVector(inv_data, 10);
  // Shortcut for creating monster with all fields set:
  auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory,
                            Color_Blue);
  builder.Finish(mloc);
  // We now have a FlatBuffer we can store or send somewhere.
  // ** file/network code goes here :) **
  // access builder.GetBufferPointer() for builder.GetSize() bytes
  // Instead, we're going to access it straight away.
  // Get access to the root:
  auto monster = GetMonster(builder.GetBufferPointer());


so, how  to update the monster after this, normally,  we will change the monster data later. 
e.g:
monster->set_hp(10);

if so, we can use monster as entity class of game.  read or write its data anytime. 

Stewart Miles

unread,
Jan 5, 2015, 1:52:31 PM1/5/15
to Tom Chen, flatb...@googlegroups.com
At the moment flatbuffers are read-only and don't support mutation (modification) after they're created.  If you want to have mutable members of a data structure, you'll need to manually create a class for your runtime data.

If you *really* want to get this working you could create your data structures using "struct" and mutate them at runtime.  However, this will not be endian-safe.

Out of curiosity what are you using flatbuffers for?

I hope this helps,
Stewart

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stewart Miles

unread,
Jan 6, 2015, 1:22:25 PM1/6/15
to Tom Chen, flatb...@googlegroups.com
I understand your use case but flatbuffers are currently read-only once they're built.  If you want to understand the gritty details read http://google.github.io/flatbuffers/md__internals.html .

For example, flatbuffers tables with unspecified values for members or members who have the same value as the default value are *not* stored in the flatbuffer.

In other words, if a monster's HP is set to 10 and the default value is 10 no storage is allocated for the HP value in the monster's flatbuffer which is nice since we end up with a compact data structure but the downside is that you can't assign a new value to the data.  This is primarily why we're not generating code to set values in flatbuffers.

If you want to use flatbuffers as a wire format, it's possible in a couple of ways:
1. Build the object you want to send to the remote (if it's not a struct).
2. Just send a struct to the remote.

To be honest, if you're careful with allocation during the flatbuffer build process, the overhead of sending a flatbuffer via a typical network stack is going to be *way* more expensive than the process of building the buffer.

Are you working on a game or application that you can share the details of?

Cheers,
Stewart


On Mon, Jan 5, 2015 at 8:13 PM, Tom Chen <cnso...@gmail.com> wrote:
Hi Stewart,
    let me explain what i wanted. 
 
    step1:  declare a  monster data class.  it is that auto generated with  auto monster = GetMonster(builder.GetBufferPointer());

     MonsterA.data = monster ;

   step2:
     
     when remote data changed, local will got a event . include the new monster data. 

     MonsterA.update(monster)  .  

    when monster data changed locally, send data to remote. 

     MonsterA.set_hp(10)   
     void set_hp(int hp)
{
      data.set_hp(hp)
      sendtoRemote( data.detlaData) 

  i am thinking the flatbuffer maybe can be used to storage this mutable class data. if so, when class define changed, no need change code.  when runtime data changed, no need create new monster from builder.   
  serialize and unserialize is transparent.  it  is  used widely in networking projects. games and the like. 

That's all. 

    


EJ Chathuranga

unread,
Aug 2, 2019, 2:32:33 AM8/2/19
to FlatBuffers
Is there any modification now>
Both of you said that currently not available in flatbuffer, but now has passed almost 4 years. Do we have any method to update that created Object or any alternatives? 
To unsubscribe from this group and stop receiving emails from it, send an email to flatb...@googlegroups.com.

Wouter van Oortmerssen

unread,
Aug 5, 2019, 12:15:45 PM8/5/19
to EJ Chathuranga, FlatBuffers
Yes, there are some options for mutation now. Primarily there's --gen-object-api which allows you to unpack a FlatBuffer into canonical C++ data structures, mutate them, then serialize them back. This of course fore-goes the "access in-place" efficiency of FlatBuffers, but if you want maximum mutation flexibility, this is the way to go.

Then there's --gen-mutable which allows you to directly mutate scalars on a buffer without unpacking it, but it is limited to scalars which are not the default value (or has to be used with force_defaults). Useful in some cases.

Finally some in-place mutation is possible using reflection, but I'd recommend against it. Use the object API instead.


To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flatbuffers/18762525-743f-4b3d-905b-a479b50997c5%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages