blob: 9df43b9ecb6c0603701215c4925c8e8f0ed8754b [file] [log] [blame]
<!-- 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.9.1"/>
<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="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></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.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></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">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('flatbuffers_guide_tutorial.html',''); initResizable(); });
/* @license-end */
</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)">
</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="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Tutorial </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="md_Tutorial"></a></p>
<h1><a class="anchor" id="autotoc_md179"></a>
Overview</h1>
<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:
<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="kotlin">Kotlin</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="typescript">TypeScript</input>
<input type="radio" name="language" value="php">PHP</input>
<input type="radio" name="language" value="c">C</input>
<input type="radio" name="language" value="dart">Dart</input>
<input type="radio" name="language" value="lua">Lua</input>
<input type="radio" name="language" value="lobster">Lobster</input>
<input type="radio" name="language" value="rust">Rust</input>
<input type="radio" name="language" value="swift">Swift</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>
<h1><a class="anchor" id="autotoc_md180"></a>
Where to Find the Example Code</h1>
<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>
<div class="language-c"> <em>Note: The above does not apply to C, instead <a href="https://github.com/dvidelabs/flatcc/tree/master/samples">look here</a>.</em> </div><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-kotlin"> <a href="https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.kt">SampleBinary.kt</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"> No sample binary is provided, since JS needs to be transpiled from TypeScript. Please see TypeScript support. </div> <div class="language-typescript"> <em>none yet</em> </div> <div class="language-php"> <a href="https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.php">SampleBinary.php</a> </div> <div class="language-c"> <a href="https://github.com/dvidelabs/flatcc/blob/master/samples/monster/monster.c">monster.c</a> </div> <div class="language-dart"> <a href="https://github.com/google/flatbuffers/blob/master/dart/example/example.dart">example.dart</a> </div> <div class="language-lua"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.lua">sample_binary.lua</a> </div> <div class="language-lobster"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.lobster">sample_binary.lobster</a> </div> <div class="language-rust"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.rs">sample_binary.rs</a> </div> <div class="language-swift"> <a href="https://github.com/google/flatbuffers/blob/master/samples/sample_binary.swift">sample_binary.swift</a> </div><h1><a class="anchor" id="autotoc_md181"></a>
Writing the Monsters' FlatBuffer Schema</h1>
<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">// Example IDL file for our monster&#39;s schema.</div>
<div class="line"> </div>
<div class="line">namespace MyGame.Sample;</div>
<div class="line"> </div>
<div class="line">enum Color:byte { Red = 0, Green, Blue = 2 }</div>
<div class="line"> </div>
<div class="line">union Equipment { Weapon } // Optionally add more tables.</div>
<div class="line"> </div>
<div class="line">struct 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; // Struct.</div>
<div class="line"> mana:short = 150;</div>
<div class="line"> hp:short = 100;</div>
<div class="line"> name:string;</div>
<div class="line"> friendly:bool = false (deprecated);</div>
<div class="line"> inventory:[ubyte]; // Vector of scalars.</div>
<div class="line"> color:Color = Blue; // Enum.</div>
<div class="line"> weapons:[Weapon]; // Vector of tables.</div>
<div class="line"> equipped:Equipment; // Union.</div>
<div class="line"> path:[Vec3]; // Vector of structs.</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>. If unspecified, scalar fields (like <code>int</code>, <code>uint</code>, or <code>float</code>) will be given a default of <code>0</code> while strings and tables will be given a default of <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> union. 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> union.</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>
<p>The scalar types can also use alias type names such as <code>int16</code> instead of <code>short</code> and <code>float32</code> instead of <code>float</code>. Thus we could also write the <code>Weapon</code> table as:</p>
<div class="fragment"><div class="line">table Weapon {</div>
<div class="line"> name:string;</div>
<div class="line"> damage:int16;</div>
<div class="line">}</div>
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md182"></a>
More Information About Schemas</h3>
<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>
<h1><a class="anchor" id="autotoc_md183"></a>
Compiling the Monsters' Schema</h1>
<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-c"> <em>Note: If you're working in C, you need to use the separate project <a href="https://github.com/dvidelabs/flatcc">FlatCC</a> which contains a schema compiler and runtime library in C for C.</em> <br />
See <a href="https://github.com/dvidelabs/flatcc#building">flatcc build instructions</a>. <br />
Please be aware of the difference between <code>flatc</code> and <code>flatcc</code> tools. <br />
</div> <div class="language-cpp"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --cpp monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --java monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --kotlin monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --csharp monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --go monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --python monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --ts monster.fbs</div>
<div class="line"># customize your TS -&gt; JS transpilation</div>
<div class="line">tsc monster_generated.ts</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --ts 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 monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">cd flatcc</div>
<div class="line">mkdir -p build/tmp/samples/monster</div>
<div class="line">bin/flatcc -a -o build/tmp/samples/monster samples/monster/monster.fbs</div>
<div class="line"># or just</div>
<div class="line">flatcc/samples/monster/build.sh</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --dart monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --lua monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --lobster monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --rust monster.fbs</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">cd flatbuffers/samples</div>
<div class="line">./../flatc --swift monster.fbs</div>
</div><!-- fragment --> </div><p>For a more complete guide to using the <code>flatc</code> compiler, please read the <a class="el" href="flatbuffers_guide_using_schema_compiler.html">Using the schema compiler</a> section of the Programmer's Guide.</p>
<h1><a class="anchor" id="autotoc_md184"></a>
Reading and Writing Monster FlatBuffers</h1>
<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>
<h3><a class="anchor" id="autotoc_md185"></a>
Creating and Writing Orc FlatBuffers</h3>
<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 &quot;monster_generated.h&quot;</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> com.google.flatbuffers.FlatBufferBuilder;</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">import MyGame.Sample.* //The `flatc` generated files. (Monster, Vec3, etc.)</div>
<div class="line"> </div>
<div class="line">import com.google.flatbuffers.FlatBufferBuilder</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keyword">using</span> <a class="code" href="namespace_flat_buffers.html">FlatBuffers</a>;</div>
<div class="line"><span class="keyword">using</span> MyGame.Sample; <span class="comment">// The `flatc` generated files. (Monster, Vec3, etc.)</span></div>
<div class="ttc" id="anamespace_flat_buffers_html"><div class="ttname"><a href="namespace_flat_buffers.html">FlatBuffers</a></div><div class="ttdef"><b>Definition:</b> FlatBufferBuilder.cs:27</div></div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">import (</div>
<div class="line"> flatbuffers &quot;github.com/google/flatbuffers/go&quot;</div>
<div class="line"> sample &quot;MyGame/Sample&quot;</div>
<div class="line">)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><span class="keyword">import</span> flatbuffers</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Generated by `flatc`.</span></div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Color</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Equipment</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Monster</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Vec3</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Weapon</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// The following code is an example - use your desired module flavor by transpiling from TS. </div>
<div class="line">var flatbuffers = require(&#39;/js/flatbuffers&#39;).flatbuffers;</div>
<div class="line">var MyGame = require(&#39;./monster_generated&#39;).MyGame; // Generated by `flatc`.</div>
<div class="line"> </div>
<div class="line">//--------------------------------------------------------------------------//</div>
<div class="line"> </div>
<div class="line">// The following code is for browser-based HTML/JavaScript. Use the above code</div>
<div class="line">// for JavaScript module loaders (e.g. Node.js).</div>
<div class="line">&lt;script src=&quot;../js/flatbuffers.js&quot;&gt;&lt;/script&gt;</div>
<div class="line">&lt;script src=&quot;monster_generated.js&quot;&gt;&lt;/script&gt; // Generated by `flatc`.</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// note: import flatbuffers with your desired import method</div>
<div class="line"> </div>
<div class="line">import { MyGame } from &#39;./monster_generated&#39;;</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">&quot;\\&quot;</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">&quot;php&quot;</span>)),</div>
<div class="line"> join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">&quot;samples&quot;</span>, <span class="stringliteral">&quot;MyGame&quot;</span>, <span class="stringliteral">&quot;Sample&quot;</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">&quot;.php&quot;</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> <div class="language-c"> <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;monster_builder.h&quot;</span> <span class="comment">// Generated by `flatcc`.</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Convenient namespace macro to manage long namespace prefix.</span></div>
<div class="line"><span class="preprocessor">#undef ns</span></div>
<div class="line"><span class="preprocessor">#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x) </span><span class="comment">// Specified in the schema.</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// A helper to simplify creating vectors from C-arrays.</span></div>
<div class="line"><span class="preprocessor">#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))</span></div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">import &#39;package:flat_buffers/flat_buffers.dart&#39; as fb;</div>
<div class="line"> </div>
<div class="line">// Generated by `flatc`.</div>
<div class="line">import &#39;monster_my_game.sample_generated.dart&#39; as myGame;</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- require the flatbuffers module</div>
<div class="line">local flatbuffers = require(&quot;flatbuffers&quot;)</div>
<div class="line"> </div>
<div class="line">-- require the generated files from `flatc`.</div>
<div class="line">local color = require(&quot;MyGame.Sample.Color&quot;)</div>
<div class="line">local equipment = require(&quot;MyGame.Sample.Equipment&quot;)</div>
<div class="line">local monster = require(&quot;MyGame.Sample.Monster&quot;)</div>
<div class="line">local vec3 = require(&quot;MyGame.Sample.Vec3&quot;)</div>
<div class="line">local weapon = require(&quot;MyGame.Sample.Weapon&quot;)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">import from &quot;../lobster/&quot; // Where to find flatbuffers.lobster</div>
<div class="line">import monster_generated</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// import the flatbuffers runtime library</div>
<div class="line">extern crate flatbuffers;</div>
<div class="line"> </div>
<div class="line">// import the generated code</div>
<div class="line">#[allow(dead_code, unused_imports)]</div>
<div class="line">#[path = &quot;./monster_generated.rs&quot;]</div>
<div class="line">mod monster_generated;</div>
<div class="line">pub use monster_generated::my_game::sample::{get_root_as_monster,</div>
<div class="line"> Color, Equipment,</div>
<div class="line"> Monster, MonsterArgs,</div>
<div class="line"> Vec3,</div>
<div class="line"> Weapon, WeaponArgs};</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">/**</div>
<div class="line">// make sure that monster_generated.swift is included in your project</div>
<div class="line">*/</div>
<div class="line">import Flatbuffers</div>
<div class="line"> </div>
<div class="line">// typealiases for convenience</div>
<div class="line">typealias Monster = MyGame1_Sample_Monster</div>
<div class="line">typealias Weapon = MyGame1_Sample_Weapon</div>
<div class="line">typealias Color = MyGame1_Sample_Color</div>
<div class="line">typealias Vec3 = MyGame1_Sample_Vec3</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. You can pass an initial size of the buffer (here 1024 bytes), which will grow automatically if needed:</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&#39; FlatBuffers.</span></div>
<div class="line"><a class="code" href="classflatbuffers_1_1_flat_buffer_builder.html">flatbuffers::FlatBufferBuilder</a> builder(1024);</div>
<div class="ttc" id="aclassflatbuffers_1_1_flat_buffer_builder_html"><div class="ttname"><a href="classflatbuffers_1_1_flat_buffer_builder.html">flatbuffers::FlatBufferBuilder</a></div><div class="ttdoc">Helper class to hold data needed in creation of a FlatBuffer.</div><div class="ttdef"><b>Definition:</b> flatbuffers.h:1215</div></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&#39; FlatBuffers.</span></div>
<div class="line">FlatBufferBuilder builder = <span class="keyword">new</span> FlatBufferBuilder(1024);</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">// Create a `FlatBufferBuilder`, which will be used to create our</div>
<div class="line">// monsters&#39; FlatBuffers.</div>
<div class="line">val builder = FlatBufferBuilder(1024)</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&#39; FlatBuffers.</span></div>
<div class="line">var builder = <span class="keyword">new</span> FlatBufferBuilder(1024);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">// Create a `FlatBufferBuilder`, which will be used to create our</div>
<div class="line">// monsters&#39; FlatBuffers.</div>
<div class="line">builder := flatbuffers.NewBuilder(1024)</div>
</div><!-- fragment --> </div> <div class="language-python"> <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&#39; FlatBuffers.</span></div>
<div class="line">builder = flatbuffers.Builder(1024)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// Create a `flatbuffer.Builder`, which will be used to create our</div>
<div class="line">// monsters&#39; FlatBuffers.</div>
<div class="line">var builder = new flatbuffers.Builder(1024);</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// Create a `flatbuffer.Builder`, which will be used to create our</div>
<div class="line">// monsters&#39; FlatBuffers.</div>
<div class="line">let builder = new flatbuffers.Builder(1024);</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&#39; 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>(1024);</div>
<div class="ttc" id="aclass_google_1_1_flat_buffers_1_1_flatbuffer_builder_html"><div class="ttname"><a href="class_google_1_1_flat_buffers_1_1_flatbuffer_builder.html">Google\FlatBuffers\FlatbufferBuilder</a></div><div class="ttdef"><b>Definition:</b> FlatbufferBuilder.php:25</div></div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">flatcc_builder_t builder, *B;</div>
<div class="line">B = &amp;builder;</div>
<div class="line"><span class="comment">// Initialize the builder object.</span></div>
<div class="line">flatcc_builder_init(B);</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// Create the fb.Builder object that will be used by our generated builders</div>
<div class="line">// Note that if you are only planning to immediately get the byte array this builder would create,</div>
<div class="line">// you can use the convenience method `toBytes()` on the generated builders.</div>
<div class="line">// For example, you could do something like `new myGame.MonsterBuilder(...).toBytes()`</div>
<div class="line">var builder = new fb.Builder(initialSize: 1024);</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- get access to the builder, providing an array of size 1024</div>
<div class="line">local builder = flatbuffers.Builder(1024)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">// get access to the builder</div>
<div class="line">let builder = flatbuffers_builder {}</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Build up a serialized buffer algorithmically.</div>
<div class="line">// Initialize it with a capacity of 1024 bytes.</div>
<div class="line">let mut builder = flatbuffers::FlatBufferBuilder::new_with_capacity(1024);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// create a `FlatBufferBuilder`, which will be used to serialize objects</div>
<div class="line">let builder = FlatBufferBuilder(initialSize: 1024)</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, let's 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">&quot;Sword&quot;</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">&quot;Axe&quot;</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"><span class="keywordtype">int</span> weaponOneName = builder.createString(<span class="stringliteral">&quot;Sword&quot;</span>)</div>
<div class="line"><span class="keywordtype">short</span> weaponOneDamage = 3;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> weaponTwoName = builder.createString(<span class="stringliteral">&quot;Axe&quot;</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-kotlin"> <div class="fragment"><div class="line">val weaponOneName = builder.createString(&quot;Sword&quot;)</div>
<div class="line">val weaponOneDamage: Short = 3;</div>
<div class="line"> </div>
<div class="line">val weaponTwoName = builder.createString(&quot;Axe&quot;)</div>
<div class="line">val weaponTwoDamage: Short = 5;</div>
<div class="line"> </div>
<div class="line">// Use the `createWeapon()` helper function to create the weapons, since we set every field.</div>
<div class="line">val sword = Weapon.createWeapon(builder, weaponOneName, weaponOneDamage)</div>
<div class="line">val 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">&quot;Sword&quot;</span>);</div>
<div class="line">var weaponOneDamage = 3;</div>
<div class="line"> </div>
<div class="line">var weaponTwoName = builder.CreateString(<span class="stringliteral">&quot;Axe&quot;</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, (<span class="keywordtype">short</span>)weaponOneDamage);</div>
<div class="line">var axe = Weapon.CreateWeapon(builder, weaponTwoName, (<span class="keywordtype">short</span>)weaponTwoDamage);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">weaponOne := builder.CreateString(&quot;Sword&quot;)</div>
<div class="line">weaponTwo := builder.CreateString(&quot;Axe&quot;)</div>
<div class="line"> </div>
<div class="line">// Create the first `Weapon` (&quot;Sword&quot;).</div>
<div class="line">sample.WeaponStart(builder)</div>
<div class="line">sample.WeaponAddName(builder, weaponOne)</div>
<div class="line">sample.WeaponAddDamage(builder, 3)</div>
<div class="line">sword := sample.WeaponEnd(builder)</div>
<div class="line"> </div>
<div class="line">// Create the second `Weapon` (&quot;Axe&quot;).</div>
<div class="line">sample.WeaponStart(builder)</div>
<div class="line">sample.WeaponAddName(builder, weaponTwo)</div>
<div class="line">sample.WeaponAddDamage(builder, 5)</div>
<div class="line">axe := sample.WeaponEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">weapon_one = builder.CreateString(<span class="stringliteral">&#39;Sword&#39;</span>)</div>
<div class="line">weapon_two = builder.CreateString(<span class="stringliteral">&#39;Axe&#39;</span>)</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Create the first `Weapon` (&#39;Sword&#39;).</span></div>
<div class="line">MyGame.Sample.Weapon.Start(builder)</div>
<div class="line">MyGame.Sample.Weapon.AddName(builder, weapon_one)</div>
<div class="line">MyGame.Sample.Weapon.AddDamage(builder, 3)</div>
<div class="line">sword = MyGame.Sample.Weapon.End(builder)</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Create the second `Weapon` (&#39;Axe&#39;).</span></div>
<div class="line">MyGame.Sample.Weapon.Start(builder)</div>
<div class="line">MyGame.Sample.Weapon.AddName(builder, weapon_two)</div>
<div class="line">MyGame.Sample.Weapon.AddDamage(builder, 5)</div>
<div class="line">axe = MyGame.Sample.Weapon.End(builder)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">var weaponOne = builder.createString(&#39;Sword&#39;);</div>
<div class="line">var weaponTwo = builder.createString(&#39;Axe&#39;);</div>
<div class="line"> </div>
<div class="line">// Create the first `Weapon` (&#39;Sword&#39;).</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">// Create the second `Weapon` (&#39;Axe&#39;).</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-typescript"> <div class="fragment"><div class="line">let weaponOne = builder.createString(&#39;Sword&#39;);</div>
<div class="line">let weaponTwo = builder.createString(&#39;Axe&#39;);</div>
<div class="line"> </div>
<div class="line">// Create the first `Weapon` (&#39;Sword&#39;).</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">let sword = MyGame.Sample.Weapon.endWeapon(builder);</div>
<div class="line"> </div>
<div class="line">// Create the second `Weapon` (&#39;Axe&#39;).</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">let 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-&gt;createString(<span class="stringliteral">&quot;Sword&quot;</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-&gt;createString(<span class="stringliteral">&quot;Axe&quot;</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> <div class="language-c"> <div class="fragment"><div class="line">flatbuffers_string_ref_t weapon_one_name = flatbuffers_string_create_str(B, <span class="stringliteral">&quot;Sword&quot;</span>);</div>
<div class="line">uint16_t weapon_one_damage = 3;</div>
<div class="line"> </div>
<div class="line">flatbuffers_string_ref_t weapon_two_name = flatbuffers_string_create_str(B, <span class="stringliteral">&quot;Axe&quot;</span>);</div>
<div class="line">uint16_t weapon_two_damage = 5;</div>
<div class="line"> </div>
<div class="line">ns(Weapon_ref_t) sword = ns(Weapon_create(B, weapon_one_name, weapon_one_damage));</div>
<div class="line">ns(Weapon_ref_t) axe = ns(Weapon_create(B, weapon_two_name, weapon_two_damage));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// The generated Builder classes work much like in other languages,</div>
<div class="line">final int weaponOneName = builder.writeString(&quot;Sword&quot;);</div>
<div class="line">final int weaponOneDamage = 3;</div>
<div class="line"> </div>
<div class="line">final int weaponTwoName = builder.writeString(&quot;Axe&quot;);</div>
<div class="line">final int weaponTwoDamage = 5;</div>
<div class="line"> </div>
<div class="line">final swordBuilder = new myGame.WeaponBuilder(builder)</div>
<div class="line"> ..begin()</div>
<div class="line"> ..addNameOffset(weaponOneName)</div>
<div class="line"> ..addDamage(weaponOneDamage);</div>
<div class="line">final int sword = swordBuilder.finish();</div>
<div class="line"> </div>
<div class="line">final axeBuilder = new myGame.WeaponBuilder(builder)</div>
<div class="line"> ..begin()</div>
<div class="line"> ..addNameOffset(weaponTwoName)</div>
<div class="line"> ..addDamage(weaponTwoDamage);</div>
<div class="line">final int axe = axeBuilder.finish();</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">// The generated ObjectBuilder classes offer an easier to use alternative</div>
<div class="line">// at the cost of requiring some additional reference allocations. If memory</div>
<div class="line">// usage is critical, or if you&#39;ll be working with especially large messages</div>
<div class="line">// or tables, you should prefer using the generated Builder classes.</div>
<div class="line">// The following code would produce an identical buffer as above.</div>
<div class="line">final String weaponOneName = &quot;Sword&quot;;</div>
<div class="line">final int weaponOneDamage = 3;</div>
<div class="line"> </div>
<div class="line">final String weaponTwoName = &quot;Axe&quot;;</div>
<div class="line">final int weaponTwoDamage = 5;</div>
<div class="line"> </div>
<div class="line">final myGame.WeaponBuilder sword = new myGame.WeaponObjectBuilder(</div>
<div class="line"> name: weaponOneName,</div>
<div class="line"> damage: weaponOneDamage,</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line">final myGame.WeaponBuilder axe = new myGame.WeaponObjectBuilder(</div>
<div class="line"> name: weaponTwoName,</div>
<div class="line"> damage: weaponTwoDamage,</div>
<div class="line">);</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">local weaponOne = builder:CreateString(&quot;Sword&quot;)</div>
<div class="line">local weaponTwo = builder:CreateString(&quot;Axe&quot;)</div>
<div class="line"> </div>
<div class="line">-- Create the first &#39;Weapon&#39;</div>
<div class="line">weapon.Start(builder)</div>
<div class="line">weapon.AddName(builder, weaponOne)</div>
<div class="line">weapon.AddDamage(builder, 3)</div>
<div class="line">local sword = weapon.End(builder)</div>
<div class="line"> </div>
<div class="line">-- Create the second &#39;Weapon&#39;</div>
<div class="line">weapon.Start(builder)</div>
<div class="line">weapon.AddName(builder, weaponTwo)</div>
<div class="line">weapon.AddDamage(builder, 5)</div>
<div class="line">local axe = weapon.End(builder)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let weapon_names = [ &quot;Sword&quot;, &quot;Axe&quot; ]</div>
<div class="line">let weapon_damages = [ 3, 5 ]</div>
<div class="line"> </div>
<div class="line">let weapon_offsets = map(weapon_names) name, i:</div>
<div class="line"> let ns = builder.CreateString(name)</div>
<div class="line"> MyGame_Sample_WeaponBuilder { b }</div>
<div class="line"> .start()</div>
<div class="line"> .add_name(ns)</div>
<div class="line"> .add_damage(weapon_damages[i])</div>
<div class="line"> .end()</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Serialize some weapons for the Monster: A &#39;sword&#39; and an &#39;axe&#39;.</div>
<div class="line">let weapon_one_name = builder.create_string(&quot;Sword&quot;);</div>
<div class="line">let weapon_two_name = builder.create_string(&quot;Axe&quot;);</div>
<div class="line"> </div>
<div class="line">// Use the `Weapon::create` shortcut to create Weapons with named field</div>
<div class="line">// arguments.</div>
<div class="line">let sword = Weapon::create(&amp;mut builder, &amp;WeaponArgs{</div>
<div class="line"> name: Some(weapon_one_name),</div>
<div class="line"> damage: 3,</div>
<div class="line">});</div>
<div class="line">let axe = Weapon::create(&amp;mut builder, &amp;WeaponArgs{</div>
<div class="line"> name: Some(weapon_two_name),</div>
<div class="line"> damage: 5,</div>
<div class="line">});</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">let weapon1Name = builder.create(string: &quot;Sword&quot;)</div>
<div class="line">let weapon2Name = builder.create(string: &quot;Axe&quot;)</div>
<div class="line"> </div>
<div class="line">// start creating the weapon by calling startWeapon</div>
<div class="line">let weapon1Start = Weapon.startWeapon(&amp;builder)</div>
<div class="line">Weapon.add(name: weapon1Name, &amp;builder)</div>
<div class="line">Weapon.add(damage: 3, &amp;builder)</div>
<div class="line">// end the object by passing the start point for the weapon 1</div>
<div class="line">let sword = Weapon.endWeapon(&amp;builder, start: weapon1Start)</div>
<div class="line"> </div>
<div class="line">let weapon2Start = Weapon.startWeapon(&amp;builder)</div>
<div class="line">Weapon.add(name: weapon2Name, &amp;builder)</div>
<div class="line">Weapon.add(damage: 5, &amp;builder)</div>
<div class="line">let axe = Weapon.endWeapon(&amp;builder, start: weapon2Start)</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 therein, 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 &quot;Orc&quot;.</span></div>
<div class="line"><span class="keyword">auto</span> name = builder.CreateString(<span class="stringliteral">&quot;Orc&quot;</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 &quot;Orc&quot;.</span></div>
<div class="line"><span class="keywordtype">int</span> name = builder.createString(<span class="stringliteral">&quot;Orc&quot;</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">byte</span>[] 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-kotlin"> <div class="fragment"><div class="line">// Serialize a name for our monster, called &quot;Orc&quot;.</div>
<div class="line">val name = builder.createString(&quot;Orc&quot;)</div>
<div class="line"> </div>
<div class="line">// Create a `vector` representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</div>
<div class="line">val treasure = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)</div>
<div class="line">val 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 &quot;Orc&quot;.</span></div>
<div class="line">var name = builder.CreateString(<span class="stringliteral">&quot;Orc&quot;</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 &gt;= 0; i--)</div>
<div class="line">{</div>
<div class="line"> builder.AddByte((<span class="keywordtype">byte</span>)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">// Serialize a name for our monster, called &quot;Orc&quot;.</div>
<div class="line">name := builder.CreateString(&quot;Orc&quot;)</div>
<div class="line"> </div>
<div class="line">// Create a `vector` representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</div>
<div class="line">// Note: Since we prepend the bytes, this loop iterates in reverse.</div>
<div class="line">sample.MonsterStartInventoryVector(builder, 10)</div>
<div class="line">for i := 9; i &gt;= 0; i-- {</div>
<div class="line"> builder.PrependByte(byte(i))</div>
<div class="line">}</div>
<div class="line">inv := builder.EndVector(10)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><span class="comment"># Serialize a name for our monster, called &quot;Orc&quot;.</span></div>
<div class="line">name = builder.CreateString(<span class="stringliteral">&quot;Orc&quot;</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">MyGame.Sample.Monster.StartInventoryVector(builder, 10)</div>
<div class="line"><span class="keywordflow">for</span> i <span class="keywordflow">in</span> reversed(range(0, 10)):</div>
<div class="line"> builder.PrependByte(i)</div>
<div class="line">inv = builder.EndVector()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// Serialize a name for our monster, called &#39;Orc&#39;.</div>
<div class="line">var name = builder.createString(&#39;Orc&#39;);</div>
<div class="line"> </div>
<div class="line">// Create a `vector` representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</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-typescript"> <div class="fragment"><div class="line">// Serialize a name for our monster, called &#39;Orc&#39;.</div>
<div class="line">let name = builder.createString(&#39;Orc&#39;);</div>
<div class="line"> </div>
<div class="line">// Create a `vector` representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</div>
<div class="line">let treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];</div>
<div class="line">let 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 &quot;Orc&quot;.</span></div>
<div class="line">$name = $builder-&gt;createString(<span class="stringliteral">&quot;Orc&quot;</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> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Serialize a name for our monster, called &quot;Orc&quot;.</span></div>
<div class="line"><span class="comment">// The _str suffix indicates the source is an ascii-z string.</span></div>
<div class="line">flatbuffers_string_ref_t name = flatbuffers_string_create_str(B, <span class="stringliteral">&quot;Orc&quot;</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">uint8_t treasure[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};</div>
<div class="line">flatbuffers_uint8_vec_ref_t inventory;</div>
<div class="line"><span class="comment">// `c_vec_len` is the convenience macro we defined earlier.</span></div>
<div class="line">inventory = flatbuffers_uint8_vec_create(B, treasure, c_vec_len(treasure));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// Serialize a name for our monster, called &quot;Orc&quot;.</div>
<div class="line">final int name = builder.writeString(&#39;Orc&#39;);</div>
<div class="line"> </div>
<div class="line">// Create a list representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</div>
<div class="line">final List&lt;int&gt; treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];</div>
<div class="line">final inventory = builder.writeListUint8(treasure);</div>
<div class="line"> </div>
<div class="line">// The following code should be used instead if you intend to use the</div>
<div class="line">// ObjectBuilder classes:</div>
<div class="line">// Serialize a name for our monster, called &quot;Orc&quot;.</div>
<div class="line">final String name = &#39;Orc&#39;;</div>
<div class="line"> </div>
<div class="line">// Create a list representing the inventory of the Orc. Each number</div>
<div class="line">// could correspond to an item that can be claimed after he is slain.</div>
<div class="line">final List&lt;int&gt; treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Serialize a name <span class="keywordflow">for</span> our mosnter, called <span class="stringliteral">&#39;orc&#39;</span></div>
<div class="line">local name = builder:CreateString(<span class="stringliteral">&quot;Orc&quot;</span>)</div>
<div class="line"> </div>
<div class="line">-- Create a `vector` representing the inventory of the Orc. Each number</div>
<div class="line">-- could correspond to an item that can be claimed after he <span class="keywordflow">is</span> slain.</div>
<div class="line">-- Note: Since we prepend the bytes, this loop iterates <span class="keywordflow">in</span> reverse.</div>
<div class="line">monster.StartInventoryVector(builder, 10)</div>
<div class="line"><span class="keywordflow">for</span> i=10,1,-1 do</div>
<div class="line"> builder:PrependByte(i)</div>
<div class="line">end</div>
<div class="line">local inv = builder:EndVector(10)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">// Name of the monster.</div>
<div class="line">let name = builder.CreateString(&quot;Orc&quot;)</div>
<div class="line"> </div>
<div class="line">// Inventory.</div>
<div class="line">let inv = builder.MyGame_Sample_MonsterCreateInventoryVector(map(10): _)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Name of the Monster.</div>
<div class="line">let name = builder.create_string(&quot;Orc&quot;);</div>
<div class="line"> </div>
<div class="line">// Inventory.</div>
<div class="line">let inventory = builder.create_vector(&amp;[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Name of the Monster.</div>
<div class="line">let name = builder.create(string: &quot;Orc&quot;)</div>
<div class="line"> </div>
<div class="line">// create inventory</div>
<div class="line">let inventory: [Byte] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</div>
<div class="line">let inventoryOffset = builder.createVector(inventory)</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>If instead of creating a vector from an existing array you serialize elements individually one by one, take care to note that this happens in reverse order, as buffers are built back to front.</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&lt;flatbuffers::Offset&lt;Weapon&gt;&gt; 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[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"><span class="keywordtype">int</span> weapons = Monster.createWeaponsVector(builder, weaps);</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">// Place the two weapons into an array, and pass it to the `createWeaponsVector()` method to</div>
<div class="line">// create a FlatBuffer vector.</div>
<div class="line">val weaps = intArrayOf(sword, axe)</div>
<div class="line"> </div>
<div class="line">// Pass the `weaps` array into the `createWeaponsVector()` method to create a FlatBuffer vector.</div>
<div class="line">val 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&lt;Weapon&gt;[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">// Create a FlatBuffer vector and prepend the weapons.</div>
<div class="line">// Note: Since we prepend the data, prepend them in reverse order.</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"><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">MyGame.Sample.Monster.StartWeaponsVector(builder, 2)</div>
<div class="line">builder.PrependUOffsetTRelative(axe)</div>
<div class="line">builder.PrependUOffsetTRelative(sword)</div>
<div class="line">weapons = builder.EndVector()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// Create an array from the two `Weapon`s and pass it to the</div>
<div class="line">// `createWeaponsVector()` method to create a FlatBuffer vector.</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-typescript"> <div class="fragment"><div class="line">// Create an array from the two `Weapon`s and pass it to the</div>
<div class="line">// `createWeaponsVector()` method to create a FlatBuffer vector.</div>
<div class="line">let weaps = [sword, axe];</div>
<div class="line">let 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> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// We use the internal builder stack to implement a dynamic vector.</span></div>
<div class="line">ns(Weapon_vec_start(B));</div>
<div class="line">ns(Weapon_vec_push(B, sword));</div>
<div class="line">ns(Weapon_vec_push(B, axe));</div>
<div class="line">ns(Weapon_vec_ref_t) weapons = ns(Weapon_vec_end(B));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// If using the Builder classes, serialize the `[sword,axe]`</div>
<div class="line">final weapons = builder.writeList([sword, axe]);</div>
<div class="line"> </div>
<div class="line">// If using the ObjectBuilders, just create an array from the two `Weapon`s</div>
<div class="line">final List&lt;myGame.WeaponBuilder&gt; weaps = [sword, axe];</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Create a FlatBuffer vector and prepend the weapons.</div>
<div class="line">-- Note: Since we prepend the data, prepend them in reverse order.</div>
<div class="line">monster.StartWeaponsVector(builder, 2)</div>
<div class="line">builder:PrependUOffsetTRelative(axe)</div>
<div class="line">builder:PrependUOffsetTRelative(sword)</div>
<div class="line">local weapons = builder:EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let weapons = builder.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Create a FlatBuffer `vector` that contains offsets to the sword and axe</div>
<div class="line">// we created above.</div>
<div class="line">let weapons = builder.create_vector(&amp;[sword, axe]);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Create a FlatBuffer `vector` that contains offsets to the sword and axe</div>
<div class="line">// we created above.</div>
<div class="line">let weaponsOffset = builder.createVector(ofOffsets: [sword, axe])</div>
</div><!-- fragment --> </div><p><br />
Note there are additional convenience overloads of <code>CreateVector</code>, allowing you to work with data that's not in a <code>std::vector</code> or allowing you to generate elements by calling a lambda. For the common case of <code>std::vector&lt;std::string&gt;</code> there's also <code>CreateVectorOfStrings</code>. </p>
<p>Note that vectors of structs are serialized differently from tables, since structs are stored in-line in the vector. For example, to create a vector for the <code>path</code> field above:</p>
<div class="language-cpp"> <div class="fragment"><div class="line">Vec3 points[] = { Vec3(1.0f, 2.0f, 3.0f), Vec3(4.0f, 5.0f, 6.0f) };</div>
<div class="line"><span class="keyword">auto</span> path = builder.CreateVectorOfStructs(points, 2);</div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line">Monster.startPathVector(fbb, 2);</div>
<div class="line">Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f);</div>
<div class="line">Vec3.createVec3(builder, 4.0f, 5.0f, 6.0f);</div>
<div class="line"><span class="keywordtype">int</span> path = fbb.endVector();</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">Monster.startPathVector(fbb, 2)</div>
<div class="line">Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f)</div>
<div class="line">Vec3.createVec3(builder, 4.0f, 5.0f, 6.0f)</div>
<div class="line">val path = fbb.endVector()</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">Monster.StartPathVector(fbb, 2);</div>
<div class="line">Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);</div>
<div class="line">Vec3.CreateVec3(builder, 4.0f, 5.0f, 6.0f);</div>
<div class="line">var path = fbb.EndVector();</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">sample.MonsterStartPathVector(builder, 2)</div>
<div class="line">sample.CreateVec3(builder, 1.0, 2.0, 3.0)</div>
<div class="line">sample.CreateVec3(builder, 4.0, 5.0, 6.0)</div>
<div class="line">path := builder.EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">MyGame.Sample.Monster.StartPathVector(builder, 2)</div>
<div class="line">MyGame.Sample.Vec3.CreateVec3(builder, 1.0, 2.0, 3.0)</div>
<div class="line">MyGame.Sample.Vec3.CreateVec3(builder, 4.0, 5.0, 6.0)</div>
<div class="line">path = builder.EndVector()</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">MyGame.Sample.Monster.startPathVector(builder, 2);</div>
<div class="line">MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0);</div>
<div class="line">MyGame.Sample.Vec3.createVec3(builder, 4.0, 5.0, 6.0);</div>
<div class="line">var path = builder.endVector();</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">MyGame.Sample.Monster.startPathVector(builder, 2);</div>
<div class="line">MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0);</div>
<div class="line">MyGame.Sample.Vec3.createVec3(builder, 4.0, 5.0, 6.0);</div>
<div class="line">let path = builder.endVector();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">\MyGame\Example\Monster::StartPathVector($builder, 2);</div>
<div class="line">\MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0);</div>
<div class="line">\MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0);</div>
<div class="line">$path = $builder-&gt;endVector();</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// TBD</span></div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// Using the Builder classes, you can write a list of structs like so:</div>
<div class="line">// Note that the intended order should be reversed if order is important.</div>
<div class="line">final vec3Builder = new myGame.Vec3Builder(builder);</div>
<div class="line">vec3Builder.finish(4.0, 5.0, 6.0);</div>
<div class="line">vec3Builder.finish(1.0, 2.0, 3.0);</div>
<div class="line">final int path = builder.endStructVector(2); // the length of the vector</div>
<div class="line"> </div>
<div class="line">// Otherwise, using the ObjectBuilder classes:</div>
<div class="line">// The dart implementation provides a simple interface for writing vectors</div>
<div class="line">// of structs, in `writeListOfStructs`. This method takes</div>
<div class="line">// `List&lt;ObjectBuilder&gt;` and is used by the generated builder classes.</div>
<div class="line">final List&lt;myGame.Vec3ObjectBuilder&gt; path = [</div>
<div class="line"> new myGame.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),</div>
<div class="line"> new myGame.Vec3ObjectBuilder(x: 4.0, y: 5.0, z: 6.0)</div>
<div class="line">];</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Create a FlatBuffer vector and prepend the path locations.</div>
<div class="line">-- Note: Since we prepend the data, prepend them in reverse order.</div>
<div class="line">monster.StartPathVector(builder, 2)</div>
<div class="line">vec3.CreateVec3(builder, 1.0, 2.0, 3.0)</div>
<div class="line">vec3.CreateVec3(builder, 4.0, 5.0, 6.0)</div>
<div class="line">local path = builder:EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">builder.MyGame_Sample_MonsterStartPathVector(2)</div>
<div class="line">builder.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0)</div>
<div class="line">builder.MyGame_Sample_CreateVec3(4.0, 5.0, 6.0)</div>
<div class="line">let path = builder.EndVector(2)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Create the path vector of Vec3 objects.</div>
<div class="line">let x = Vec3::new(1.0, 2.0, 3.0);</div>
<div class="line">let y = Vec3::new(4.0, 5.0, 6.0);</div>
<div class="line">let path = builder.create_vector(&amp;[x, y]);</div>
<div class="line"> </div>
<div class="line">// Note that, for convenience, it is also valid to create a vector of</div>
<div class="line">// references to structs, like this:</div>
<div class="line">// let path = builder.create_vector(&amp;[&amp;x, &amp;y]);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">//</div>
<div class="line">let points = fbb.createVector(ofStructs: [</div>
<div class="line"> Vec3(x: 1, y: 2, z: 3),</div>
<div class="line"> Vec3(x: 4, y: 5, z: 6)</div>
<div class="line">])</div>
<div class="line"> </div>
<div class="line">// OR</div>
<div class="line">var vec3 = [</div>
<div class="line"> Vec3(x: 1, y: 2, z: 3),</div>
<div class="line"> Vec3(x: 4, y: 5, z: 6)</div>
<div class="line">]</div>
<div class="line">Monster.startVectorOfVec3(2, in: &amp;fbb)</div>
<div class="line">for i in obj {</div>
<div class="line"> _ = create(struct: i)</div>
<div class="line">}</div>
<div class="line">let points = fbb.endVector(len: size)</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">// Create the position struct</span></div>
<div class="line"><span class="keyword">auto</span> position = Vec3(1.0f, 2.0f, 3.0f);</div>
<div class="line"> </div>
<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, &amp;position, mana, hp, name, inventory,</div>
<div class="line"> Color_Red, weapons, Equipment_Weapon, axe.Union(),</div>
<div class="line"> path);</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, Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f));</div>
<div class="line">Monster.addName(builder, name);</div>
<div class="line">Monster.addColor(builder, Color.Red);</div>
<div class="line">Monster.addHp(builder, (<span class="keywordtype">short</span>)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">Monster.addPath(builder, path);</div>
<div class="line"><span class="keywordtype">int</span> orc = Monster.endMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">// Create our monster using `startMonster()` and `endMonster()`.</div>
<div class="line">Monster.startMonster(builder)</div>
<div class="line">Monster.addPos(builder, Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f))</div>
<div class="line">Monster.addName(builder, name)</div>
<div class="line">Monster.addColor(builder, Color.Red)</div>
<div class="line">Monster.addHp(builder, 300.toShort())</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">Monster.addPath(builder, path)</div>
<div class="line">val 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, Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f));</div>
<div class="line">Monster.AddHp(builder, (<span class="keywordtype">short</span>)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">Monster.AddPath(builder, path);</div>
<div class="line">var orc = Monster.EndMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">// Create our monster using `MonsterStart()` and `MonsterEnd()`.</div>
<div class="line">sample.MonsterStart(builder)</div>
<div class="line">sample.MonsterAddPos(builder, sample.CreateVec3(builder, 1.0, 2.0, 3.0))</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">sample.MonsterAddPath(builder, path)</div>
<div class="line">orc := sample.MonsterEnd(builder)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><span class="comment"># Create our monster by using `Monster.Start()` and `Monster.End()`.</span></div>
<div class="line">MyGame.Sample.Monster.Start(builder)</div>
<div class="line">MyGame.Sample.Monster.AddPos(builder,</div>
<div class="line"> MyGame.Sample.Vec3.CreateVec3(builder, 1.0, 2.0, 3.0))</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,</div>
<div class="line"> MyGame.Sample.Color.Color().Red)</div>
<div class="line">MyGame.Sample.Monster.AddWeapons(builder, weapons)</div>
<div class="line">MyGame.Sample.Monster.AddEquippedType(</div>
<div class="line"> builder, MyGame.Sample.Equipment.Equipment().Weapon)</div>
<div class="line">MyGame.Sample.Monster.AddEquipped(builder, axe)</div>
<div class="line">MyGame.Sample.Monster.AddPath(builder, path)</div>
<div class="line">orc = MyGame.Sample.Monster.End(builder)</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// Create our monster by using `startMonster()` and `endMonster()`.</div>
<div class="line">MyGame.Sample.Monster.startMonster(builder);</div>
<div class="line">MyGame.Sample.Monster.addPos(builder,</div>
<div class="line"> MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0));</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">MyGame.Sample.Monster.addPath(builder, path);</div>
<div class="line">var orc = MyGame.Sample.Monster.endMonster(builder);</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// Create our monster by using `startMonster()` and `endMonster()`.</div>
<div class="line">MyGame.Sample.Monster.startMonster(builder);</div>
<div class="line">MyGame.Sample.Monster.addPos(builder,</div>
<div class="line"> MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0));</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">MyGame.Sample.Monster.addPath(builder, path);</div>
<div class="line">let 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,</div>
<div class="line"> \MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0));</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">\MyGame\Sample\Monster::AddPath($builder, $path);</div>
<div class="line">$orc = \MyGame\Sample\Monster::EndMonster($builder);</div>
</div><!-- fragment --> </div> <div class="language-c"> <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">uint16_t hp = 300;</div>
<div class="line">uint16_t mana = 150;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Define an equipment union. `create` calls in C has a single</span></div>
<div class="line"><span class="comment">// argument for unions where C++ has both a type and a data argument.</span></div>
<div class="line">ns(Equipment_union_ref_t) equipped = ns(Equipment_as_Weapon(axe));</div>
<div class="line">ns(Vec3_t) pos = { 1.0f, 2.0f, 3.0f };</div>
<div class="line">ns(Monster_create_as_root(B, &amp;pos, mana, hp, name, inventory, ns(Color_Red),</div>
<div class="line"> weapons, equipped, path));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// Using the Builder API:</div>
<div class="line">// Set his hit points to 300 and his mana to 150.</div>
<div class="line">final int hp = 300;</div>
<div class="line">final int mana = 150;</div>
<div class="line"> </div>
<div class="line">final monster = new myGame.MonsterBuilder(builder)</div>
<div class="line"> ..begin()</div>
<div class="line"> ..addNameOffset(name)</div>
<div class="line"> ..addInventoryOffset(inventory)</div>
<div class="line"> ..addWeaponsOffset(weapons)</div>
<div class="line"> ..addEquippedType(myGame.EquipmentTypeId.Weapon)</div>
<div class="line"> ..addEquippedOffset(axe)</div>
<div class="line"> ..addHp(hp)</div>
<div class="line"> ..addMana(mana)</div>
<div class="line"> ..addPos(vec3Builder.finish(1.0, 2.0, 3.0))</div>
<div class="line"> ..addPathOffset(path)</div>
<div class="line"> ..addColor(myGame.Color.Red);</div>
<div class="line"> </div>
<div class="line">final int orc = monster.finish();</div>
<div class="line"> </div>
<div class="line">// -Or- using the ObjectBuilder API:</div>
<div class="line">// Set his hit points to 300 and his mana to 150.</div>
<div class="line">final int hp = 300;</div>
<div class="line">final int mana = 150;</div>
<div class="line"> </div>
<div class="line">// Note that these parameters are optional - it is not necessary to set</div>
<div class="line">// all of them.</div>
<div class="line">// Also note that it is not necessary to `finish` the builder helpers above</div>
<div class="line">// - the generated code will automatically reuse offsets if the same object</div>
<div class="line">// is used in more than one place (e.g. the axe appearing in `weapons` and</div>
<div class="line">// `equipped`).</div>
<div class="line">final myGame.MonsterBuilder orcBuilder = new myGame.MonsterBuilder(</div>
<div class="line"> name: name,</div>
<div class="line"> inventory: treasure,</div>
<div class="line"> weapons: weaps,</div>
<div class="line"> equippedType: myGame.EquipmentTypeId.Weapon,</div>
<div class="line"> equipped: axe,</div>
<div class="line"> path: path,</div>
<div class="line"> hp: hp,</div>
<div class="line"> mana: mana,</div>
<div class="line"> pos: new myGame.Vec3Builder(x: 1.0, y: 2.0, z: 3.0),</div>
<div class="line"> color: myGame.Color.Red,</div>
<div class="line"> path: [</div>
<div class="line"> new myGame.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),</div>
<div class="line"> new myGame.Vec3ObjectBuilder(x: 4.0, y: 5.0, z: 6.0)</div>
<div class="line"> ]);</div>
<div class="line"> </div>
<div class="line">final int orc = orcBuilder.finish(builder);</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Create our monster by using Start() andEnd()</div>
<div class="line">monster.Start(builder)</div>
<div class="line">monster.AddPos(builder, vec3.CreateVec3(builder, 1.0, 2.0, 3.0))</div>
<div class="line">monster.AddHp(builder, 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)</div>
<div class="line">monster.AddPath(builder, path)</div>
<div class="line">local orc = monster.End(builder)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let orc = MyGame_Sample_MonsterBuilder { b }</div>
<div class="line"> .start()</div>
<div class="line"> .add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))</div>
<div class="line"> .add_hp(300)</div>
<div class="line"> .add_name(name)</div>
<div class="line"> .add_inventory(inv)</div>
<div class="line"> .add_color(MyGame_Sample_Color_Red)</div>
<div class="line"> .add_weapons(weapons)</div>
<div class="line"> .add_equipped_type(MyGame_Sample_Equipment_Weapon)</div>
<div class="line"> .add_equipped(weapon_offsets[1])</div>
<div class="line"> .add_path(path)</div>
<div class="line"> .end()</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Create the monster using the `Monster::create` helper function. This</div>
<div class="line">// function accepts a `MonsterArgs` struct, which supplies all of the data</div>
<div class="line">// needed to build a `Monster`. To supply empty/default fields, just use the</div>
<div class="line">// Rust built-in `Default::default()` function, as demonstrated below.</div>
<div class="line">let orc = Monster::create(&amp;mut builder, &amp;MonsterArgs{</div>
<div class="line"> pos: Some(&amp;Vec3::new(1.0f32, 2.0f32, 3.0f32)),</div>
<div class="line"> mana: 150,</div>
<div class="line"> hp: 80,</div>
<div class="line"> name: Some(name),</div>
<div class="line"> inventory: Some(inventory),</div>
<div class="line"> color: Color::Red,</div>
<div class="line"> weapons: Some(weapons),</div>
<div class="line"> equipped_type: Equipment::Weapon,</div>
<div class="line"> equipped: Some(axe.as_union_value()),</div>
<div class="line"> path: Some(path),</div>
<div class="line"> ..Default::default()</div>
<div class="line">});</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">let orc = Monster.createMonster(</div>
<div class="line"> &amp;builder,</div>
<div class="line"> pos: MyGame_Sample_Vec3(x: 1, y: 2, z: 3),</div>
<div class="line"> hp: 300,</div>
<div class="line"> nameOffset: name,</div>
<div class="line"> inventoryVectorOffset: inventoryOffset,</div>
<div class="line"> color: .red,</div>
<div class="line"> weaponsVectorOffset: weaponsOffset,</div>
<div class="line"> equippedType: .weapon,</div>
<div class="line"> equippedOffset: axe)</div>
</div><!-- fragment --> </div><p>Note how we create <code>Vec3</code> struct in-line in the table. Unlike tables, structs are simple combinations of scalars that are always stored inline, just like scalars themselves.</p>
<p><b>Important</b>: Unlike structs, you should not nest tables or other objects, which is why we created all the strings/vectors/tables that this monster refers to before <code>start</code>. If you try to create any of them between <code>start</code> and <code>end</code>, you will get an assert/exception/panic depending on your language.</p>
<p><em>Note: Since we are 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 about 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></p>
<div class="language-cpp"> <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 />
<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(&amp;position);</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_equipped(axe.Union());</div>
<div class="line"><span class="keyword">auto</span> orc = monster_builder.Finish();</div>
</div><!-- fragment --> </div> <div class="language-c"> 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>create_monster_as_root()</code>. The following snippet is functionally equivalent to the above code, but provides a bit more flexibility. <br />
<div class="fragment"><div class="line"><span class="comment">// It is important to pair `start_as_root` with `end_as_root`.</span></div>
<div class="line">ns(Monster_start_as_root(B));</div>
<div class="line">ns(Monster_pos_create(B, 1.0f, 2.0f, 3.0f));</div>
<div class="line"><span class="comment">// or alternatively</span></div>
<div class="line"><span class="comment">//ns(Monster_pos_add(&amp;pos);</span></div>
<div class="line"> </div>
<div class="line">ns(Monster_hp_add(B, hp));</div>
<div class="line"><span class="comment">// Notice that `Monser_name_add` adds a string reference unlike the</span></div>
<div class="line"><span class="comment">// add_str and add_strn variants.</span></div>
<div class="line">ns(Monster_name_add(B, name));</div>
<div class="line">ns(Monster_inventory_add(B, inventory));</div>
<div class="line">ns(Monster_color_add(B, ns(Color_Red)));</div>
<div class="line">ns(Monster_weapons_add(B, weapons));</div>
<div class="line">ns(Monster_equipped_add(B, equipped));</div>
<div class="line"><span class="comment">// Complete the monster object and make it the buffer root object.</span></div>
<div class="line">ns(Monster_end_as_root(B));</div>
</div><!-- fragment --> </div><div class="language-swift"> <div class="fragment"><div class="line">let start = Monster.startMonster(&amp;builder)</div>
<div class="line">Monster.add(pos: Vec3(x: 1, y: 2, z: 3), &amp;builder)</div>
<div class="line">Monster.add(hp: 300, &amp;builder)</div>
<div class="line">Monster.add(name: name, &amp;builder)</div>
<div class="line">Monster.addVectorOf(inventory: inventoryOffset, &amp;builder)</div>
<div class="line">Monster.add(color: .red, &amp;builder)</div>
<div class="line">Monster.addVectorOf(weapons: weaponsOffset, &amp;builder)</div>
<div class="line">Monster.add(equippedType: .weapon, &amp;builder)</div>
<div class="line">Monster.add(equipped: axe, &amp;builder)</div>
<div class="line">var orc = Monster.endMonster(&amp;builder, start: start)</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 of 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.Union()); <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-kotlin"> <div class="fragment"><div class="line">Monster.addEquippedType(builder, Equipment.Weapon) // Union type</div>
<div class="line">Monster.addEquipped(axe) // Union data</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) // Union type</div>
<div class="line">sample.MonsterAddEquipped(builder, axe) // Union data</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">MyGame.Sample.Monster.AddEquippedType( <span class="comment"># Union type</span></div>
<div class="line"> builder, MyGame.Sample.Equipment.Equipment().Weapon)</div>
<div class="line">MyGame.Sample.Monster.AddEquipped(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); // Union type</div>
<div class="line">MyGame.Sample.Monster.addEquipped(builder, axe); // Union data</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon); // Union type</div>
<div class="line">MyGame.Sample.Monster.addEquipped(builder, axe); // Union data</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> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Add union type and data simultaneously.</span></div>
<div class="line">ns(Monster_equipped_Weapon_add(B, axe));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// using the builder API:</div>
<div class="line">..addEquippedType(myGame.EquipmentTypeId.Weapon)</div>
<div class="line">..addEquippedOffset(axe)</div>
<div class="line"> </div>
<div class="line">// in the ObjectBuilder API:</div>
<div class="line">equippedTypeId: myGame.EquipmentTypeId.Weapon, // Union type</div>
<div class="line">equipped: axe, // Union data</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">monster.AddEquippedType(builder, equipment.Weapon) -- Union type</div>
<div class="line">monster.AddEquipped(builder, axe) -- Union data</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">.add_equipped_type(MyGame_Sample_Equipment_Weapon)</div>
<div class="line">.add_equipped(axe)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// You need to call `as_union_value` to turn an object into a type that</div>
<div class="line">// can be used as a union value.</div>
<div class="line">monster_builder.add_equipped_type(Equipment::Weapon); // Union type</div>
<div class="line">monster_builder.add_equipped(axe.as_union_value()); // Union data</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">Monster.add(equippedType: .weapon, builder) // Type of union</div>
<div class="line">Monster.add(equipped: axe, builder) // Union data</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, 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-kotlin"> <div class="fragment"><div class="line">// Call `finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.finish(orc) // You could also call `Monster.finishMonsterBuffer(builder, orc);`.</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">// Call `Finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.Finish(orc)</div>
</div><!-- fragment --> </div> <div class="language-python"> <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-javascript"> <div class="fragment"><div class="line">// Call `finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder, orc);`.</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// Call `finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder, orc);`.</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-&gt;finish($orc); <span class="comment">// You may also call `\MyGame\Sample\Monster::FinishMonsterBuffer($builder, $orc);`.</span></div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Because we used `Monster_create_as_root`, we do not need a `finish` call in C`.</span></div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// Call `finish()` to instruct the builder that this monster is complete.</div>
<div class="line">// See the next code section, as in Dart `finish` will also return the byte array.</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Call &#39;Finish()&#39; to instruct the builder that this monster is complete.</div>
<div class="line">builder:Finish(orc)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">// Call `Finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.Finish(orc)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Call `finish()` to instruct the builder that this monster is complete.</div>
<div class="line">builder.finish(orc, None);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Call `finish(offset:)` to instruct the builder that this monster is complete.</div>
<div class="line">builder.finish(offset: orc)</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 class="line"><span class="comment">// The data in this ByteBuffer does NOT start at 0, but at buf.position().</span></div>
<div class="line"><span class="comment">// The number of bytes is buf.remaining().</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Alternatively this copies the above data out of the ByteBuffer for you:</span></div>
<div class="line"><span class="keywordtype">byte</span>[] buf = builder.sizedByteArray();</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">// This must be called after `finish()`.</div>
<div class="line">val buf = builder.dataBuffer()</div>
<div class="line">// The data in this ByteBuffer does NOT start at 0, but at buf.position().</div>
<div class="line">// The number of bytes is buf.remaining().</div>
<div class="line"> </div>
<div class="line">// Alternatively this copies the above data out of the ByteBuffer for you:</div>
<div class="line">val buf = builder.sizedByteArray()</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 class="line"><span class="comment">// The data in this ByteBuffer does NOT start at 0, but at buf.Position.</span></div>
<div class="line"><span class="comment">// The end of the data is marked by buf.Length, so the size is</span></div>
<div class="line"><span class="comment">// buf.Length - buf.Position.</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Alternatively this copies the above data out of the ByteBuffer for you:</span></div>
<div class="line"><span class="keywordtype">byte</span>[] buf = builder.SizedByteArray();</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">// This must be called after `Finish()`.</div>
<div class="line">buf := builder.FinishedBytes() // Of type `byte[]`.</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><span class="comment"># This must be called after `Finish()`.</span></div>
<div class="line">buf = builder.Output() // Of type `bytearray`.</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// This must be called after `finish()`.</div>
<div class="line">var buf = builder.asUint8Array(); // Of type `Uint8Array`.</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// This must be called after `finish()`.</div>
<div class="line">let buf = builder.asUint8Array(); // Of type `Uint8Array`.</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-&gt;dataBuffer(); <span class="comment">// Of type `Google\FlatBuffers\ByteBuffer`</span></div>
<div class="line"><span class="comment">// The data in this ByteBuffer does NOT start at 0, but at buf-&gt;getPosition().</span></div>
<div class="line"><span class="comment">// The end of the data is marked by buf-&gt;capacity(), so the size is</span></div>
<div class="line"><span class="comment">// buf-&gt;capacity() - buf-&gt;getPosition().</span></div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">uint8_t *buf;</div>
<div class="line"><span class="keywordtype">size_t</span> size;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Allocate and extract a readable buffer from internal builder heap.</span></div>
<div class="line"><span class="comment">// The returned buffer must be deallocated using `free`.</span></div>
<div class="line"><span class="comment">// NOTE: Finalizing the buffer does NOT change the builder, it</span></div>
<div class="line"><span class="comment">// just creates a snapshot of the builder content.</span></div>
<div class="line">buf = flatcc_builder_finalize_buffer(B, &amp;size);</div>
<div class="line"><span class="comment">// use buf</span></div>
<div class="line">free(buf);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Optionally reset builder to reuse builder without deallocating</span></div>
<div class="line"><span class="comment">// internal stack and heap.</span></div>
<div class="line">flatcc_builder_reset(B);</div>
<div class="line"><span class="comment">// build next buffer.</span></div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Cleanup.</span></div>
<div class="line">flatcc_builder_clear(B);</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">final Uint8List buf = builder.finish(orc);</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- Get the flatbuffer as a string containing the binary data</div>
<div class="line">local bufAsString = builder:Output()</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">// This must be called after `Finish()`.</div>
<div class="line">let buf = builder.SizedCopy() // Of type `string`.</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// This must be called after `finish()`.</div>
<div class="line">// `finished_data` returns a byte slice.</div>
<div class="line">let buf = builder.finished_data(); // Of type `&amp;[u8]`</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// This must be called after `finish()`.</div>
<div class="line">// `sizedByteArray` returns the finished buf of type [UInt8].</div>
<div class="line">let buf = builder.sizedByteArray</div>
<div class="line">// or you can use to get an object of type Data</div>
<div class="line">let bufData = ByteBuffer(data: builder.data)</div>
</div><!-- fragment --> </div><p>Now you can write the bytes to a file or send them over the network. <b>Make sure your file mode (or transfer protocol) is set to BINARY, not text.</b> If you transfer a FlatBuffer in text mode, the buffer will be corrupted, which will lead to hard to find problems when you read the buffer.</p>
<div class="language-javascript"> For example, in Node you can simply do: <div class="fragment"><div class="line">writeFileSync(&#39;monster.bin&#39;, buf, &#39;binary&#39;);</div>
</div><!-- fragment --> </div> <div class="language-typescript"> For example, in Node you can simply do: <div class="fragment"><div class="line">writeFileSync(&#39;monster.bin&#39;, buf, &#39;binary&#39;);</div>
</div><!-- fragment --> </div><h3><a class="anchor" id="autotoc_md186"></a>
Reading Orc FlatBuffers</h3>
<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 access a FlatBuffer.</p>
<p>This section 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 &quot;monster_generated.h&quot;</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> com.google.flatbuffers.FlatBufferBuilder;</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">import MyGame.Sample.* //The `flatc` generated files. (Monster, Vec3, etc.)</div>
<div class="line"> </div>
<div class="line">import com.google.flatbuffers.FlatBufferBuilder</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="keyword">using</span> <a class="code" href="namespace_flat_buffers.html">FlatBuffers</a>;</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">import (</div>
<div class="line"> flatbuffers &quot;github.com/google/flatbuffers/go&quot;</div>
<div class="line"> sample &quot;MyGame/Sample&quot;</div>
<div class="line">)</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line"><span class="keyword">import</span> flatbuffers</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Generated by `flatc`.</span></div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Any</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Color</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Monster</div>
<div class="line"><span class="keyword">import</span> MyGame.Sample.Vec3</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// The following code is an example - use your desired module flavor by transpiling from TS. </div>
<div class="line">var flatbuffers = require(&#39;/js/flatbuffers&#39;).flatbuffers;</div>
<div class="line">var MyGame = require(&#39;./monster_generated&#39;).MyGame; // Generated by `flatc`.</div>
<div class="line"> </div>
<div class="line">//--------------------------------------------------------------------------//</div>
<div class="line"> </div>
<div class="line">// The following code an example for browser-based HTML/JavaScript. Use the above code</div>
<div class="line">// for JavaScript module loaders (e.g. Node.js).</div>
<div class="line">&lt;script src=&quot;../js/flatbuffers.js&quot;&gt;&lt;/script&gt;</div>
<div class="line">&lt;script src=&quot;monster_generated.js&quot;&gt;&lt;/script&gt; // Generated by `flatc`.</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// note: import flatbuffers with your desired import method</div>
<div class="line"> </div>
<div class="line">// note: the `./monster_generated.ts` file was previously generated by `flatc` above using the `monster.fbs` schema</div>
<div class="line">import { MyGame } from &#39;./monster_generated&#39;;</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">&quot;\\&quot;</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">&quot;php&quot;</span>)),</div>
<div class="line"> join(DIRECTORY_SEPARATOR, array($root_dir, <span class="stringliteral">&quot;samples&quot;</span>, <span class="stringliteral">&quot;MyGame&quot;</span>, <span class="stringliteral">&quot;Sample&quot;</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">&quot;.php&quot;</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> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Only needed if we don&#39;t have `#include &quot;monster_builder.h&quot;`.</span></div>
<div class="line"><span class="preprocessor">#include &quot;monster_reader.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#undef ns</span></div>
<div class="line"><span class="preprocessor">#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x) </span><span class="comment">// Specified in the schema.</span></div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">import &#39;package:flat_buffers/flat_buffers.dart&#39; as fb;</div>
<div class="line">import &#39;./monster_my_game.sample_generated.dart&#39; as myGame;</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">-- require the flatbuffers module</div>
<div class="line">local flatbuffers = require(&quot;flatbuffers&quot;)</div>
<div class="line"> </div>
<div class="line">-- require the generated files from `flatc`.</div>
<div class="line">local color = require(&quot;MyGame.Sample.Color&quot;)</div>
<div class="line">local equipment = require(&quot;MyGame.Sample.Equipment&quot;)</div>
<div class="line">local monster = require(&quot;MyGame.Sample.Monster&quot;)</div>
<div class="line">local vec3 = require(&quot;MyGame.Sample.Vec3&quot;)</div>
<div class="line">local weapon = require(&quot;MyGame.Sample.Weapon&quot;)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">import from &quot;../lobster/&quot; // Where to find flatbuffers.lobster</div>
<div class="line">import monster_generated</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// import the flatbuffers runtime library</div>
<div class="line">extern crate flatbuffers;</div>
<div class="line"> </div>
<div class="line">// import the generated code</div>
<div class="line">#[allow(dead_code, unused_imports)]</div>
<div class="line">#[path = &quot;./monster_generated.rs&quot;]</div>
<div class="line">mod monster_generated;</div>
<div class="line">pub use monster_generated::my_game::sample::{get_root_as_monster,</div>
<div class="line"> Color, Equipment,</div>
<div class="line"> Monster, MonsterArgs,</div>
<div class="line"> Vec3,</div>
<div class="line"> Weapon, WeaponArgs};</div>
</div><!-- fragment --> </div><p>Then, assuming you have a buffer of bytes received from disk, network, etc., you can start accessing the buffer like so:</p>
<p><b>Again, make sure you read the bytes in BINARY mode, otherwise the code below won't work.</b></p>
<div class="language-cpp"> <div class="fragment"><div class="line">uint8_t *buffer_pointer = <span class="comment">/* the data you just read */</span>;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Get a pointer to the root object inside 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 *`.</span></div>
<div class="line"><span class="comment">// Note: root object pointers are NOT the same as `buffer_pointer`.</span></div>
<div class="line"><span class="comment">// `GetMonster` is a convenience function that calls `GetRoot&lt;Monster&gt;`,</span></div>
<div class="line"><span class="comment">// the latter is also available for non-root types.</span></div>
</div><!-- fragment --> </div> <div class="language-java"> <div class="fragment"><div class="line"><span class="keywordtype">byte</span>[] bytes = <span class="comment">/* the data you just read */</span></div>
<div class="line">java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(bytes);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Get an accessor to the root object inside the buffer.</span></div>
<div class="line">Monster monster = Monster.getRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">val bytes = /* the data you just read */</div>
<div class="line">val buf = java.nio.ByteBuffer.wrap(bytes)</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</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="keywordtype">byte</span>[] bytes = <span class="comment">/* the data you just read */</span></div>
<div class="line">var buf = <span class="keyword">new</span> ByteBuffer(bytes);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Get an accessor to the root object inside 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">var buf []byte = /* the data you just read */</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">monster := sample.GetRootAsMonster(buf, 0)</div>
<div class="line"> </div>
<div class="line">// Note: We use `0` for the offset here, which is typical for most buffers</div>
<div class="line">// you would read. If you wanted to read from `builder.Bytes` directly, you</div>
<div class="line">// would need to pass in the offset of `builder.Head()`, as the builder</div>
<div class="line">// constructs the buffer backwards, so may not start at offset 0.</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">buf = /* the data you just read, <span class="keywordflow">in</span> an object of type <span class="stringliteral">&quot;bytearray&quot;</span> */</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">monster = MyGame.Sample.Monster.Monster.GetRootAs(buf, 0)</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Note: We use `0` for the offset here, which is typical for most buffers</span></div>
<div class="line"><span class="comment"># you would read. If you wanted to read from the `builder.Bytes` directly,</span></div>
<div class="line"><span class="comment"># you would need to pass in the offset of `builder.Head()`, as the builder</span></div>
<div class="line"><span class="comment"># constructs the buffer backwards, so may not start at offset 0.</span></div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">// the data you just read, as a `Uint8Array`</div>
<div class="line">// Note that the example here uses `readFileSync` from the built-in `fs` module,</div>
<div class="line">// but other methods for accessing the file contents will also work.</div>
<div class="line">var bytes = new Uint8Array(readFileSync(&#39;./monsterdata.bin&#39;));</div>
<div class="line"> </div>
<div class="line">var buf = new flatbuffers.ByteBuffer(bytes);</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">var monster = MyGame.Sample.Monster.getRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">// the data you just read, as a `Uint8Array`.</div>
<div class="line">// Note that the example here uses `readFileSync` from the built-in `fs` module,</div>
<div class="line">// but other methods for accessing the file contents will also work.</div>
<div class="line">let bytes = new Uint8Array(readFileSync(&#39;./monsterdata.bin&#39;));</div>
<div class="line"> </div>
<div class="line">let buf = new flatbuffers.ByteBuffer(bytes);</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">let monster = MyGame.Sample.Monster.getRootAsMonster(buf);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$bytes = <span class="comment">/* the data you just read, in a string */</span></div>
<div class="line">$buf = Google\FlatBuffers\ByteBuffer::wrap($bytes);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Get an accessor to the root object inside the buffer.</span></div>
<div class="line">$monster = \MyGame\Sample\Monster::GetRootAsMonster($buf);</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Note that we use the `table_t` suffix when reading a table object</span></div>
<div class="line"><span class="comment">// as opposed to the `ref_t` suffix used during the construction of</span></div>
<div class="line"><span class="comment">// the buffer.</span></div>
<div class="line">ns(Monster_table_t) monster = ns(Monster_as_root(buffer));</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Note: root object pointers are NOT the same as the `buffer` pointer.</span></div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">List&lt;int&gt; data = ... // the data, e.g. from file or network</div>
<div class="line">// A generated factory constructor that will read the data.</div>
<div class="line">myGame.Monster monster = new myGame.Monster(data);</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">local bufAsString = -- The data you just read in</div>
<div class="line"> </div>
<div class="line">-- Convert the string representation into binary array Lua structure</div>
<div class="line">local buf = flatbuffers.binaryArray.New(bufAsString)</div>
<div class="line"> </div>
<div class="line">-- Get an accessor to the root object insert the buffer</div>
<div class="line">local mon = monster.GetRootAsMonster(buf, 0)</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">buf = /* the data you just read, in a string */</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">let monster = MyGame_Sample_GetRootAsMonster(buf)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">let buf = /* the data you just read, in a &amp;[u8] */</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">let monster = get_root_as_monster(buf);</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// create a ByteBuffer(:) from an [UInt8] or Data()</div>
<div class="line">let buf = // Get your data</div>
<div class="line"> </div>
<div class="line">// Get an accessor to the root object inside the buffer.</div>
<div class="line">let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))</div>
</div><!-- fragment --> </div><p>If you look in the generated files from the schema compiler, 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-&gt;hp();</div>
<div class="line"><span class="keyword">auto</span> mana = monster-&gt;mana();</div>
<div class="line"><span class="keyword">auto</span> name = monster-&gt;name()-&gt;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-kotlin"> <div class="fragment"><div class="line">val hp = monster.hp</div>
<div class="line">val mana = monster.mana</div>
<div class="line">val name = monster.name</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line"><span class="comment">// For C#, unlike most other languages support by FlatBuffers, most values (except for</span></div>
<div class="line"><span class="comment">// vectors and unions) are available as properties instead of accessor 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()) // Note: `monster.Name()` returns a byte[].</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">hp = monster.Hp()</div>
<div class="line">mana = monster.Mana()</div>
<div class="line">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-typescript"> <div class="fragment"><div class="line">let hp = monster.hp();</div>
<div class="line">let mana = monster.mana();</div>
<div class="line">let name = monster.name();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$hp = $monster-&gt;getHp();</div>
<div class="line">$mana = $monster-&gt;getMana();</div>
<div class="line">$name = monster-&gt;getName();</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">uint16_t hp = ns(Monster_hp(monster));</div>
<div class="line">uint16_t mana = ns(Monster_mana(monster));</div>
<div class="line">flatbuffers_string_t name = ns(Monster_name(monster));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">// For Dart, unlike other languages support by FlatBuffers, most values</div>
<div class="line">// are available as properties instead of accessor methods.</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-lua"> <div class="fragment"><div class="line">local hp = mon:Hp()</div>
<div class="line">local mana = mon:Mana()</div>
<div class="line">local name = mon:Name()</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let hp = monster.hp</div>
<div class="line">let mana = monster.mana</div>
<div class="line">let name = monster.name</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Get and test some scalar types from the FlatBuffer.</div>
<div class="line">let hp = monster.hp();</div>
<div class="line">let mana = monster.mana();</div>
<div class="line">let name = monster.name();</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">let hp = monster.hp</div>
<div class="line">let mana = monster.mana</div>
<div class="line">let name = monster.name // returns an optional string</div>
</div><!-- fragment --> </div><p>These should hold <code>300</code>, <code>150</code>, and <code>"Orc"</code> respectively.</p>
<p><em>Note: The default value <code>150</code> wasn't stored in <code>mana</code>, but we are still able to retrieve it.</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-&gt;pos();</div>
<div class="line"><span class="keyword">auto</span> x = pos-&gt;x();</div>
<div class="line"><span class="keyword">auto</span> y = pos-&gt;y();</div>
<div class="line"><span class="keyword">auto</span> z = pos-&gt;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-kotlin"> <div class="fragment"><div class="line">val pos = monster.pos!!</div>
<div class="line">val x = pos.x</div>
<div class="line">val y = pos.y</div>
<div class="line">val z = pos.z</div>
</div><!-- fragment --> </div> <div class="language-csharp"> <div class="fragment"><div class="line">var pos = monster.Pos.Value;</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">// Note: Whenever you access a new object, like in `Pos()`, a new temporary</div>
<div class="line">// accessor object gets created. If your code is very performance sensitive,</div>
<div class="line">// you can pass in a pointer to an existing `Vec3` instead of `nil`. This</div>
<div class="line">// allows you to reuse it across many calls to reduce the amount of object</div>
<div class="line">// allocation/garbage collection.</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">pos = monster.Pos()</div>
<div class="line">x = pos.X()</div>
<div class="line">y = pos.Y()</div>
<div class="line">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-typescript"> <div class="fragment"><div class="line">let pos = monster.pos();</div>
<div class="line">let x = pos.x();</div>
<div class="line">let y = pos.y();</div>
<div class="line">let z = pos.z();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$pos = $monster-&gt;getPos();</div>
<div class="line">$x = $pos-&gt;getX();</div>
<div class="line">$y = $pos-&gt;getY();</div>
<div class="line">$z = $pos-&gt;getZ();</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">ns(Vec3_struct_t) pos = ns(Monster_pos(monster));</div>
<div class="line"><span class="keywordtype">float</span> x = ns(Vec3_x(pos));</div>
<div class="line"><span class="keywordtype">float</span> y = ns(Vec3_y(pos));</div>
<div class="line"><span class="keywordtype">float</span> z = ns(Vec3_z(pos));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">myGame.Vec3 pos = monster.pos;</div>
<div class="line">double x = pos.x;</div>
<div class="line">double y = pos.y;</div>
<div class="line">double z = pos.z;</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">local pos = mon:Pos()</div>
<div class="line">local x = pos:X()</div>
<div class="line">local y = pos:Y()</div>
<div class="line">local z = pos:Z()</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let pos = monster.pos</div>
<div class="line">let x = pos.x</div>
<div class="line">let y = pos.y</div>
<div class="line">let z = pos.z</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">let pos = monster.pos().unwrap();</div>
<div class="line">let x = pos.x();</div>
<div class="line">let y = pos.y();</div>
<div class="line">let z = pos.z();</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">let pos = monster.pos</div>
<div class="line">let x = pos.x</div>
<div class="line">let y = pos.y</div>
<div class="line">let z = pos.z</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 a <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-&gt;inventory(); <span class="comment">// A pointer to a `flatbuffers::Vector&lt;&gt;`.</span></div>
<div class="line"><span class="keyword">auto</span> inv_len = inv-&gt;size();</div>
<div class="line"><span class="keyword">auto</span> third_item = inv-&gt;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"><span class="keywordtype">byte</span> thirdItem = monster.inventory(2);</div>
</div><!-- fragment --> </div> <div class="language-kotlin"> <div class="fragment"><div class="line">val invLength = monster.inventoryLength</div>
<div class="line">val 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.Inventory(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">inv_len = monster.InventoryLength()</div>
<div class="line">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-typescript"> <div class="fragment"><div class="line">let invLength = monster.inventoryLength();</div>
<div class="line">let thirdItem = monster.inventory(2);</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$inv_len = $monster-&gt;getInventoryLength();</div>
<div class="line">$third_item = $monster-&gt;getInventory(2);</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// If `inv` hasn&#39;t been set, it will be null. It is valid get</span></div>
<div class="line"><span class="comment">// the length of null which will be 0, useful for iteration.</span></div>
<div class="line">flatbuffers_uint8_vec_t inv = ns(Monster_inventory(monster));</div>
<div class="line"><span class="keywordtype">size_t</span> inv_len = flatbuffers_uint8_vec_len(inv);</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">int invLength = monster.inventory.length;</div>
<div class="line">var thirdItem = monster.inventory[2];</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">local invLength = mon:InventoryLength()</div>
<div class="line">local thirdItem = mon:Inventory(3) -- Lua is 1-based</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let inv_len = monster.inventory_length</div>
<div class="line">let third_item = monster.inventory(2)</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Get and test an element from the `inventory` FlatBuffer&#39;s `vector`.</div>
<div class="line">let inv = monster.inventory().unwrap();</div>
<div class="line"> </div>
<div class="line">// Note that this vector is returned as a slice, because direct access for</div>
<div class="line">// this type, a `u8` vector, is safe on all platforms:</div>
<div class="line">let third_item = inv[2];</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Get a the count of objects in the vector</div>
<div class="line">let count = monster.inventoryCount</div>
<div class="line"> </div>
<div class="line">// get item at index 4</div>
<div class="line">let object = monster.inventory(at: 4)</div>
<div class="line"> </div>
<div class="line">// or you can fetch the entire array</div>
<div class="line">let inv = monster.inventory</div>
<div class="line">// inv[4] should equal object</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 you 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-&gt;weapons(); <span class="comment">// A pointer to a `flatbuffers::Vector&lt;&gt;`.</span></div>
<div class="line"><span class="keyword">auto</span> weapon_len = weapons-&gt;size();</div>
<div class="line"><span class="keyword">auto</span> second_weapon_name = weapons-&gt;Get(1)-&gt;name()-&gt;str();</div>
<div class="line"><span class="keyword">auto</span> second_weapon_damage = weapons-&gt;Get(1)-&gt;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-kotlin"> <div class="fragment"><div class="line">val weaponsLength = monster.weaponsLength</div>
<div class="line">val secondWeaponName = monster.weapons(1)!!.name</div>
<div class="line">val 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.Weapons(1).Name;</div>
<div class="line">var secondWeaponDamage = monster.Weapons(1).Damage;</div>
</div><!-- fragment --> </div> <div class="language-go"> <div class="fragment"><div class="line">weaponLength := monster.WeaponsLength()</div>
<div class="line">weapon := new(sample.Weapon) // We need a `sample.Weapon` to pass into `monster.Weapons()`</div>
<div class="line"> // to capture the output of the function.</div>
<div class="line">if 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">weapons_length = monster.WeaponsLength()</div>
<div class="line">second_weapon_name = monster.Weapons(1).Name()</div>
<div class="line">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-typescript"> <div class="fragment"><div class="line">let weaponsLength = monster.weaponsLength();</div>
<div class="line">let secondWeaponName = monster.weapons(1).name();</div>
<div class="line">let secondWeaponDamage = monster.weapons(1).damage();</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$weapons_len = $monster-&gt;getWeaponsLength();</div>
<div class="line">$second_weapon_name = $monster-&gt;getWeapons(1)-&gt;getName();</div>
<div class="line">$second_weapon_damage = $monster-&gt;getWeapons(1)-&gt;getDamage();</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">ns(Weapon_vec_t) weapons = ns(Monster_weapons(monster));</div>
<div class="line"><span class="keywordtype">size_t</span> weapons_len = ns(Weapon_vec_len(weapons));</div>
<div class="line"><span class="comment">// We can use `const char *` instead of `flatbuffers_string_t`.</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> *second_weapon_name = ns(Weapon_name(ns(Weapon_vec_at(weapons, 1))));</div>
<div class="line">uint16_t second_weapon_damage = ns(Weapon_damage(ns(Weapon_vec_at(weapons, 1))));</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">int weaponsLength = monster.weapons.length;</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-lua"> <div class="fragment"><div class="line">local weaponsLength = mon:WeaponsLength()</div>
<div class="line">local secondWeaponName = mon:Weapon(2):Name()</div>
<div class="line">local secondWeaponDamage = mon:Weapon(2):Damage()</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">let weapons_length = monster.weapons_length</div>
<div class="line">let second_weapon_name = monster.weapons(1).name</div>
<div class="line">let second_weapon_damage = monster.weapons(1).damage</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Get and test the `weapons` FlatBuffers&#39;s `vector`.</div>
<div class="line">let weps = monster.weapons().unwrap();</div>
<div class="line">let weps_len = weps.len();</div>
<div class="line"> </div>
<div class="line">let wep2 = weps.get(1);</div>
<div class="line">let second_weapon_name = wep2.name();</div>
<div class="line">let second_weapon_damage = wep2.damage();</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Get the count of weapon objects</div>
<div class="line">let wepsCount = monster.weaponsCount</div>
<div class="line"> </div>
<div class="line">let weapon2 = monster.weapons(at: 1)</div>
<div class="line">let weaponName = weapon2.name</div>
<div class="line">let weaponDmg = weapon2.damage</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&lt;</span><span class="keyword">const </span>Weapon*<span class="keyword">&gt;</span>(monster-&gt;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-&gt;name()-&gt;str(); <span class="comment">// &quot;Axe&quot;</span></div>
<div class="line"> <span class="keyword">auto</span> weapon_damage = weapon-&gt;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">// &quot;Axe&quot;</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-kotlin"> <div class="fragment"><div class="line">val unionType = monster.EquippedType</div>
<div class="line"> </div>
<div class="line">if (unionType == Equipment.Weapon) {</div>
<div class="line"> val weapon = monster.equipped(Weapon()) as Weapon // Requires explicit cast</div>
<div class="line"> // to `Weapon`.</div>
<div class="line"> </div>
<div class="line"> val weaponName = weapon.name // &quot;Axe&quot;</div>
<div class="line"> val weaponDamage = weapon.damage // 5</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 = monster.Equipped&lt;Weapon&gt;().Value;</div>
<div class="line"> </div>
<div class="line"> var weaponName = weapon.Name; <span class="comment">// &quot;Axe&quot;</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">// We need a `flatbuffers.Table` to capture the output of the</div>
<div class="line">// `monster.Equipped()` function.</div>
<div class="line">unionTable := new(flatbuffers.Table)</div>
<div class="line"> </div>
<div class="line">if monster.Equipped(unionTable) {</div>
<div class="line"> unionType := monster.EquippedType()</div>
<div class="line"> </div>
<div class="line"> if unionType == sample.EquipmentWeapon {</div>
<div class="line"> // Create a `sample.Weapon` object that can be initialized with the contents</div>
<div class="line"> // of the `flatbuffers.Table` (`unionTable`), which was populated by</div>
<div class="line"> // `monster.Equipped()`.</div>
<div class="line"> unionWeapon = new(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">union_type = monster.EquippedType()</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">if</span> union_type == MyGame.Sample.Equipment.Equipment().Weapon:</div>
<div class="line"> <span class="comment"># `monster.Equipped()` returns a `flatbuffers.Table`, which can be used to</span></div>
<div class="line"> <span class="comment"># initialize a `MyGame.Sample.Weapon.Weapon()`.</span></div>
<div class="line"> union_weapon = MyGame.Sample.Weapon.Weapon()</div>
<div class="line"> union_weapon.Init(monster.Equipped().Bytes, monster.Equipped().Pos)</div>
<div class="line"> </div>
<div class="line"> weapon_name = union_weapon.Name() // <span class="stringliteral">&#39;Axe&#39;</span></div>
<div class="line"> 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">if (unionType == MyGame.Sample.Equipment.Weapon) {</div>
<div class="line"> var weaponName = monster.equipped(new MyGame.Sample.Weapon()).name(); // &#39;Axe&#39;</div>
<div class="line"> var weaponDamage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5</div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">let unionType = monster.equippedType();</div>
<div class="line"> </div>
<div class="line">if (unionType == MyGame.Sample.Equipment.Weapon) {</div>
<div class="line"> let weaponName = monster.equipped(new MyGame.Sample.Weapon()).name(); // &#39;Axe&#39;</div>
<div class="line"> let weaponDamage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5</div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">$union_type = $monster-&gt;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-&gt;getEquipped(<span class="keyword">new</span> \MyGame\Sample\Weapon())-&gt;getName(); <span class="comment">// &quot;Axe&quot;</span></div>
<div class="line"> $weapon_damage = $monster-&gt;getEquipped(<span class="keyword">new</span> \MyGame\Sample\Weapon())-&gt;getDamage(); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line"><span class="comment">// Access union type field.</span></div>
<div class="line"><span class="keywordflow">if</span> (ns(Monster_equipped_type(monster)) == ns(Equipment_Weapon)) {</div>
<div class="line"> <span class="comment">// Cast to appropriate type:</span></div>
<div class="line"> <span class="comment">// C allows for silent void pointer assignment, so we need no explicit cast.</span></div>
<div class="line"> ns(Weapon_table_t) weapon = ns(Monster_equipped(monster));</div>
<div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span> *weapon_name = ns(Weapon_name(weapon)); <span class="comment">// &quot;Axe&quot;</span></div>
<div class="line"> uint16_t weapon_damage = ns(Weapon_damage(weapon)); <span class="comment">// 5</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">var unionType = monster.equippedType.value;</div>
<div class="line"> </div>
<div class="line">if (unionType == myGame.EquipmentTypeId.Weapon.value) {</div>
<div class="line"> myGame.Weapon weapon = mon.equipped as myGame.Weapon;</div>
<div class="line"> </div>
<div class="line"> var weaponName = weapon.name; // &quot;Axe&quot;</div>
<div class="line"> var weaponDamage = weapon.damage; // 5</div>
<div class="line">}</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">local unionType = mon:EquippedType()</div>
<div class="line"> </div>
<div class="line">if unionType == equipment.Weapon then</div>
<div class="line"> local unionWeapon = weapon.New()</div>
<div class="line"> unionWeapon:Init(mon:Equipped().bytes, mon:Equipped().pos)</div>
<div class="line"> </div>
<div class="line"> local weaponName = unionWeapon:Name() -- &#39;Axe&#39;</div>
<div class="line"> local weaponDamage = unionWeapon:Damage() -- 5</div>
<div class="line">end</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">union_type = monster.equipped_type</div>
<div class="line"> </div>
<div class="line">if union_type == MyGame_Sample_Equipment_Weapon:</div>
<div class="line"> // `monster.equipped_as_Weapon` returns a FlatBuffer handle much like normal table fields,</div>
<div class="line"> // but this is only valid to call if we already know it is the correct type.</div>
<div class="line"> let union_weapon = monster.equipped_as_Weapon</div>
<div class="line"> </div>
<div class="line"> let weapon_name = union_weapon.name // &quot;Axe&quot;</div>
<div class="line"> let weapon_damage = union_weapon.damage // 5</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">// Get and test the `Equipment` union (`equipped` field).</div>
<div class="line">// `equipped_as_weapon` returns a FlatBuffer handle much like normal table</div>
<div class="line">// fields, but this will return `None` if the union is not actually of that</div>
<div class="line">// type.</div>
<div class="line">if monster.equipped_type() == Equipment::Weapon {</div>
<div class="line"> let equipped = monster.equipped_as_weapon().unwrap();</div>
<div class="line"> let weapon_name = equipped.name();</div>
<div class="line"> let weapon_damage = equipped.damage();</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">// Get and check if the monster has an equipped item</div>
<div class="line">if monster.equippedType == .weapon {</div>
<div class="line"> let _weapon = monster.equipped(type: Weapon.self)</div>
<div class="line"> let name = _weapon.name // should return &quot;Axe&quot;</div>
<div class="line"> let dmg = _weapon.damage // should return 5</div>
<div class="line">}</div>
</div><!-- fragment --> </div><h1><a class="anchor" id="autotoc_md187"></a>
Mutating FlatBuffers</h1>
<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-&gt;mutate_hp(10); <span class="comment">// Set the table `hp` field.</span></div>
<div class="line">monster-&gt;mutable_pos()-&gt;mutate_z(4); <span class="comment">// Set struct field.</span></div>
<div class="line">monster-&gt;mutable_inventory()-&gt;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-kotlin"> <div class="fragment"><div class="line">val monster = Monster.getRootAsMonster(buf)</div>
<div class="line">monster.mutateHp(10) // Set table field.</div>
<div class="line">monster.pos!!.mutateZ(4) // Set struct field.</div>
<div class="line">monster.mutateInventory(0, 1) // Set vector element.</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">&lt;API for mutating FlatBuffers is not yet available in Go.&gt;</div>
</div><!-- fragment --> </div> <div class="language-python"> <div class="fragment"><div class="line">&lt;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.&gt;</div>
</div><!-- fragment --> </div> <div class="language-javascript"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers is not yet supported in JavaScript.&gt;</div>
</div><!-- fragment --> </div> <div class="language-typescript"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers is not yet supported in TypeScript.&gt;</div>
</div><!-- fragment --> </div> <div class="language-php"> <div class="fragment"><div class="line">&lt;API <span class="keywordflow">for</span> mutating <a class="code" href="namespace_flat_buffers.html">FlatBuffers</a> is not yet supported in PHP.&gt;</div>
</div><!-- fragment --> </div> <div class="language-c"> <div class="fragment"><div class="line">&lt;API <span class="keywordflow">for</span> in-place mutating <a class="code" href="namespace_flat_buffers.html">FlatBuffers</a> will not be supported in C</div>
<div class="line">(except in-place vector sorting is possible).&gt;</div>
</div><!-- fragment --> </div> <div class="language-dart"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers not yet available in Dart.&gt;</div>
</div><!-- fragment --> </div> <div class="language-lua"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers is not yet available in Lua.&gt;</div>
</div><!-- fragment --> </div> <div class="language-lobster"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers is not yet available in Lobster.&gt;</div>
</div><!-- fragment --> </div> <div class="language-rust"> <div class="fragment"><div class="line">&lt;API for mutating FlatBuffers is not yet available in Rust.&gt;</div>
</div><!-- fragment --> </div> <div class="language-swift"> <div class="fragment"><div class="line">let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))</div>
<div class="line">monster.mutate(hp: 10) // mutates a value in a table</div>
<div class="line">/// to mutate structs in swift you have to use the mutable accessors</div>
<div class="line">monster.mutablePos.mutate(z: 4) // mutates a value in a struct</div>
<div class="line">monster.mutate(inventory: 6, at index: 0) // mutates a value in an Scalar array</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 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>
<p>If this is not sufficient, other ways of mutating FlatBuffers may be supported in your language through an object based API (<code>--gen-object-api</code>) or reflection. See the individual language documents for support.</p>
<h1><a class="anchor" id="autotoc_md188"></a>
Using &lt;tt&gt;flatc&lt;/tt&gt; as a JSON Conversion Tool</h1>
<p>If you are working with C, C++, or Lobster, you can parse JSON at runtime. If your language does not support JSON at the moment, <code>flatc</code> may provide an alternative. Using <code>flatc</code> is often the preferred method, 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>
<h3><a class="anchor" id="autotoc_md189"></a>
JSON to binary representation</h3>
<p>Let's 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>
<div class="fragment"><div class="line">{</div>
<div class="line"> &quot;pos&quot;: {</div>
<div class="line"> &quot;x&quot;: 1.0,</div>
<div class="line"> &quot;y&quot;: 2.0,</div>
<div class="line"> &quot;z&quot;: 3.0</div>
<div class="line"> },</div>
<div class="line"> &quot;hp&quot;: 300,</div>
<div class="line"> &quot;name&quot;: &quot;Orc&quot;,</div>
<div class="line"> &quot;weapons&quot;: [</div>
<div class="line"> {</div>
<div class="line"> &quot;name&quot;: &quot;axe&quot;,</div>
<div class="line"> &quot;damage&quot;: 100</div>
<div class="line"> },</div>
<div class="line"> {</div>
<div class="line"> &quot;name&quot;: &quot;bow&quot;,</div>
<div class="line"> &quot;damage&quot;: 90</div>
<div class="line"> }</div>
<div class="line"> ],</div>
<div class="line"> &quot;equipped_type&quot;: &quot;Weapon&quot;,</div>
<div class="line"> &quot;equipped&quot;: {</div>
<div class="line"> &quot;name&quot;: &quot;bow&quot;,</div>
<div class="line"> &quot;damage&quot;: 90</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --><p>You can run this file through the <code>flatc</code> compiler with the <code>-b</code> flag and our <code>monster.fbs</code> schema to produce a FlatBuffer binary file.</p>
<div class="fragment"><div class="line">./../flatc --binary monster.fbs monsterdata.json</div>
</div><!-- fragment --><p>The output of this will be a file <code>monsterdata.bin</code>, which will contain the FlatBuffer binary representation of the contents from our <code>.json</code> file.</p>
<div class="language-cpp"> <em>Note: If you're working in C++, you can also parse JSON at runtime. See the <a class="el" href="flatbuffers_guide_use_cpp.html">Use in C++</a> section of the Programmer's Guide for more information.</em> </div> <div class="language-c"> <em>Note: If you're working in C, the <code>flatcc --json</code> (not <code>flatc</code>) compiler will generate schema specific high performance json parsers and printers that you can compile and use at runtime. The <code>flatc</code> compiler (not <code>flatcc</code>) on the other hand, is still useful for general offline json to flatbuffer conversion from a given schema. There are no current plans for <code>flatcc</code> to support this.</em> </div> <div class="language-lobster"> <em>Note: If you're working in Lobster, you can also parse JSON at runtime. See the <a class="el" href="flatbuffers_guide_use_lobster.html">Use in Lobster</a> section of the Programmer's Guide for more information.</em> </div><h3><a class="anchor" id="autotoc_md190"></a>
FlatBuffer binary to JSON</h3>
<p>Converting from a FlatBuffer binary representation to JSON is supported as well: </p><div class="fragment"><div class="line">./../flatc --json --raw-binary monster.fbs -- monsterdata.bin</div>
</div><!-- fragment --><p> This will convert <code>monsterdata.bin</code> back to its original JSON representation. You need to pass the corresponding FlatBuffers schema so that flatc knows how to interpret the binary buffer. Since <code>monster.fbs</code> does not specify an explicit <code>file_identifier</code> for binary buffers, <code>flatc</code> needs to be forced into reading the <code>.bin</code> file using the <code>--raw-binary</code> option.</p>
<p>The FlatBuffer binary representation does not explicitly encode default values, therefore they are not present in the resulting JSON unless you specify <code>--defaults-json</code>.</p>
<p>If you intend to process the JSON with other tools, you may consider switching on <code>--strict-json</code> so that identifiers are quoted properly.</p>
<p><em>Note: The resulting JSON file is not necessarily identical with the original JSON. If the binary representation contains floating point numbers, floats and doubles are rounded to 6 and 12 digits, respectively, in order to represent them as decimals in the JSON document.</em></p>
<h1><a class="anchor" id="autotoc_md191"></a>
Advanced Features for Each Language</h1>
<p>Each language has a dedicated <code>Use in XXX</code> page in the Programmer's Guide to cover the nuances of FlatBuffers in that language.</p>
<p>For your chosen language, see:</p>
<div class="language-cpp"> <a class="el" href="flatbuffers_guide_use_cpp.html">Use in C++</a> </div> <div class="language-java"> <a class="el" href="flatbuffers_guide_use_java.html">Use in Java</a> </div> <div class="language-kotlin"> Use in Kotlin </div> <div class="language-csharp"> <a class="el" href="flatbuffers_guide_use_c-sharp.html">Use in C#</a> </div> <div class="language-go"> <a class="el" href="flatbuffers_guide_use_go.html">Use in Go</a> </div> <div class="language-python"> <a class="el" href="flatbuffers_guide_use_python.html">Use in Python</a> </div> <div class="language-javascript"> <a class="el" href="flatbuffers_guide_use_javascript.html">Use in JavaScript</a> </div> <div class="language-typescript"> <a class="el" href="flatbuffers_guide_use_typescript.html">Use in TypeScript</a> </div> <div class="language-php"> <a class="el" href="flatbuffers_guide_use_php.html">Use in PHP</a> </div> <div class="language-c"> <a class="el" href="flatbuffers_guide_use_c.html">Use in C</a> </div> <div class="language-dart"> <a class="el" href="flatbuffers_guide_use_dart.html">Use in Dart</a> </div> <div class="language-lua"> <a class="el" href="flatbuffers_guide_use_lua.html">Use in Lua</a> </div> <div class="language-lobster"> <a class="el" href="flatbuffers_guide_use_lobster.html">Use in Lobster</a> </div> <div class="language-rust"> <a class="el" href="flatbuffers_guide_use_rust.html">Use in Rust</a> </div> <div class="language-swift"> <a class="el" href="flatbuffers_guide_use_swift.html">Use in Swift</a> </div><p> <br />
</p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-49880327-7', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>