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