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 15import from "../lobster/" 16import monster_generated 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 27let 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 35let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets) 36 37// Name of the monster. 38let name = b.CreateString("Orc") 39 40// Inventory. 41let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _) 42 43// Now pack it all together in our root monster object. 44let 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! 57b.Finish(orc) 58 59// We now have a FlatBuffer that we could store on disk or send over a network. 60 61let 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. 69let monster = MyGame_Sample_GetRootAsMonster(buf) 70 71// Note: We did not set the `mana` field explicitly, so we get a default value. 72assert monster.mana == 150 73assert monster.hp == 300 74assert monster.name == "Orc" 75assert monster.color == MyGame_Sample_Color_Red 76let pos = monster.pos 77assert pos 78assert pos.x == 1.0 79assert pos.y == 2.0 80assert pos.z == 3.0 81 82// Get and test the `inventory` FlatBuffer vector. 83for(monster.inventory_length) e, i: 84 assert monster.inventory(i) == e 85 86// Get and test the `weapons` FlatBuffer vector of tables. 87for(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. 92assert 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: 95let union_weapon = monster.equipped_as_Weapon 96 97assert union_weapon.name == "Axe" 98assert union_weapon.damage == 5 99 100print "The FlatBuffer was successfully created and verified!" 101