1 -- need to update the Lua path to point to the local flatbuffers implementation 2 package.path = string.format("../lua/?.lua;%s",package.path) 3 package.path = string.format("./lua/?.lua;%s",package.path) 4 5 -- require the library 6 local flatbuffers = require("flatbuffers") 7 8 local binaryArray = flatbuffers.binaryArray-- for hex dump utility 9 10 -- require the files generated from the schema 11 local weapon = require("MyGame.Sample.Weapon") 12 local monster = require("MyGame.Sample.Monster") 13 local vec3 = require("MyGame.Sample.Vec3") 14 local color = require("MyGame.Sample.Color") 15 local equipment = require("MyGame.Sample.Equipment") 16 17 -- get access to the builder, providing an array of size 1024 18 local builder = flatbuffers.Builder(1024) 19 20 local weaponOne = builder:CreateString("Sword") 21 local weaponTwo = builder:CreateString("Axe") 22 23 -- Create the first 'Weapon' 24 weapon.Start(builder) 25 weapon.AddName(builder, weaponOne) 26 weapon.AddDamage(builder, 3) 27 local sword = weapon.End(builder) 28 29 -- Create the second 'Weapon' 30 weapon.Start(builder) 31 weapon.AddName(builder, weaponTwo) 32 weapon.AddDamage(builder, 5) 33 local axe = weapon.End(builder) 34 35 -- Serialize a name for our mosnter, called 'orc' 36 local name = builder:CreateString("Orc") 37 38 -- Create a `vector` representing the inventory of the Orc. Each number 39 -- could correspond to an item that can be claimed after he is slain. 40 -- Note: Since we prepend the bytes, this loop iterates in reverse. 41 monster.StartInventoryVector(builder, 10) 42 for i=10,1,-1 do 43 builder:PrependByte(i) 44 end 45 local inv = builder:EndVector(10) 46 47 -- Create a FlatBuffer vector and prepend the weapons. 48 -- Note: Since we prepend the data, prepend them in reverse order. 49 monster.StartWeaponsVector(builder, 2) 50 builder:PrependUOffsetTRelative(axe) 51 builder:PrependUOffsetTRelative(sword) 52 local weapons = builder:EndVector(2) 53 54 -- Create our monster by using Start() andEnd() 55 monster.Start(builder) 56 monster.AddPos(builder, vec3.CreateVec3(builder, 1.0, 2.0, 3.0)) 57 monster.AddHp(builder, 300) 58 monster.AddName(builder, name) 59 monster.AddInventory(builder, inv) 60 monster.AddColor(builder, color.Red) 61 monster.AddWeapons(builder, weapons) 62 monster.AddEquippedType(builder, equipment.Weapon) 63 monster.AddEquipped(builder, axe) 64 local orc = monster.End(builder) 65 66 -- Call 'Finish()' to instruct the builder that this monster is complete. 67 builder:Finish(orc) 68 69 -- Get the flatbuffer as a string containing the binary data 70 local bufAsString = builder:Output() 71 72 -- Convert the string representation into binary array Lua structure 73 local buf = flatbuffers.binaryArray.New(bufAsString) 74 75 -- Get an accessor to the root object insert the buffer 76 local mon = monster.GetRootAsMonster(buf, 0) 77 78 assert(mon:Mana() == 150) 79 assert(mon:Hp() == 300) 80 assert(mon:Name() == "Orc") 81 assert(mon:Color() == color.Red) 82 assert(mon:Pos():X() == 1.0) 83 assert(mon:Pos():Y() == 2.0) 84 assert(mon:Pos():Z() == 3.0) 85 86 for i=1,mon:InventoryLength() do 87 assert(mon:Inventory(i) == i) 88 end 89 90 local expected = { 91 {w = 'Sword', d = 3}, 92 {w = 'Axe', d = 5} 93 } 94 95 for i=1,mon:WeaponsLength() do 96 assert(mon:Weapons(i):Name() == expected[i].w) 97 assert(mon:Weapons(i):Damage() == expected[i].d) 98 end 99 100 assert(mon:EquippedType() == equipment.Weapon) 101 102 local unionWeapon = weapon.New() 103 unionWeapon:Init(mon:Equipped().bytes,mon:Equipped().pos) 104 assert(unionWeapon:Name() == "Axe") 105 assert(unionWeapon:Damage() == 5) 106 107 print("The Lua FlatBuffer example was successfully created and verified!")