| 1 |
// Copyright 2018 Google Inc. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
import from "../lobster/" |
| 16 |
import monster_generated |
| 17 |
|
| 18 |
// Example of how to use FlatBuffers to create and read binary buffers. |
| 19 |
|
| 20 |
// Create a builder. |
| 21 |
let b = flatbuffers_builder {} |
| 22 |
|
| 23 |
// Create some weapons for our monster. |
| 24 |
let weapon_names = [ "Sword", "Axe" ] |
| 25 |
let weapon_damages = [ 3, 5 ] |
| 26 |
|
| 27 |
let weapon_offsets = map(weapon_names) name, i: |
| 28 |
let ns = b.CreateString(name) |
| 29 |
MyGame_Sample_WeaponBuilder { b } |
| 30 |
.start() |
| 31 |
.add_name(ns) |
| 32 |
.add_damage(weapon_damages[i]) |
| 33 |
.end() |
| 34 |
|
| 35 |
let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets) |
| 36 |
|
| 37 |
// Name of the monster. |
| 38 |
let name = b.CreateString("Orc") |
| 39 |
|
| 40 |
// Inventory. |
| 41 |
let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _) |
| 42 |
|
| 43 |
// Now pack it all together in our root monster object. |
| 44 |
let orc = MyGame_Sample_MonsterBuilder { b } |
| 45 |
.start() |
| 46 |
.add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0)) |
| 47 |
.add_hp(300) |
| 48 |
.add_name(name) |
| 49 |
.add_inventory(inv) |
| 50 |
.add_color(MyGame_Sample_Color_Red) |
| 51 |
.add_weapons(weapons) |
| 52 |
.add_equipped_type(MyGame_Sample_Equipment_Weapon) |
| 53 |
.add_equipped(weapon_offsets[1]) |
| 54 |
.end() |
| 55 |
|
| 56 |
// Finish the buffer! |
| 57 |
b.Finish(orc, "MONS") |
| 58 |
|
| 59 |
// We now have a FlatBuffer that we could store on disk or send over a network. |
| 60 |
|
| 61 |
let buf = b.SizedCopy() |
| 62 |
|
| 63 |
// ...Saving to file or sending over a network code goes here... |
| 64 |
|
| 65 |
// Instead, we are going to access this buffer right away (as if we just |
| 66 |
// received it). |
| 67 |
|
| 68 |
// Get the root object accessor. |
| 69 |
let monster = MyGame_Sample_GetRootAsMonster(buf) |
| 70 |
|
| 71 |
// Note: We did not set the `mana` field explicitly, so we get a default value. |
| 72 |
assert monster.mana == 150 |
| 73 |
assert monster.hp == 300 |
| 74 |
assert monster.name == "Orc" |
| 75 |
assert monster.color == MyGame_Sample_Color_Red |
| 76 |
let pos = monster.pos |
| 77 |
assert pos |
| 78 |
assert pos.x == 1.0 |
| 79 |
assert pos.y == 2.0 |
| 80 |
assert pos.z == 3.0 |
| 81 |
|
| 82 |
// Get and test the `inventory` FlatBuffer vector. |
| 83 |
for(monster.inventory_length) e, i: |
| 84 |
assert monster.inventory(i) == e |
| 85 |
|
| 86 |
// Get and test the `weapons` FlatBuffer vector of tables. |
| 87 |
for(monster.weapons_length) i: |
| 88 |
assert monster.weapons(i).name == weapon_names[i] |
| 89 |
assert monster.weapons(i).damage == weapon_damages[i] |
| 90 |
|
| 91 |
// Get and test the `equipped` FlatBuffer union. |
| 92 |
assert monster.equipped_type() == MyGame_Sample_Equipment_Weapon |
| 93 |
|
| 94 |
// Now that we know the union value is a weapon, we can safely call as_Weapon: |
| 95 |
let union_weapon = monster.equipped_as_Weapon |
| 96 |
|
| 97 |
assert union_weapon.name == "Axe" |
| 98 |
assert union_weapon.damage == 5 |
| 99 |
|
| 100 |
print "The FlatBuffer was successfully created and verified!" |