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 include from "../lobster/" 16 include "monster_generated.lobster" 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 weapon_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 34 let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets) 35 36 // Name of the monster. 37 let name = b.CreateString("Orc") 38 39 // Inventory. 40 let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _) 41 42 // Now pack it all together in our root monster object. 43 b.MyGame_Sample_MonsterStart() 44 b.MyGame_Sample_MonsterAddPos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0)) 45 b.MyGame_Sample_MonsterAddHp(300) 46 b.MyGame_Sample_MonsterAddName(name) 47 b.MyGame_Sample_MonsterAddInventory(inv) 48 b.MyGame_Sample_MonsterAddColor(MyGame_Sample_Color_Red) 49 b.MyGame_Sample_MonsterAddWeapons(weapons) 50 b.MyGame_Sample_MonsterAddEquippedType(MyGame_Sample_Equipment_Weapon) 51 b.MyGame_Sample_MonsterAddEquipped(weapon_offsets[1]) 52 let orc = b.MyGame_Sample_MonsterEnd() 53 54 // Finish the buffer! 55 b.Finish(orc) 56 57 // We now have a FlatBuffer that we could store on disk or send over a network. 58 59 let 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. 67 let monster = MyGame_Sample_GetRootAsMonster(buf) 68 69 // Note: We did not set the `mana` field explicitly, so we get a default value. 70 assert monster.mana == 150 71 assert monster.hp == 300 72 assert monster.name == "Orc" 73 assert monster.color == MyGame_Sample_Color_Red 74 let pos = monster.pos 75 assert pos 76 assert pos.x == 1.0 77 assert pos.y == 2.0 78 assert pos.z == 3.0 79 80 // Get and test the `inventory` FlatBuffer vector. 81 for(monster.inventory_length) e, i: 82 assert monster.inventory(i) == e 83 84 // Get and test the `weapons` FlatBuffer vector of tables. 85 for(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. 90 assert 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: 93 let union_weapon = monster.equipped_as_Weapon 94 95 assert union_weapon.name == "Axe" 96 assert union_weapon.damage == 5 97 98 print "The FlatBuffer was successfully created and verified!" 99