Repository navigation
Expand file tree
/
Copy pathflatbuffers_guide_tutorial.html
More file actions
1030 lines (1023 loc) · 106 KB
/
Copy pathflatbuffers_guide_tutorial.html
File metadata and controls
1030 lines (1023 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.7"/>
<title>FlatBuffers: Tutorial</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,400italic,500,500italic,700,700italic|Roboto+Mono:400,700" rel="stylesheet">
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea" style="height: 110px;">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="commonprojectlogo">
<img alt="Logo" src="fpl_logo_small.png"/>
</td>
<td style="padding-left: 0.5em;">
<div id="projectname">FlatBuffers
</div>
<div style="font-size:12px;">
An open source project by <a href="https://developers.google.com/games/#Tools">FPL</a>.
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.7 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="usergroup0.html"><span>Programmer's Guide</span></a></li>
<li><a href="flatbuffers_support.html"><span>Platform / Language / Feature support</span></a></li>
<li><a href="flatbuffers_benchmarks.html"><span>Benchmarks</span></a></li>
<li><a href="flatbuffers_white_paper.html"><span>FlatBuffers white paper</span></a></li>
<li><a href="flatbuffers_internals.html"><span>FlatBuffers internals</span></a></li>
<li><a href="flatbuffers_grammar.html"><span>Grammar of the schema langauge</span></a></li>
<li><a href="usergroup1.html"><span>API Reference</span></a></li>
<li><a href="contributing.html"><span>Contributing</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('flatbuffers_guide_tutorial.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Properties</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Pages</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Tutorial </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Overview</h2>
<p>This tutorial provides a basic example of how to work with <a class="el" href="index.html#flatbuffers_overview">FlatBuffers</a>. We will step through a simple example application, which shows you how to:</p>
<ul>
<li>Write a FlatBuffer <code>schema</code> file.</li>
<li>Use the <code>flatc</code> FlatBuffer compiler.</li>
<li>Parse <a href="http://json.org">JSON</a> files that conform to a schema into FlatBuffer binary files.</li>
<li>Use the generated files in many of the supported languages (such as C++, Java, and more.)</li>
</ul>
<p>During this example, imagine that you are creating a game where the main character, the hero of the story, needs to slay some <code>orc</code>s. We will walk through each step necessary to create this monster type using FlatBuffers.</p>
<p>Please select your desired language for our quest:</p>
<p>
<form>
<input type="radio" name="language" value="cpp" checked="checked">C++</input>
<input type="radio" name="language" value="java">Java</input>
<input type="radio" name="language" value="csharp">C#</input>
<input type="radio" name="language" value="go">Go</input>
<input type="radio" name="language" value="python">Python</input>
<input type="radio" name="language" value="javascript">JavaScript</input>
<input type="radio" name="language" value="php">PHP</input>
</form>
</p>
<p>
<script>
/**
* Check if an HTML `class` attribute is in the language-specific format.
* @param {string} languageClass An HTML `class` attribute in the format
* 'language-{lang}', where {lang} is a programming language (e.g. 'cpp',
* 'java', 'go', etc.).
* @return {boolean} Returns `true` if `languageClass` was in the valid
* format, prefixed with 'language-'. Otherwise, it returns false.
*/
function isProgrammingLanguageClassName(languageClass) {
if (languageClass && languageClass.substring(0, 9) == 'language-' &&
languageClass.length > 8) {
return true;
} else {
return false;
}
}
/**
* Given a language-specific HTML `class` attribute, extract the language.
* @param {string} languageClass The string name of an HTML `class` attribute,
* in the format `language-{lang}`, where {lang} is a programming language
* (e.g. 'cpp', 'java', 'go', etc.).
* @return {string} Returns a string containing only the {lang} portion of
* the class name. If the input was invalid, then it returns `null`.
*/
function extractProgrammingLanguageFromLanguageClass(languageClass) {
if (isProgrammingLanguageClassName(languageClass)) {
return languageClass.substring(9);
} else {
return null;
}
}
/**
* Hide every code snippet, except for the language that is selected.
*/
function displayChosenLanguage() {
var selection = $('input:checked').val();
var htmlElements = document.getElementsByTagName('*');
for (var i = 0; i < htmlElements.length; i++) {
if (isProgrammingLanguageClassName(htmlElements[i].className)) {
if (extractProgrammingLanguageFromLanguageClass(
htmlElements[i].className).toLowerCase() != selection) {
htmlElements[i].style.display = 'none';
} else {
htmlElements[i].style.display = 'initial';
}
}
}
}
$( document ).ready(displayChosenLanguage);
$('input[type=radio]').on("click", displayChosenLanguage);
</script>
</p>
<h2>Where to Find the Example Code</h2>
<p>Samples demonstating the concepts in this example are located in the source code package, under the <code>samples</code> directory. You can browse the samples on GitHub <a href="https://github.com/google/flatbuffers/tree/master/samples">here</a>.</p>
<p>For your chosen language, please cross-reference with:</p>
<div class="language-cpp"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.cpp">sample_binary.cpp</a> </div> <div class="language-java"> <a href="https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.java">SampleBinary.java</a> </div> <div class="language-csharp"> <a href="https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.cs">SampleBinary.cs</a> </div> <div class="language-go"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.go">sample_binary.go</a> </div> <div class="language-python"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.py">sample_binary.py</a> </div> <div class="language-javascript"> <a href="https://github.com/google/flatbuffers/blob/master/samples/samplebinary.js">samplebinary.js</a> </div> <div class="language-php"> <a href="https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.php">SampleBinary.php</a> </div><h2>Writing the Monsters' FlatBuffer Schema</h2>
<p>To start working with FlatBuffers, you first need to create a <code>schema</code> file, which defines the format for each data structure you wish to serialize. Here is the <code>schema</code> that defines the template for our monsters:</p>
<div class="fragment"><div class="line"><span class="comment">// Example IDL file for our monster's schema.</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">namespace </span>MyGame.Sample;</div>
<div class="line"></div>
<div class="line"><span class="keyword">enum</span> Color:byte { Red = 0, Green, Blue = 2 }</div>
<div class="line"></div>
<div class="line"><span class="keyword">union </span>Equipment { Weapon } <span class="comment">// Optionally add more tables.</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">struct </span>Vec3 {</div>
<div class="line"> x:float;</div>
<div class="line"> y:float;</div>
<div class="line"> z:float;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">table Monster {</div>
<div class="line"> pos:Vec3; <span class="comment">// Struct.</span></div>
<div class="line"> mana:<span class="keywordtype">short</span> = 150;</div>
<div class="line"> hp:<span class="keywordtype">short</span> = 100;</div>
<div class="line"> name:string;</div>
<div class="line"> friendly:<span class="keywordtype">bool</span> = <span class="keyword">false</span> (deprecated);</div>
<div class="line"> inventory:[ubyte]; <span class="comment">// Vector of scalars.</span></div>
<div class="line"> color:Color = Blue; <span class="comment">// Enum.</span></div>
<div class="line"> weapons:[Weapon]; <span class="comment">// Vector of tables.</span></div>
<div class="line"> equipped:Equipment; <span class="comment">// Union.</span></div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">table Weapon {</div>
<div class="line"> name:string;</div>
<div class="line"> damage:short;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line">root_type Monster;</div>
</div><!-- fragment --><p>As you can see, the syntax for the <code>schema</code> <a href="https://en.wikipedia.org/wiki/Interface_description_language">Interface Definition Language (IDL)</a> is similar to those of the C family of languages, and other IDL languages. Let's examine each part of this <code>schema</code> to determine what it does.</p>
<p>The <code>schema</code> starts with a <code>namespace</code> declaration. This determines the corresponding package/namespace for the generated code. In our example, we have the <code>Sample</code> namespace inside of the <code>MyGame</code> namespace.</p>
<p>Next, we have an <code>enum</code> definition. In this example, we have an <code>enum</code> of type <code>byte</code>, named <code>Color</code>. We have three values in this <code>enum</code>: <code>Red</code>, <code>Green</code>, and <code>Blue</code>. We specify <code>Red = 0</code> and <code>Blue = 2</code>, but we do not specify an explicit value for <code>Green</code>. Since the behavior of an <code>enum</code> is to increment if unspecified, <code>Green</code> will receive the implicit value of <code>1</code>.</p>
<p>Following the <code>enum</code> is a <code>union</code>. The <code>union</code> in this example is not very useful, as it only contains the one <code>table</code> (named <code>Weapon</code>). If we had created multiple tables that we would want the <code>union</code> to be able to reference, we could add more elements to the <code>union Equipment</code>.</p>
<p>After the <code>union</code> comes a <code>struct Vec3</code>, which represents a floating point vector with <code>3</code> dimensions. We use a <code>struct</code> here, over a <code>table</code>, because <code>struct</code>s are ideal for data structures that will not change, since they use less memory and have faster lookup.</p>
<p>The <code>Monster</code> table is the main object in our FlatBuffer. This will be used as the template to store our <code>orc</code> monster. We specify some default values for fields, such as <code>mana:short = 150</code>. All unspecified fields will default to <code>0</code> or <code>NULL</code>. Another thing to note is the line <code>friendly:bool = false (deprecated);</code>. Since you cannot delete fields from a <code>table</code> (to support backwards compatability), you can set fields as <code>deprecated</code>, which will prevent the generation of accessors for this field in the generated code. Be careful when using <code>deprecated</code>, however, as it may break legacy code that used this accessor.</p>
<p>The <code>Weapon</code> table is a sub-table used within our FlatBuffer. It is used twice: once within the <code>Monster</code> table and once within the <code>Equipment</code> enum. For our <code>Monster</code>, it is used to populate a <code>vector of tables</code> via the <code>weapons</code> field within our <code>Monster</code>. It is also the only table referenced by the <code>Equipment</code> enum.</p>
<p>The last part of the <code>schema</code> is the <code>root_type</code>. The root type declares what will be the root table for the serialized data. In our case, the root type is our <code>Monster</code> table.</p>
<h4>More Information About Schemas</h4>
<p>You can find a complete guide to writing <code>schema</code> files in the <a class="el" href="flatbuffers_guide_writing_schema.html">Writing a schema</a> section of the Programmer's Guide. You can also view the formal <a class="el" href="flatbuffers_grammar.html">Grammar of the schema language</a>.</p>
<h2>Compiling the Monsters' Schema</h2>
<p>After you have written the FlatBuffers schema, the next step is to compile it.</p>
<p>If you have not already done so, please follow <a class="el" href="flatbuffers_guide_building.html">these instructions</a> to build <code>flatc</code>, the FlatBuffer compiler.</p>
<p>Once <code>flatc</code> is built successfully, compile the schema for your language:</p>
<div class="language-cpp"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --cpp samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --java samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --csharp samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --go samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --python samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --javascript samples/monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">cd flatbuffers/sample</div>
<div class="line">./../flatc --php samples/monster.fbs</div>
</div><!-- fragment --> </div><p>For a more complete guide to using the <code>flatc</code> compiler, pleaes read the <a class="el" href="flatbuffers_guide_using_schema_compiler.html">Using the schema compiler</a> section of the Programmer's Guide.</p>
<h2>Reading and Writing Monster FlatBuffers</h2>
<p>Now that we have compiled the schema for our programming language, we can start creating some monsters and serializing/deserializing them from FlatBuffers.</p>
<h4>Creating and Writing Orc FlatBuffers</h4>
<p>The first step is to import/include the library, generated files, etc.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="preprocessor">#include "monster_generate.h"</span> <span class="comment">// This was generated by `flatc`.</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>MyGame::Sample; <span class="comment">// Specified in the schema.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keyword">import</span> MyGame.Sample.*; <span class="comment">//The `flatc` generated files. (Monster, Vec3, etc.)</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">import</span> <a class="code" href="classcom_1_1google_1_1flatbuffers_1_1_flat_buffer_builder.html">com.google.flatbuffers.FlatBufferBuilder</a>;</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keyword">using</span> FlatBuffers;</div>
<div class="line"><span class="keyword">using</span> MyGame.Sample; <span class="comment">// The `flatc` generated files. (Monster, Vec3, etc.)</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="keyword">import</span> (</div>
<div class="line"> flatbuffers <span class="stringliteral">"github.com/google/flatbuffers/go"</span></div>
<div class="line"> sample <span class="stringliteral">"MyGame/Sample"</span></div>
<div class="line">)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="keyword">import</span> flatbuffers</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> </div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment"># Generated by `flatc`.</span></div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="keyword">import</span> MyGame.Sample.Color</div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="keyword">import</span> MyGame.Sample.Equipment</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="keyword">import</span> MyGame.Sample.Monster</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> <span class="keyword">import</span> MyGame.Sample.Vec3</div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="keyword">import</span> MyGame.Sample.Weapon</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// The following code is for JavaScript module loaders (e.g. Node.js). See</span></div>
<div class="line"><span class="comment">// below for a browser-based HTML/JavaScript example of including the library.</span></div>
<div class="line">var flatbuffers = require(<span class="stringliteral">'/js/flatbuffers'</span>).flatbuffers;</div>
<div class="line">var MyGame = require(<span class="stringliteral">'./monster_generated'</span>).MyGame; <span class="comment">// Generated by `flatc`.</span></div>
<div class="line"></div>
<div class="line"><span class="comment">//--------------------------------------------------------------------------//</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// The following code is for browser-based HTML/JavaScript. Use the above code</span></div>
<div class="line"><span class="comment">// for JavaScript module loaders (e.g. Node.js).</span></div>
<div class="line"><script src=<span class="stringliteral">"../js/flatbuffers.js"</span>></script></div>
<div class="line"><script src=<span class="stringliteral">"monster_generated.js"</span>></script> <span class="comment">// Generated by `flatc`.</span></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// It is recommended that your use PSR autoload when using FlatBuffers in PHP.</span></div>
<div class="line"><span class="comment">// Here is an example from `SampleBinary.php`:</span></div>
<div class="line"><span class="keyword">function</span> __autoload($class_name) {</div>
<div class="line"> <span class="comment">// The last segment of the class name matches the file name.</span></div>
<div class="line"> $class = substr($class_name, strrpos($class_name, <span class="stringliteral">"\\"</span>) + 1);</div>
<div class="line"> $root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)))); <span class="comment">// `flatbuffers` root.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// Contains the `*.php` files for the FlatBuffers library and the `flatc` generated files.</span></div>
<div class="line"> $paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">"php"</span>)),</div>
<div class="line"> join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">"samples"</span>, <span class="stringliteral">"MyGame"</span>, <span class="stringliteral">"Sample"</span>)));</div>
<div class="line"> <span class="keywordflow">foreach</span> ($paths as $path) {</div>
<div class="line"> $file = join(DIRECTORY_SEPARATOR, array($path, $class . <span class="stringliteral">".php"</span>));</div>
<div class="line"> <span class="keywordflow">if</span> (file_exists($file)) {</div>
<div class="line"> require($file);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div><p>Now we are ready to start building some buffers. In order to start, we need to create an instance of the <code>FlatBufferBuilder</code>, which will contain the buffer as it grows:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line"><a class="code" href="classflatbuffers_1_1_flat_buffer_builder.html">flatbuffers::FlatBufferBuilder</a> builder;</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line">FlatBufferBuilder builder = <span class="keyword">new</span> FlatBufferBuilder(0);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line">var builder = <span class="keyword">new</span> FlatBufferBuilder(1);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line">builder := flatbuffers.NewBuilder(0)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment"># monsters' FlatBuffers.</span></div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> builder = flatbuffers.Builder(0)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Create a `flatbuffer.Builder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line">var builder = <span class="keyword">new</span> flatbuffers.Builder(1);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Create a `FlatBufferBuilder`, which will be used to create our</span></div>
<div class="line"><span class="comment">// monsters' FlatBuffers.</span></div>
<div class="line">$builder = <span class="keyword">new</span> <a class="code" href="class_google_1_1_flat_buffers_1_1_flatbuffer_builder.html">Google\FlatBuffers\FlatbufferBuilder</a>(0);</div>
</div><!-- fragment --> </div><p>After creating the <code>builder</code>, we can start serializing our data. Before we make our <code>orc</code> Monster, lets create some <code>Weapon</code>s: a <code>Sword</code> and an <code>Axe</code>.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> weapon_one_name = builder.CreateString(<span class="stringliteral">"Sword"</span>);</div>
<div class="line"><span class="keywordtype">short</span> weapon_one_damage = 3;</div>
<div class="line"></div>
<div class="line"><span class="keyword">auto</span> weapon_two_name = builder.CreateString(<span class="stringliteral">"Axe"</span>);</div>
<div class="line"><span class="keywordtype">short</span> weapon_two_damage = 5;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Use the `CreateWeapon` shortcut to create Weapons with all the fields set.</span></div>
<div class="line"><span class="keyword">auto</span> sword = CreateWeapon(builder, weapon_one_name, weapon_one_damage);</div>
<div class="line"><span class="keyword">auto</span> axe = CreateWeapon(builder, weapon_two_name, weapon_two_damage);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">String weaponOneName = builder.createString(<span class="stringliteral">"Sword"</span>)</div>
<div class="line"><span class="keywordtype">short</span> weaponOneDamage = 3;</div>
<div class="line"></div>
<div class="line">String weaponTwoName = builder.createString(<span class="stringliteral">"Axe"</span>);</div>
<div class="line"><span class="keywordtype">short</span> weaponTwoDamage = 5;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Use the `createWeapon()` helper function to create the weapons, since we set every field.</span></div>
<div class="line"><span class="keywordtype">int</span> sword = Weapon.createWeapon(builder, weaponOneName, weaponOneDamage);</div>
<div class="line"><span class="keywordtype">int</span> axe = Weapon.createWeapon(builder, weaponTwoName, weaponTwoDamage);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var weaponOneName = builder.CreateString(<span class="stringliteral">"Sword"</span>);</div>
<div class="line">var weaponOneDamage = 3;</div>
<div class="line"></div>
<div class="line">var weaponTwoName = builder.CreateString(<span class="stringliteral">"Axe"</span>);</div>
<div class="line">var weaponTwoDamage = 5;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Use the `CreateWeapon()` helper function to create the weapons, since we set every field.</span></div>
<div class="line">var sword = Weapon.CreateWeapon(builder, weaponOneName, (short)weaponOneDamage);</div>
<div class="line">var axe = Weapon.CreateWeapon(builder, weaponTwoName, (short)weaponTwoDamage);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">weaponOne := builder.CreateString(<span class="stringliteral">"Sword"</span>)</div>
<div class="line">weaponTwo := builder.CreateString(<span class="stringliteral">"Axe"</span>)</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create the first `Weapon` ("Sword").</span></div>
<div class="line">sample.WeaponStart(builder)</div>
<div class="line">sample.Weapon.AddName(builder, weaponOne)</div>
<div class="line">sample.Weapon.AddDamage(builder, 3)</div>
<div class="line">sword := sample.WeaponEnd(builder)</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create the second `Weapon` ("Axe").</span></div>
<div class="line">sample.WeaponStart(builder)</div>
<div class="line">sample.Weapon.AddName(builder, weaponTwo)</div>
<div class="line">sample.Weapon.AddDamage(builder, 5)</div>
<div class="line">axe := sample.WeaponEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> weapon_one = builder.CreateString(<span class="stringliteral">'Sword'</span>)</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> weapon_two = builder.CreateString(<span class="stringliteral">'Axe'</span>)</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> </div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment"># Create the first `Weapon` ('Sword').</span></div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> MyGame.Sample.Weapon.WeaponStart(builder)</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> MyGame.Sample.Weapon.WeaponAddName(builder, weapon_one)</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> MyGame.Sample.Weapon.WeaponAddDamage(builder, 3)</div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> sword = MyGame.Sample.Weapon.WeaponEnd(builder)</div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span> </div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="comment"># Create the second `Weapon` ('Axe').</span></div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span> MyGame.Sample.Weapon.WeaponStart(builder)</div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span> MyGame.Sample.Weapon.WeaponAddName(builder, weapon_two)</div>
<div class="line"><a name="l00013"></a><span class="lineno"> 13</span> MyGame.Sample.Weapon.WeaponAddDamage(builder, 5)</div>
<div class="line"><a name="l00014"></a><span class="lineno"> 14</span> axe = MyGame.Sample.Weapon.WeaponEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var weaponOne = builder.createString(<span class="stringliteral">'Sword'</span>);</div>
<div class="line">var weaponTwo = builder.createString(<span class="stringliteral">'Axe'</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create the first `Weapon` ('Sword').</span></div>
<div class="line">MyGame.Sample.Weapon.startWeapon(builder);</div>
<div class="line">MyGame.Sample.Weapon.addName(builder, weaponOne);</div>
<div class="line">MyGame.Sample.Weapon.addDamage(builder, 3);</div>
<div class="line">var sword = MyGame.Sample.Weapon.endWeapon(builder);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create the second `Weapon` ('Axe').</span></div>
<div class="line">MyGame.Sample.Weapon.startWeapon(builder);</div>
<div class="line">MyGame.Sample.Weapon.addName(builder, weaponTwo);</div>
<div class="line">MyGame.Sample.Weapon.addDamage(builder, 5);</div>
<div class="line">var axe = MyGame.Sample.Weapon.endWeapon(builder);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Create the `Weapon`s using the `createWeapon()` helper function.</span></div>
<div class="line">$weapon_one_name = $builder->createString(<span class="stringliteral">"Sword"</span>);</div>
<div class="line">$sword = \MyGame\Sample\Weapon::CreateWeapon($builder, $weapon_one_name, 3);</div>
<div class="line"></div>
<div class="line">$weapon_two_name = $builder->createString(<span class="stringliteral">"Axe"</span>);</div>
<div class="line">$axe = \MyGame\Sample\Weapon::CreateWeapon($builder, $weapon_two_name, 5);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create an array from the two `Weapon`s and pass it to the</span></div>
<div class="line"><span class="comment">// `CreateWeaponsVector()` method to create a FlatBuffer vector.</span></div>
<div class="line">$weaps = array($sword, $axe);</div>
<div class="line">$weapons = \MyGame\Sample\Monster::CreateWeaponsVector($builder, $weaps);</div>
</div><!-- fragment --> </div><p>Now let's create our monster, the <code>orc</code>. For this <code>orc</code>, lets make him <code>red</code> with rage, positioned at <code>(1.0, 2.0, 3.0)</code>, and give him a large pool of hit points with <code>300</code>. We can give him a vector of weapons to choose from (our <code>Sword</code> and <code>Axe</code> from earlier). In this case, we will equip him with the <code>Axe</code>, since it is the most powerful of the two. Lastly, let's fill his inventory with some potential treasures that can be taken once he is defeated.</p>
<p>Before we serialize a monster, we need to first serialize any objects that are contained there-in, i.e. we serialize the data tree using depth-first, pre-order traversal. This is generally easy to do on any tree structures.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called "Orc".</span></div>
<div class="line"><span class="keyword">auto</span> name = builder.CreateString(<span class="stringliteral">"Orc"</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> treasure = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};</div>
<div class="line"><span class="keyword">auto</span> inventory = builder.CreateVector(treasure, 10);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called "Orc".</span></div>
<div class="line"><span class="keywordtype">int</span> name = builder.createString(<span class="stringliteral">"Orc"</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line">byte[] treasure = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};</div>
<div class="line"><span class="keywordtype">int</span> inv = Monster.createInventoryVector(builder, treasure);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called "Orc".</span></div>
<div class="line">var name = builder.CreateString(<span class="stringliteral">"Orc"</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line"><span class="comment">// Note: Since we prepend the bytes, this loop iterates in reverse order.</span></div>
<div class="line">Monster.StartInventoryVector(builder, 10);</div>
<div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 9; i >= 0; i--)</div>
<div class="line">{</div>
<div class="line"> builder.AddByte((byte)i);</div>
<div class="line">}</div>
<div class="line">var inv = builder.EndVector();</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called "Orc".</span></div>
<div class="line">name := builder.CreateString(<span class="stringliteral">"Orc"</span>)</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line"><span class="comment">// Note: Since we prepend the bytes, this loop iterates in reverse.</span></div>
<div class="line">sample.MonsterStartInventoryVector(builder, 10)</div>
<div class="line"><span class="keywordflow">for</span> i := 9; i >= 0; i-- {</div>
<div class="line"> builder.PrependByte(byte(i))</div>
<div class="line">}</div>
<div class="line"><span class="keywordtype">int</span> := builder.EndVector(10)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Serialize a name for our monster, called "Orc".</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> name = builder.CreateString(<span class="stringliteral">"Orc"</span>)</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> </div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment"># Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="comment"># could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="comment"># Note: Since we prepend the bytes, this loop iterates in reverse.</span></div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> MyGame.Sample.Monster.MonsterStartInventoryVector(builder, 10)</div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="keywordflow">for</span> i <span class="keywordflow">in</span> reversed(range(0, 10)):</div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span>  builder.PrependByte(i)</div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span> inv = builder.EndVector(10)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called 'Orc'.</span></div>
<div class="line">var name = builder.createString(<span class="stringliteral">'Orc'</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line">var treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];</div>
<div class="line">var inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called "Orc".</span></div>
<div class="line">$name = $builder->createString(<span class="stringliteral">"Orc"</span>);</div>
<div class="line"></div>
<div class="line"><span class="comment">// Create a `vector` representing the inventory of the Orc. Each number</span></div>
<div class="line"><span class="comment">// could correspond to an item that can be claimed after he is slain.</span></div>
<div class="line">$treasure = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);</div>
<div class="line">$inv = \MyGame\Sample\Monster::CreateInventoryVector($builder, $treasure);</div>
</div><!-- fragment --> </div><p>We serialized two built-in data types (<code>string</code> and <code>vector</code>) and captured their return values. These values are offsets into the serialized data, indicating where they are stored, such that we can refer to them below when adding fields to our monster.</p>
<p><em>Note: To create a <code>vector</code> of nested objects (e.g. <code>table</code>s, <code>string</code>s, or other <code>vector</code>s), collect their offsets into a temporary data structure, and then create an additional <code>vector</code> containing their offsets.</em></p>
<p>For example, take a look at the two <code>Weapon</code>s that we created earlier (<code>Sword</code> and <code>Axe</code>). These are both FlatBuffer <code>table</code>s, whose offsets we now store in memory. Therefore we can create a FlatBuffer <code>vector</code> to contain these offsets.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Place the weapons into a `std::vector`, then convert that into a FlatBuffer `vector`.</span></div>
<div class="line">std::vector<flatbuffers::Offset<Weapon>> weapons_vector;</div>
<div class="line">weapons_vector.push_back(sword);</div>
<div class="line">weapons_vector.push_back(axe);</div>
<div class="line"><span class="keyword">auto</span> weapons = builder.CreateVector(weapons_vector);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Place the two weapons into an array, and pass it to the `createWeaponsVector()` method to</span></div>
<div class="line"><span class="comment">// create a FlatBuffer vector.</span></div>
<div class="line"><span class="keywordtype">int</span>[] weaps = <span class="keyword">new</span> <span class="keywordtype">int</span>[2];</div>
<div class="line">weaps[1] = sword;</div>
<div class="line">weaps[2] = axe;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Pass the `weaps` array into the `createWeaponsVector()` method to create a FlatBuffer vector.</span></div>
<div class="line"><span class="keywordtype">int</span> weapons = Monster.createWeaponsVector(builder, weaps);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var weaps = <span class="keyword">new</span> Offset<Weapon>[2];</div>
<div class="line">weaps[0] = sword;</div>
<div class="line">weaps[1] = axe;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Pass the `weaps` array into the `CreateWeaponsVector()` method to create a FlatBuffer vector.</span></div>
<div class="line">var weapons = Monster.CreateWeaponsVector(builder, weaps);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Create a FlatBuffer vector and prepend the weapons.</span></div>
<div class="line"><span class="comment">// Note: Since we prepend the data, prepend them in reverse order.</span></div>
<div class="line">sample.MonsterStartWeaponsVector(builder, 2)</div>
<div class="line">builder.PrependUOffsetT(axe)</div>
<div class="line">builder.PrependUOffsetT(sword)</div>
<div class="line">weapons := builder.EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Create a FlatBuffer vector and prepend the weapons.</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment"># Note: Since we prepend the data, prepend them in reverse order.</span></div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> MyGame.Sample.Monster.MonsterStartWeaponsVector(builder, 2)</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> builder.PrependUOffsetTRelative(axe)</div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> builder.PrependUOffsetTRelative(sword)</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> weapons = builder.EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Create an array from the two `Weapon`s and pass it to the</span></div>
<div class="line"><span class="comment">// `createWeaponsVector()` method to create a FlatBuffer vector.</span></div>
<div class="line">var weaps = [sword, axe];</div>
<div class="line">var weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Create an array from the two `Weapon`s and pass it to the</span></div>
<div class="line"><span class="comment">// `CreateWeaponsVector()` method to create a FlatBuffer vector.</span></div>
<div class="line">$weaps = array($sword, $axe);</div>
<div class="line">$weapons = \MyGame\Sample\Monster::CreateWeaponsVector($builder, $weaps);</div>
</div><!-- fragment --> </div><p>To create a <code>struct</code>, use the <code>Vec3</code> class/struct that was generated by <code>flatc</code>:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line"><span class="keyword">auto</span> pos = Vec3(1.0f, 2.0f, 3.0f);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line"><span class="keywordtype">int</span> pos = Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line">var pos = Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line">pos := sample.CreateVec3(builder, 1.0, 2.0, 3.0)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> pos = MyGame.Sample.Vec3.CreateVec3(builder, 1.0, 2.0, 3.0)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line">var pos = MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Create a `Vec3`, representing the Orc's position in 3-D space.</span></div>
<div class="line">$pos = \MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0);</div>
</div><!-- fragment --> </div><p>We have now serialized the non-scalar components of the orc, so we can serialize the monster itself:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Set his hit points to 300 and his mana to 150.</span></div>
<div class="line"><span class="keywordtype">int</span> hp = 300;</div>
<div class="line"><span class="keywordtype">int</span> mana = 150;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Finally, create the monster using the `CreateMonster` helper function</span></div>
<div class="line"><span class="comment">// to set all fields.</span></div>
<div class="line"><span class="keyword">auto</span> orc = CreateMonster(builder, &pos, mana, hp, name, inventory, Color_Red,</div>
<div class="line"> weapons, Equipment_Weapon, axe.Union());</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Create our monster using `startMonster()` and `endMonster()`.</span></div>
<div class="line">Monster.startMonster(builder);</div>
<div class="line">Monster.addPos(builder, pos);</div>
<div class="line">Monster.addName(builder, name);</div>
<div class="line">Monster.addColor(builder, Color.Red);</div>
<div class="line">Monster.addHp(builder, (short)300);</div>
<div class="line">Monster.addInventory(builder, inv);</div>
<div class="line">Monster.addWeapons(builder, weapons);</div>
<div class="line">Monster.addEquippedType(builder, Equipment.Weapon);</div>
<div class="line">Monster.addEquipped(builder, axe);</div>
<div class="line"><span class="keywordtype">int</span> orc = Monster.endMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// Create our monster using `StartMonster()` and `EndMonster()`.</span></div>
<div class="line">Monster.StartMonster(builder);</div>
<div class="line">Monster.AddPos(builder, pos);</div>
<div class="line">Monster.AddHp(builder, (short)300);</div>
<div class="line">Monster.AddName(builder, name);</div>
<div class="line">Monster.AddInventory(builder, inv);</div>
<div class="line">Monster.AddColor(builder, Color.Red);</div>
<div class="line">Monster.AddWeapons(builder, weapons);</div>
<div class="line">Monster.AddEquippedType(builder, Equipment.Weapon);</div>
<div class="line">Monster.AddEquipped(builder, axe.Value); <span class="comment">// Axe</span></div>
<div class="line">var orc = Monster.EndMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Create our monster using `MonsterStart()` and `MonsterEnd()`.</span></div>
<div class="line">sample.MonsterStart(builder)</div>
<div class="line">sample.MonsterAddPos(builder, pos)</div>
<div class="line">sample.MonsterAddHp(builder, 300)</div>
<div class="line">sample.MonsterAddName(builder, name)</div>
<div class="line">sample.MonsterAddInventory(builder, inv)</div>
<div class="line">sample.MonsterAddColor(builder, sample.ColorRed)</div>
<div class="line">sample.MonsterAddWeapons(builder, weapons)</div>
<div class="line">sample.MonsterAddEquippedType(builder, sample.EquipmentWeapon)</div>
<div class="line">sample.MonsterAddEquipped(builder, axe)</div>
<div class="line">orc := sample.MonsterEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Create our monster by using `MonsterStart()` and `MonsterEnd()`.</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> MyGame.Sample.Monster.MonsterStart(builder)</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> MyGame.Sample.Monster.MonsterAddPos(builder, pos)</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> MyGame.Sample.Monster.MonsterAddHp(builder, 300)</div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> MyGame.Sample.Monster.MonsterAddName(builder, name)</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> MyGame.Sample.Monster.MonsterAddInventory(builder, inv)</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> MyGame.Sample.Monster.MonsterAddColor(builder,</div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span>  MyGame.Sample.Color.Color().Red)</div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span> MyGame.Sample.Monster.MonsterAddWeapons(builder, weapons)</div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span> MyGame.Sample.Monster.MonsterAddEquippedType(</div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span>  builder, MyGame.Sample.Equipment.Equipment().Weapon)</div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span> MyGame.Sample.Monster.MonsterAddEquipped(builder, axe)</div>
<div class="line"><a name="l00013"></a><span class="lineno"> 13</span> orc = MyGame.Sample.Monster.MonsterEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Create our monster by using `startMonster()` and `endMonster()`.</span></div>
<div class="line">MyGame.Sample.Monster.startMonster(builder);</div>
<div class="line">MyGame.Sample.Monster.addPos(builder, pos);</div>
<div class="line">MyGame.Sample.Monster.addHp(builder, 300);</div>
<div class="line">MyGame.Sample.Monster.addColor(builder, MyGame.Sample.Color.Red)</div>
<div class="line">MyGame.Sample.Monster.addName(builder, name);</div>
<div class="line">MyGame.Sample.Monster.addInventory(builder, inv);</div>
<div class="line">MyGame.Sample.Monster.addWeapons(builder, weapons);</div>
<div class="line">MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon);</div>
<div class="line">MyGame.Sample.Monster.addEquipped(builder, axe);</div>
<div class="line">var orc = MyGame.Sample.Monster.endMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Create our monster by using `StartMonster()` and `EndMonster()`.</span></div>
<div class="line">\MyGame\Sample\Monster::StartMonster($builder);</div>
<div class="line">\MyGame\Sample\Monster::AddPos($builder, $pos);</div>
<div class="line">\MyGame\Sample\Monster::AddHp($builder, 300);</div>
<div class="line">\MyGame\Sample\Monster::AddName($builder, $name);</div>
<div class="line">\MyGame\Sample\Monster::AddInventory($builder, $inv);</div>
<div class="line">\MyGame\Sample\Monster::AddColor($builder, \MyGame\Sample\Color::Red);</div>
<div class="line">\MyGame\Sample\Monster::AddWeapons($builder, $weapons);</div>
<div class="line">\MyGame\Sample\Monster::AddEquippedType($builder, \MyGame\Sample\Equipment::Weapon);</div>
<div class="line">\MyGame\Sample\Monster::AddEquipped($builder, $axe);</div>
<div class="line">$orc = \MyGame\Sample\Monster::EndMonster($builder);</div>
</div><!-- fragment --> </div><div class="language-cpp"> <br />
<em>Note: Since we passing <code>150</code> as the <code>mana</code> field, which happens to be the default value, the field will not actually be written to the buffer, since the default value will be returned on query anyway. This is a nice space savings, especially if default values are common in your data. It also means that you do not need to be worried of adding a lot of fields that are only used in a small number of instances, as it will not bloat the buffer if unused.</em> <br />
<br />
If you do not wish to set every field in a <code>table</code>, it may be more convenient to manually set each field of your monster, instead of calling <code>CreateMonster()</code>. The following snippet is functionally equivalent to the above code, but provides a bit more flexibility. <br />
</p><div class="fragment"><div class="line"><span class="comment">// You can use this code instead of `CreateMonster()`, to create our orc</span></div>
<div class="line"><span class="comment">// manually.</span></div>
<div class="line">MonsterBuilder monster_builder(builder);</div>
<div class="line">monster_builder.add_pos(&pos);</div>
<div class="line">monster_builder.add_hp(hp);</div>
<div class="line">monster_builder.add_name(name);</div>
<div class="line">monster_builder.add_inventory(inventory);</div>
<div class="line">monster_builder.add_color(Color_Red);</div>
<div class="line">monster_builder.add_weapons(weapons);</div>
<div class="line">monster_builder.add_equipped_type(Equipment_Weapon);</div>
<div class="line">monster_builder.add_equpped(axe);</div>
<div class="line"><span class="keyword">auto</span> orc = monster_builder.Finish();</div>
</div><!-- fragment --> </div><p>Before finishing the serialization, let's take a quick look at FlatBuffer <code>union Equipped</code>. There are two parts to each FlatBuffer <code>union</code>. The first, is a hidden field <code>_type</code>, that is generated to hold the type of <code>table</code> referred to by the <code>union</code>. This allows you to know which type to cast to at runtime. Second, is the <code>union</code>'s data.</p>
<p>In our example, the last two things we added to our <code>Monster</code> were the <code>Equipped Type</code> and the <code>Equipped</code> union itself.</p>
<p>Here is a repetition these lines, to help highlight them more clearly:</p>
<div class="language-cpp"> <div class="fragment"><div class="line">monster_builder.add_equipped_type(Equipment_Weapon); <span class="comment">// Union type</span></div>
<div class="line">monster_builder.add_equipped(axe); <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">Monster.addEquippedType(builder, Equipment.Weapon); <span class="comment">// Union type</span></div>
<div class="line">Monster.addEquipped(axe); <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">Monster.AddEquippedType(builder, Equipment.Weapon); <span class="comment">// Union type</span></div>
<div class="line">Monster.AddEquipped(builder, axe.Value); <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">sample.MonsterAddEquippedType(builder, sample.EquipmentWeapon) <span class="comment">// Union type</span></div>
<div class="line">sample.MonsterAddEquipped(builder, axe) <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> MyGame.Sample.Monster.MonsterAddEquippedType( <span class="comment"># Union type</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span>  builder, MyGame.Sample.Equipment.Equipment().Weapon)</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> MyGame.Sample.Monster.MonsterAddEquipped(builder, axe) <span class="comment"># Union data</span></div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon); <span class="comment">// Union type</span></div>
<div class="line">MyGame.Sample.Monster.addEquipped(builder, axe); <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">\MyGame\Sample\Monster::AddEquippedType($builder, \MyGame\Sample\Equipment::Weapon); <span class="comment">// Union type</span></div>
<div class="line">\MyGame\Sample\Monster::AddEquipped($builder, $axe); <span class="comment">// Union data</span></div>
</div><!-- fragment --> </div><p>After you have created your buffer, you will have the offset to the root of the data in the <code>orc</code> variable, so you can finish the buffer by calling the appropriate <code>finish</code> method.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// Call `Finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line"><span class="comment">// Note: Regardless of how you created the `orc`, you still need to call</span></div>
<div class="line"><span class="comment">// `Finish()` on the `FlatBufferBuilder`.</span></div>
<div class="line">builder.Finish(orc); <span class="comment">// You could also call `FinishMonsterBuffer(builder,</span></div>
<div class="line"> <span class="comment">// orc);`.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// Call `finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line">builder.finish(orc); <span class="comment">// You could also call `Monster.finishMonsterBuffer(builder, orc);`.</span></div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// Call `Finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line">builder.Finish(orc.Value); <span class="comment">// You could also call `Monster.FinishMonsterBuffer(builder, orc);`.</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// Call `Finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line">builder.Finish(orc)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># Call `Finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> builder.Finish(orc)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// Call `finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line">builder.finish(orc); <span class="comment">// You could also call `MyGame.Example.Monster.finishMonsterBuffer(builder,</span></div>
<div class="line"> <span class="comment">// orc);`.</span></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// Call `finish()` to instruct the builder that this monster is complete.</span></div>
<div class="line"> $builder->finish($orc); <span class="comment">// You may also call `\MyGame\Sample\Monster::FinishMonsterBuffer(</span></div>
<div class="line"> <span class="comment">// $builder, $orc);`.</span></div>
</div><!-- fragment --> </div><p>The buffer is now ready to be stored somewhere, sent over the network, be compressed, or whatever you'd like to do with it. You can access the buffer like so:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `Finish()`.</span></div>
<div class="line">uint8_t *buf = builder.GetBufferPointer();</div>
<div class="line"><span class="keywordtype">int</span> size = builder.GetSize(); <span class="comment">// Returns the size of the buffer that</span></div>
<div class="line"> <span class="comment">// `GetBufferPointer()` points to.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `finish()`.</span></div>
<div class="line">java.nio.ByteBuffer buf = builder.dataBuffer();</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `Finish()`.</span></div>
<div class="line">var buf = builder.DataBuffer; <span class="comment">// Of type `FlatBuffers.ByteBuffer`.</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `Finish()`.</span></div>
<div class="line">buf := builder.FinishedBytes() <span class="comment">// Of type `byte[]`.</span></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># This must be called after `Finish()`.</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> buf = builder.Output() // Of type `bytearray`.</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `finish()`.</span></div>
<div class="line">var buf = builder.dataBuffer(); <span class="comment">// Of type `flatbuffers.ByteBuffer`.</span></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// This must be called after `finish()`.</span></div>
<div class="line">$buf = $builder->dataBuffer(); <span class="comment">// Of type `Google\FlatBuffers\ByteBuffer`</span></div>
</div><!-- fragment --> </div><h4>Reading Orc FlatBuffers</h4>
<p>Now that we have successfully created an <code>Orc</code> FlatBuffer, the monster data can be saved, sent over a network, etc. Let's now adventure into the inverse, and deserialize a FlatBuffer.</p>
<p>This seciton requires the same import/include, namespace, etc. requirements as before:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="preprocessor">#include "monster_generate.h"</span> <span class="comment">// This was generated by `flatc`.</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>MyGame::Sample; <span class="comment">// Specified in the schema.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keyword">import</span> MyGame.Sample.*; <span class="comment">//The `flatc` generated files. (Monster, Vec3, etc.)</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">import</span> <a class="code" href="classcom_1_1google_1_1flatbuffers_1_1_flat_buffer_builder.html">com.google.flatbuffers.FlatBufferBuilder</a>;</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keyword">using</span> FlatBuffers;</div>
<div class="line"><span class="keyword">using</span> MyGame.Sample; <span class="comment">// The `flatc` generated files. (Monster, Vec3, etc.)</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="keyword">import</span> (</div>
<div class="line"> flatbuffers <span class="stringliteral">"github.com/google/flatbuffers/go"</span></div>
<div class="line"> sample <span class="stringliteral">"MyGame/Sample"</span></div>
<div class="line">)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="keyword">import</span> flatbuffers</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> </div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment"># Generated by `flatc`.</span></div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="keyword">import</span> MyGame.Sample.Any</div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="keyword">import</span> MyGame.Sample.Color</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="keyword">import</span> MyGame.Sample.Monster</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> <span class="keyword">import</span> MyGame.Sample.Vec3</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// The following code is for JavaScript module loaders (e.g. Node.js). See</span></div>
<div class="line"><span class="comment">// below for a browser-based HTML/JavaScript example of including the library.</span></div>
<div class="line">var flatbuffers = require(<span class="stringliteral">'/js/flatbuffers'</span>).flatbuffers;</div>
<div class="line">var MyGame = require(<span class="stringliteral">'./monster_generated'</span>).MyGame; <span class="comment">// Generated by `flatc`.</span></div>
<div class="line"></div>
<div class="line"><span class="comment">//--------------------------------------------------------------------------//</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// The following code is for browser-based HTML/JavaScript. Use the above code</span></div>
<div class="line"><span class="comment">// for JavaScript module loaders (e.g. Node.js).</span></div>
<div class="line"><script src=<span class="stringliteral">"../js/flatbuffers.js"</span>></script></div>
<div class="line"><script src=<span class="stringliteral">"monster_generated.js"</span>></script> <span class="comment">// Generated by `flatc`.</span></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// It is recommended that your use PSR autoload when using FlatBuffers in PHP.</span></div>
<div class="line"><span class="comment">// Here is an example from `SampleBinary.php`:</span></div>
<div class="line"><span class="keyword">function</span> __autoload($class_name) {</div>
<div class="line"> <span class="comment">// The last segment of the class name matches the file name.</span></div>
<div class="line"> $class = substr($class_name, strrpos($class_name, <span class="stringliteral">"\\"</span>) + 1);</div>
<div class="line"> $root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)))); <span class="comment">// `flatbuffers` root.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// Contains the `*.php` files for the FlatBuffers library and the `flatc` generated files.</span></div>
<div class="line"> $paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">"php"</span>)),</div>
<div class="line"> join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">"samples"</span>, <span class="stringliteral">"MyGame"</span>, <span class="stringliteral">"Sample"</span>)));</div>
<div class="line"> <span class="keywordflow">foreach</span> ($paths as $path) {</div>
<div class="line"> $file = join(DIRECTORY_SEPARATOR, array($path, $class . <span class="stringliteral">".php"</span>));</div>
<div class="line"> <span class="keywordflow">if</span> (file_exists($file)) {</div>
<div class="line"> require($file);</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div><p>Then, assuming you have a variable containing to the bytes of data from disk, network, etc., you can create a monster from this data:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line"><span class="keyword">auto</span> buffer_pointer = builder.GetBufferPointer();</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line"><span class="keyword">auto</span> monster = GetMonster(buffer_pointer);</div>
<div class="line"></div>
<div class="line"><span class="comment">// `monster` is of type`Monster *`, and points to somewhere inside the buffer.</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// Note: root object pointers are NOT the same as `buffer_pointer`.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line">java.nio.ByteBuffer buf = builder.dataBuffer();</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line">Monster monster = Monster.getRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line">var buf = builder.DataBuffer;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line">var monster = Monster.GetRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line">buf := builder.FinishedBytes()</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line">monster := sample.GetRootAsMonster(buf, 0)</div>
<div class="line"></div>
<div class="line"><span class="comment">// Note: We use `0` for the offset here, since we got the data using the</span></div>
<div class="line"><span class="comment">// `builder.FinishedBytes()` method. This simulates the data you would</span></div>
<div class="line"><span class="comment">// store/receive in your FlatBuffer. If you wanted to read from the</span></div>
<div class="line"><span class="comment">// `builder.Bytes` directly, you would need to pass in the offset of</span></div>
<div class="line"><span class="comment">// `builder.Head()`, as the builder actually constructs the buffer backwards.</span></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment"># We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment"># network, was read off of disk, etc.</span></div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> buf = builder.Output()</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> </div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="comment"># Deserialize the data from the buffer.</span></div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> monster = MyGame.Sample.Monster.Monster.GetRootAsMonster(buf, 0)</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span> </div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="comment"># Note: We use `0` for the offset here, since we got the data using the</span></div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span> <span class="comment"># `builder.Output()` method. This simulates the data you would store/receive</span></div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="comment"># in your FlatBuffer. If you wanted to read from the `builder.Bytes` directly,</span></div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="comment"># you would need to pass in the offset of `builder.Head()`, as the builder</span></div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span> <span class="comment"># actually constructs the buffer backwards.</span></div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line">var buf = builder.dataBuffer();</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line">var monster = MyGame.Sample.Monster.getRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><span class="comment">// We can access the buffer we just made directly. Pretend this came over a</span></div>
<div class="line"><span class="comment">// network, was read off of disk, etc.</span></div>
<div class="line">$buf = $builder->dataBuffer();</div>
<div class="line"></div>
<div class="line"><span class="comment">// Deserialize the data from the buffer.</span></div>
<div class="line">$monster = \MyGame\Sample\Monster::GetRootAsMonster($buf);</div>
</div><!-- fragment --> </div><p>If you look in the generated files from <code>flatc</code>, you will see it generated accessors for all non-<code>deprecated</code> fields. For example:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> hp = monster->hp();</div>
<div class="line"><span class="keyword">auto</span> mana = monster->mana();</div>
<div class="line"><span class="keyword">auto</span> name = monster->name()->c_str();</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keywordtype">short</span> hp = monster.hp();</div>
<div class="line"><span class="keywordtype">short</span> mana = monster.mana();</div>
<div class="line">String name = monster.name();</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// For C#, unlike other languages support by FlatBuffers, most values (except for</span></div>
<div class="line"><span class="comment">// vectors and unions) are available as propreties instead of asccessor methods.</span></div>
<div class="line">var hp = monster.Hp</div>
<div class="line">var mana = monster.Mana</div>
<div class="line">var name = monster.Name</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">hp := monster.Hp()</div>
<div class="line">mana := monster.Mana()</div>
<div class="line">name := string(monster.Name()) <span class="comment">// Note: `monster.Name()` returns a byte[].</span></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> hp = monster.Hp()</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> mana = monster.Mana()</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> name = monster.Name()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var hp = $monster.hp();</div>
<div class="line">var mana = $monster.mana();</div>
<div class="line">var name = $monster.name();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$hp = $monster->getHp();</div>
<div class="line">$mana = $monster->getMana();</div>
<div class="line">$name = monster->getName();</div>
</div><!-- fragment --> </div><p>These should hold <code>300</code>, <code>150</code>, and <code>"Orc"</code> respectively.</p>
<p><em>Note: We never stored a value in <code>mp</code>, so we got the default value of <code>150</code>.</em></p>
<p>To access sub-objects, in the case of our <code>pos</code>, which is a <code>Vec3</code>:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> pos = monster->pos();</div>
<div class="line"><span class="keyword">auto</span> x = pos->x();</div>
<div class="line"><span class="keyword">auto</span> y = pos->y();</div>
<div class="line"><span class="keyword">auto</span> z = pos->z();</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">Vec3 pos = monster.pos();</div>
<div class="line"><span class="keywordtype">float</span> x = pos.x();</div>
<div class="line"><span class="keywordtype">float</span> y = pos.y();</div>
<div class="line"><span class="keywordtype">float</span> z = pos.z();</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var pos = monster.Pos</div>
<div class="line">var x = pos.X</div>
<div class="line">var y = pos.Y</div>
<div class="line">var z = pos.Z</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">pos := monster.Pos(nil)</div>
<div class="line">x := pos.X()</div>
<div class="line">y := pos.Y()</div>
<div class="line">z := pos.Z()</div>
<div class="line"></div>
<div class="line"><span class="comment">// Note: Whenever you access a new object, like in `Pos()`, a new temporary</span></div>
<div class="line"><span class="comment">// accessor object gets created. If your code is very performance sensitive,</span></div>
<div class="line"><span class="comment">// you can pass in a pointer to an existing `Vec3` instead of `nil`. This</span></div>
<div class="line"><span class="comment">// allows you to reuse it across many calls to reduce the amount of object</span></div>
<div class="line"><span class="comment">// allocation/garbage collection.</span></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> pos = monster.Pos()</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> x = pos.X()</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> y = pos.Y()</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> z = pos.Z()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var pos = monster.pos();</div>
<div class="line">var x = pos.x();</div>
<div class="line">var y = pos.y();</div>
<div class="line">var z = pos.z();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$pos = $monster->getPos();</div>
<div class="line">$x = $pos->getX();</div>
<div class="line">$y = $pos->getY();</div>
<div class="line">$z = $pos->getZ();</div>
</div><!-- fragment --> </div><p><code>x</code>, <code>y</code>, and <code>z</code> will contain <code>1.0</code>, <code>2.0</code>, and <code>3.0</code>, respectively.</p>
<p><em>Note: Had we not set <code>pos</code> during serialization, it would be <code>NULL</code>-value.</em></p>
<p>Similarly, we can access elements of the inventory <code>vector</code> by indexing it. You can also iterate over the length of the array/vector representing the FlatBuffers <code>vector</code>.</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> inv = monster->inventory(); <span class="comment">// A pointer to a `flatbuffers::Vector<>`.</span></div>
<div class="line"><span class="keyword">auto</span> inv_len = inv->Length();</div>
<div class="line"><span class="keyword">auto</span> third_item = inv->Get(2);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keywordtype">int</span> invLength = monster.inventoryLength();</div>
<div class="line">byte thirdItem = monster.inventory(2);</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keywordtype">int</span> invLength = monster.InventoryLength;</div>
<div class="line">var thirdItem = monster.GetInventory(2);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">invLength := monster.InventoryLength()</div>
<div class="line">thirdItem := monster.Inventory(2)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> inv_len = monster.InventoryLength()</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> third_item = monster.Inventory(2)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var invLength = monster.inventoryLength();</div>
<div class="line">var thirdItem = monster.inventory(2);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$inv_len = $monster->getInventoryLength();</div>
<div class="line">$third_item = $monster->getInventory(2);</div>
</div><!-- fragment --> </div><p>For <code>vector</code>s of <code>table</code>s, you can access the elements like any other vector, except your need to handle the result as a FlatBuffer <code>table</code>:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> weapons = monster->weapons(); <span class="comment">// A pointer to a `flatbuffers::Vector<>`.</span></div>
<div class="line"><span class="keyword">auto</span> weapon_len = weapons->Length();</div>
<div class="line"><span class="keyword">auto</span> second_weapon_name = weapons->Get(1)->name()->str();</div>
<div class="line"><span class="keyword">auto</span> second_weapon_damage = weapons->Get(1)->damage()</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keywordtype">int</span> weaponsLength = monster.weaponsLength();</div>
<div class="line">String secondWeaponName = monster.weapons(1).name();</div>
<div class="line"><span class="keywordtype">short</span> secondWeaponDamage = monster.weapons(1).damage();</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keywordtype">int</span> weaponsLength = monster.WeaponsLength;</div>
<div class="line">var secondWeaponName = monster.GetWeapons(1).Name;</div>
<div class="line">var secondWeaponDamage = monster.GetWeapons(1).Damage;</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">weaponLength := monster.WeaponsLength()</div>
<div class="line">weapon := <span class="keyword">new</span>(sample.Weapon) <span class="comment">// We need a `sample.Weapon` to pass into `monster.Weapons()`</span></div>
<div class="line"> <span class="comment">// to capture the output of the function.</span></div>
<div class="line"><span class="keywordflow">if</span> monster.Weapons(weapon, 1) {</div>
<div class="line"> secondWeaponName := weapon.Name()</div>
<div class="line"> secondWeaponDamage := weapon.Damage()</div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> weapons_length = monster.WeaponsLength()</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> second_weapon_name = monster.Weapons(1).Name()</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> second_weapon_damage = monster.Weapons(1).Damage()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var weaponsLength = monster.weaponsLength();</div>
<div class="line">var secondWeaponName = monster.weapons(1).name();</div>
<div class="line">var secondWeaponDamage = monster.weapons(1).damage();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$weapons_len = $monster->getWeaponsLength();</div>
<div class="line">$second_weapon_name = $monster->getWeapons(1)->getName();</div>
<div class="line">$second_weapon_damage = $monster->getWeapons(1)->getDamage();</div>
</div><!-- fragment --> </div><p>Last, we can access our <code>Equipped</code> FlatBuffer <code>union</code>. Just like when we created the <code>union</code>, we need to get both parts of the <code>union</code>: the type and the data.</p>
<p>We can access the type to dynamically cast the data as needed (since the <code>union</code> only stores a FlatBuffer <code>table</code>).</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> union_type = monster.equipped_type();</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> (union_type == Equipment_Weapon) {</div>
<div class="line"> <span class="keyword">auto</span> weapon = <span class="keyword">static_cast<</span><span class="keyword">const </span>Weapon*<span class="keyword">></span>(monster->equipped()); <span class="comment">// Requires `static_cast`</span></div>
<div class="line"> <span class="comment">// to type `const Weapon*`.</span></div>
<div class="line"></div>
<div class="line"> <span class="keyword">auto</span> weapon_name = weapon->name()->str(); <span class="comment">// "Axe"</span></div>
<div class="line"> <span class="keyword">auto</span> weapon_damage = weapon->damage(); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keywordtype">int</span> unionType = monster.EquippedType();</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> (unionType == Equipment.Weapon) {</div>
<div class="line"> Weapon weapon = (Weapon)monster.equipped(<span class="keyword">new</span> Weapon()); <span class="comment">// Requires explicit cast</span></div>
<div class="line"> <span class="comment">// to `Weapon`.</span></div>
<div class="line"></div>
<div class="line"> String weaponName = weapon.name(); <span class="comment">// "Axe"</span></div>
<div class="line"> <span class="keywordtype">short</span> weaponDamage = weapon.damage(); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var unionType = monster.EquippedType;</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> (unionType == Equipment.Weapon) {</div>
<div class="line"> var weapon = (Weapon)monster.GetEquipped(<span class="keyword">new</span> Weapon()); <span class="comment">// Requires explicit cast</span></div>
<div class="line"> <span class="comment">// to `Weapon`.</span></div>
<div class="line"></div>
<div class="line"> var weaponName = weapon.Name; <span class="comment">// "Axe"</span></div>
<div class="line"> var weaponDamage = weapon.Damage; <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><span class="comment">// We need a `flatbuffers.Table` to capture the output of the</span></div>
<div class="line"><span class="comment">// `monster.Equipped()` function.</span></div>
<div class="line">unionTable := <span class="keyword">new</span>(flatbuffers.Table)</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> monster.Equipped(unionTable) {</div>
<div class="line"> unionType := monster.EquippedType()</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> unionType == sample.EquipmentWeapon {</div>
<div class="line"> <span class="comment">// Create a `sample.Weapon` object that can be initialized with the contents</span></div>
<div class="line"> <span class="comment">// of the `flatbuffers.Table` (`unionTable`), which was populated by</span></div>
<div class="line"> <span class="comment">// `monster.Equipped()`.</span></div>
<div class="line"> unionWeapon = <span class="keyword">new</span>(sample.Weapon)</div>
<div class="line"> unionWeapon.Init(unionTable.Bytes, unionTable.Pos)</div>
<div class="line"></div>
<div class="line"> weaponName = unionWeapon.Name()</div>
<div class="line"> weaponDamage = unionWeapon.Damage()</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> union_type = monster.EquippedType()</div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> </div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="keywordflow">if</span> union_type == MyGame.Sample.Equipment.Equipment().Weapon:</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span>  <span class="comment"># `monster.Equipped()` returns a `flatbuffers.Table`, which can be used to</span></div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span>  <span class="comment"># initialize a `MyGame.Sample.Weapon.Weapon()`.</span></div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span>  union_weapon = MyGame.Sample.Weapon.Weapon()</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span>  union_weapon.Init(monster.Equipped().Bytes, monster.Equipped().Pos)</div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> </div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span>  weapon_name = union_weapon.Name() // <span class="stringliteral">'Axe'</span></div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span>  weapon_damage = union_weapon.Damage() // 5</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var unionType = monster.equippedType();</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> (unionType == MyGame.Sample.Equipment.Weapon) {</div>
<div class="line"> var weapon_name = monster.equipped(<span class="keyword">new</span> MyGame.Sample.Weapon()).name(); <span class="comment">// 'Axe'</span></div>
<div class="line"> var weapon_damage = monster.equipped(<span class="keyword">new</span> MyGame.Sample.Weapon()).damage(); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$union_type = $monster->getEquippedType();</div>
<div class="line"></div>
<div class="line"><span class="keywordflow">if</span> ($union_type == \MyGame\Sample\Equipment::Weapon) {</div>
<div class="line"> $weapon_name = $monster->getEquipped(<span class="keyword">new</span> \MyGame\Sample\Weapon())->getName(); <span class="comment">// "Axe"</span></div>
<div class="line"> $weapon_damage = $monster->getEquipped(<span class="keyword">new</span> \MyGame\Sample\Weapon())->getDamage(); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div><h2>Mutating FlatBuffers</h2>
<p>As you saw above, typically once you have created a FlatBuffer, it is read-only from that moment on. There are, however, cases where you have just received a FlatBuffer, and you'd like to modify something about it before sending it on to another recipient. With the above functionality, you'd have to generate an entirely new FlatBuffer, while tracking what you modified in your own data structures. This is inconvenient.</p>
<p>For this reason FlatBuffers can also be mutated in-place. While this is great for making small fixes to an existing buffer, you generally want to create buffers from scratch whenever possible, since it is much more efficient and the API is much more general purpose.</p>
<p>To get non-const accessors, invoke <code>flatc</code> with <code>--gen-mutable</code>.</p>
<p>Similar to how we read fields using the accessors above, we can now use the mutators like so:</p>
<div class="language-cpp"> <div class="fragment"><div class="line"><span class="keyword">auto</span> monster = GetMutableMonster(buffer_pointer); <span class="comment">// non-const</span></div>
<div class="line">monster->mutate_hp(10); <span class="comment">// Set the table `hp` field.</span></div>
<div class="line">monster->mutable_pos()->mutate_z(4); <span class="comment">// Set struct field.</span></div>
<div class="line">monster->mutable_inventory()->Mutate(0, 1); <span class="comment">// Set vector element.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">Monster monster = Monster.getRootAsMonster(buf);</div>
<div class="line">monster.mutateHp(10); <span class="comment">// Set table field.</span></div>
<div class="line">monster.pos().mutateZ(4); <span class="comment">// Set struct field.</span></div>
<div class="line">monster.mutateInventory(0, 1); <span class="comment">// Set vector element.</span></div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var monster = Monster.GetRootAsMonster(buf);</div>
<div class="line">monster.MutateHp(10); <span class="comment">// Set table field.</span></div>
<div class="line">monster.Pos.MutateZ(4); <span class="comment">// Set struct field.</span></div>
<div class="line">monster.MutateInventory(0, 1); <span class="comment">// Set vector element.</span></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line"><API <span class="keywordflow">for</span> mutating FlatBuffers is not yet available in Go.></div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <API <span class="keywordflow">for</span> mutating FlatBuffers <span class="keywordflow">is</span> <span class="keywordflow">not</span> yet available <span class="keywordflow">in</span> Python.></div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line"><API <span class="keywordflow">for</span> mutating FlatBuffers is not yet support in JavaScript.></div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line"><API <span class="keywordflow">for</span> mutating FlatBuffers is not yet supported in PHP.></div>
</div><!-- fragment --> </div><p>We use the somewhat verbose term <code>mutate</code> instead of <code>set</code> to indicate that this is a special use case, not to be confused with the default way of constructing FlatBuffer data.</p>
<p>After the above mutations, you can send on the FlatBuffer to a new recipient without any further work!</p>
<p>Note that any <code>mutate</code> functions on a table will return a boolean, which is <code>false</code> if the field we're trying to set is not present in the buffer. Fields that are not present if they weren't set, or even if they happen to be equal to the default value. For example, in the creation code above, the <code>mana</code> field is equal to <code>150</code>, which is the default value, so it was never stored in the buffer. Trying to call the corresponding <code>mutate</code> method for <code>mana</code> on such data will return <code>false</code>, and the value won't actually be modified!</p>
<p>One way to solve this is to call <code>ForceDefaults</code> on a FlatBufferBuilder to force all fields you set to actually be written. This, of course, increases the size of the buffer somewhat, but this may be acceptable for a mutable buffer.</p>
<div class="language-csharp"> <b>Note: <code>ForceDefaults</code> is not yet implemented in C#.</b> </div><h2>JSON with FlatBuffers</h2>
<h4>Using <code>flatc</code> as a Conversion Tool</h4>
<p>This is often the preferred method to use JSON with FlatBuffers, as it doesn't require you to add any new code to your program. It is also efficient, since you can ship with the binary data. The drawback is that it requires an extra step for your users/developers to perform (although it may be able to be automated as part of your compilation).</p>
<p>Lets say you have a JSON file that describes your monster. In this example, we will use the file <code>flatbuffers/samples/monsterdata.json</code>.</p>
<p>Here are the contents of the file:</p>