Please be aware of the difference between `flatc` and `flatcc` tools.
<br>
</div>
-
<div class="language-cpp">
~~~{.sh}
cd flatbuffers/samples
~~~
</div>
<div class="language-typescript">
+~~~{.ts}
// note: import flatbuffers with your desired import method
import { MyGame } from './monster_generated';
+~~~
</div>
<div class="language-php">
~~~{.php}
Weapon, WeaponArgs};
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
/**
~~~
</div>
<div class="language-typescript">
-~~~{.js}
+~~~{.ts}
let weaponOne = builder.createString('Sword');
let weaponTwo = builder.createString('Axe');
});
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
let weapon1Name = builder.create(string: "Sword")
~~~
</div>
<div class="language-typescript">
-~~~{.js}
+~~~{.ts}
// Serialize a name for our monster, called 'Orc'.
let name = builder.createString('Orc');
<div class="language-swift">
~~~{.swift}
//
- let points = builder.createVector(structs: [MyGame.Sample.createVec3(x: 1, y: 2, z: 3),
+ let points = builder.createVector(structs: [MyGame.Sample.createVec3(x: 1, y: 2, z: 3),
MyGame.Sample.createVec3(x: 4, y: 5, z: 6)],
type: Vec3.self)
~~~
</div>
<div class="language-swift">
~~~{.swift}
- let orc = Monster.createMonster(builder,
+ let orc = Monster.createMonster(builder,
offsetOfPos: pos,
hp: 300,
offsetOfName: name,
ns(Monster_end_as_root(B));
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
let start = Monster.startMonster(builder)
// Call `Finish()` to instruct the builder that this monster is complete.
// Note: Regardless of how you created the `orc`, you still need to call
// `Finish()` on the `FlatBufferBuilder`.
- builder.Finish(orc); // You could also call `FinishMonsterBuffer(builder,
- // orc);`.
+ builder.Finish(orc); // You could also call `FinishMonsterBuffer(builder, orc);`.
~~~
</div>
<div class="language-java">
<div class="language-javascript">
~~~{.js}
// Call `finish()` to instruct the builder that this monster is complete.
- builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder,
- // orc);`.
+ builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder, orc);`.
~~~
</div>
<div class="language-typescript">
~~~{.ts}
// Call `finish()` to instruct the builder that this monster is complete.
- builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder,
- // orc);`.
+ builder.finish(orc); // You could also call `MyGame.Sample.Monster.finishMonsterBuffer(builder, orc);`.
~~~
</div>
<div class="language-php">
~~~{.php}
// Call `finish()` to instruct the builder that this monster is complete.
- $builder->finish($orc); // You may also call `\MyGame\Sample\Monster::FinishMonsterBuffer(
- // $builder, $orc);`.
+ $builder->finish($orc); // You may also call `\MyGame\Sample\Monster::FinishMonsterBuffer($builder, $orc);`.
~~~
</div>
<div class="language-c">
let buf = builder.finished_data(); // Of type `&[u8]`
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
// This must be called after `finish()`.
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.
+<div class="language-javascript">
+For example, in Node you can simply do:
+~~~{.js}
+ writeFileSync('monster.bin', buf, 'binary');
+~~~
+</div>
+<div class="language-typescript">
+For example, in Node you can simply do:
+~~~{.ts}
+ writeFileSync('monster.bin', buf, 'binary');
+~~~
+</div>
+
#### Reading Orc FlatBuffers
Now that we have successfully created an `Orc` FlatBuffer, the monster data can
~~~
</div>
<div class="language-typescript">
-~~~{.js}
- // note: import flabuffers with your desired import method
+~~~{.ts}
+ // note: import flatbuffers with your desired import method
+ // note: the `./monster_generated.ts` file was previously generated by `flatc` above using the `monster.fbs` schema
import { MyGame } from './monster_generated';
~~~
</div>
</div>
<div class="language-javascript">
~~~{.js}
- var bytes = /* the data you just read, in an object of type "Uint8Array" */
+ // the data you just read, as a `Uint8Array`
+ // Note that the example here uses `readFileSync` from the built-in `fs` module,
+ // but other methods for accessing the file contents will also work.
+ var bytes = new Uint8Array(readFileSync('./monsterdata.bin'));
+
var buf = new flatbuffers.ByteBuffer(bytes);
// Get an accessor to the root object inside the buffer.
</div>
<div class="language-typescript">
~~~{.ts}
- let bytes = /* the data you just read, in an object of type "Uint8Array" */
+ // the data you just read, as a `Uint8Array`.
+ // Note that the example here uses `readFileSync` from the built-in `fs` module,
+ // but other methods for accessing the file contents will also work.
+ let bytes = new Uint8Array(readFileSync('./monsterdata.bin'));
+
let buf = new flatbuffers.ByteBuffer(bytes);
// Get an accessor to the root object inside the buffer.
let monster = get_root_as_monster(buf);
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
// create a ByteBuffer(:) from an [UInt8] or Data()
</div>
<div class="language-javascript">
~~~{.js}
- var hp = $monster.hp();
- var mana = $monster.mana();
- var name = $monster.name();
+ var hp = monster.hp();
+ var mana = monster.mana();
+ var name = monster.name();
~~~
</div>
<div class="language-typescript">
~~~{.ts}
- let hp = $monster.hp();
- let mana = $monster.mana();
- let name = $monster.name();
+ let hp = monster.hp();
+ let mana = monster.mana();
+ let name = monster.name();
~~~
</div>
<div class="language-php">
let name = monster.name();
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
let hp = monster.hp
let z = pos.z();
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
let pos = monster.pos
let third_item = inv[2];
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
// Get a the count of objects in the vector
var unionType = monster.equippedType();
if (unionType == MyGame.Sample.Equipment.Weapon) {
- var weapon_name = monster.equipped(new MyGame.Sample.Weapon()).name(); // 'Axe'
- var weapon_damage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5
+ var weaponName = monster.equipped(new MyGame.Sample.Weapon()).name(); // 'Axe'
+ var weaponDamage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5
}
~~~
</div>
let unionType = monster.equippedType();
if (unionType == MyGame.Sample.Equipment.Weapon) {
- let weapon_name = monster.equipped(new MyGame.Sample.Weapon()).name(); // 'Axe'
- let weapon_damage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5
+ let weaponName = monster.equipped(new MyGame.Sample.Weapon()).name(); // 'Axe'
+ let weaponDamage = monster.equipped(new MyGame.Sample.Weapon()).damage(); // 5
}
~~~
</div>
let weapon_damage = equipped.damage();
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
// Get and check if the monster has an equipped item
<API for mutating FlatBuffers is not yet available in Rust.>
~~~
</div>
-
<div class="language-swift">
~~~{.swift}
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
our `monster.fbs` schema to produce a FlatBuffer binary file.
~~~{.sh}
-./../flatc -b monster.fbs monsterdata.json
+./../flatc --binary monster.fbs monsterdata.json
~~~
The output of this will be a file `monsterdata.bin`, which will contain the