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 15include from "../lobster/" 16include "monster_generated.lobster" 17 18// Example of how to use FlatBuffers to create and read binary buffers. 19 20// Create a builder. 21let b = flatbuffers_builder {} 22 23// Create some weapons for our monster. 24let weapon_names = [ "Sword", "Axe" ] 25let weapon_damages = [ 3, 5 ] 26 27weapon_offsets := map(weapon_names) name, i: 28 let ns = b.CreateString(name) 29 b.MyGame_Sample_WeaponStart() 30 b.MyGame_Sample_WeaponAddName(ns) 31 b.MyGame_Sample_WeaponAddDamage(weapon_damages[i]) 32 b.MyGame_Sample_WeaponEnd() 33 34let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets) 35 36// Name of the monster. 37let name = b.CreateString("Orc") 38 39// Inventory. 40let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _) 41 42// Now pack it all together in our root monster object. 43b.MyGame_Sample_MonsterStart() 44b.MyGame_Sample_MonsterAddPos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0)) 45b.MyGame_Sample_MonsterAddHp(300) 46b.MyGame_Sample_MonsterAddName(name) 47b.MyGame_Sample_MonsterAddInventory(inv) 48b.MyGame_Sample_MonsterAddColor(MyGame_Sample_Color_Red) 49b.MyGame_Sample_MonsterAddWeapons(weapons) 50b.MyGame_Sample_MonsterAddEquippedType(MyGame_Sample_Equipment_Weapon) 51b.MyGame_Sample_MonsterAddEquipped(weapon_offsets[1]) 52let orc = b.MyGame_Sample_MonsterEnd() 53 54// Finish the buffer! 55b.Finish(orc) 56 57// We now have a FlatBuffer that we could store on disk or send over a network. 58 59let buf = b.SizedCopy() 60 61// ...Saving to file or sending over a network code goes here... 62 63// Instead, we are going to access this buffer right away (as if we just 64// received it). 65 66// Get the root object accessor. 67let monster = MyGame_Sample_GetRootAsMonster(buf) 68 69// Note: We did not set the `mana` field explicitly, so we get a default value. 70assert monster.mana == 150 71assert monster.hp == 300 72assert monster.name == "Orc" 73assert monster.color == MyGame_Sample_Color_Red 74let pos = monster.pos 75assert pos 76assert pos.x == 1.0 77assert pos.y == 2.0 78assert pos.z == 3.0 79 80// Get and test the `inventory` FlatBuffer vector. 81for(monster.inventory_length) e, i: 82 assert monster.inventory(i) == e 83 84// Get and test the `weapons` FlatBuffer vector of tables. 85for(monster.weapons_length) i: 86 assert monster.weapons(i).name == weapon_names[i] 87 assert monster.weapons(i).damage == weapon_damages[i] 88 89// Get and test the `equipped` FlatBuffer union. 90assert monster.equipped_type() == MyGame_Sample_Equipment_Weapon 91 92// Now that we know the union value is a weapon, we can safely call as_Weapon: 93let union_weapon = monster.equipped_as_Weapon 94 95assert union_weapon.name == "Axe" 96assert union_weapon.damage == 5 97 98print "The FlatBuffer was successfully created and verified!" 99