summaryrefslogtreecommitdiff
path: root/samples/sample_binary.lobster
blob: cd7adab2a2e66a8da02899d4952c1eb84d9773ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import from "../lobster/"
import monster_generated

// Example of how to use FlatBuffers to create and read binary buffers.

// Create a builder.
let b = flatbuffers_builder {}

// Create some weapons for our monster.
let weapon_names = [ "Sword", "Axe" ]
let weapon_damages = [ 3, 5 ]

let weapon_offsets = map(weapon_names) name, i:
    let ns = b.CreateString(name)
    MyGame_Sample_WeaponBuilder { b }
        .start()
        .add_name(ns)
        .add_damage(weapon_damages[i])
        .end()

let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets)

// Name of the monster.
let name = b.CreateString("Orc")

// Inventory.
let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _)

// Now pack it all together in our root monster object.
let orc = MyGame_Sample_MonsterBuilder { b }
    .start()
    .add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
    .add_hp(300)
    .add_name(name)
    .add_inventory(inv)
    .add_color(MyGame_Sample_Color_Red)
    .add_weapons(weapons)
    .add_equipped_type(MyGame_Sample_Equipment_Weapon)
    .add_equipped(weapon_offsets[1])
    .end()

// Finish the buffer!
b.Finish(orc)

// We now have a FlatBuffer that we could store on disk or send over a network.

let buf = b.SizedCopy()

// ...Saving to file or sending over a network code goes here...

// Instead, we are going to access this buffer right away (as if we just
// received it).

// Get the root object accessor.
let monster = MyGame_Sample_GetRootAsMonster(buf)

// Note: We did not set the `mana` field explicitly, so we get a default value.
assert monster.mana == 150
assert monster.hp == 300
assert monster.name == "Orc"
assert monster.color == MyGame_Sample_Color_Red
let pos = monster.pos
assert pos
assert pos.x == 1.0
assert pos.y == 2.0
assert pos.z == 3.0

// Get and test the `inventory` FlatBuffer vector.
for(monster.inventory_length) e, i:
  assert monster.inventory(i) == e

// Get and test the `weapons` FlatBuffer vector of tables.
for(monster.weapons_length) i:
  assert monster.weapons(i).name == weapon_names[i]
  assert monster.weapons(i).damage == weapon_damages[i]

// Get and test the `equipped` FlatBuffer union.
assert monster.equipped_type() == MyGame_Sample_Equipment_Weapon

// Now that we know the union value is a weapon, we can safely call as_Weapon:
let union_weapon = monster.equipped_as_Weapon

assert union_weapon.name == "Axe"
assert union_weapon.damage == 5

print "The FlatBuffer was successfully created and verified!"