public static Offset <Monster> BuildMonster(FlatBufferBuilder fbb) { var sword = Weapon.CreateWeapon(fbb, fbb.CreateString("Sword"), 3); var axe = Weapon.CreateWeapon(fbb, fbb.CreateString("Axe"), 5); Monster.StartInventoryVector(fbb, 5); var inventoryOffset = BuildVector(fbb, true);; var weapons = new Offset <Weapon>[2] { sword, axe }; var weaponsOffset = Monster.CreateWeaponsVector(fbb, weapons); Monster.StartPathVector(fbb, 2); //Vector of structs Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f); Vec3.CreateVec3(fbb, 4.0f, 5.0f, 6.0f); var path = fbb.EndVector(); var nameOffset = fbb.CreateString("Orc"); Monster.StartMonster(fbb); Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f)); Monster.AddHp(fbb, 300); Monster.AddName(fbb, nameOffset); Monster.AddInventory(fbb, inventoryOffset); Monster.AddColor(fbb, Color.Red); Monster.AddWeapons(fbb, weaponsOffset); Monster.AddEquippedType(fbb, Equipment.Weapon); //Type of union stored Monster.AddEquipped(fbb, axe.Value); // Value of union Monster.AddPath(fbb, path); return(Monster.EndMonster(fbb)); }
private byte[] Save() { Debug.Log("Creating and saving a Monster to byte array"); // Create a `FlatBufferBuilder`, which will be used to create our // monsters' FlatBuffers. var builder = new FlatBufferBuilder(1024); var weaponOneName = builder.CreateString("Sword"); var weaponOneDamage = 3; var weaponTwoName = builder.CreateString("Axe"); var weaponTwoDamage = 5; // Use the `CreateWeapon()` helper function to create the weapons, since we set every field. var sword = Weapon.CreateWeapon(builder, weaponOneName, (short)weaponOneDamage); var axe = Weapon.CreateWeapon(builder, weaponTwoName, (short)weaponTwoDamage); // Serialize a name for our monster, called "Orc". var name = builder.CreateString("Orc"); // Create a `vector` representing the inventory of the Orc. Each number // could correspond to an item that can be claimed after he is slain. // Note: Since we prepend the bytes, this loop iterates in reverse order. Monster.StartInventoryVector(builder, 10); for (int i = 9; i >= 0; i--) { builder.AddByte((byte)i); } var inv = builder.EndVector(); var weaps = new Offset <Weapon> [2]; weaps[0] = sword; weaps[1] = axe; // Pass the `weaps` array into the `CreateWeaponsVector()` method to create a FlatBuffer vector. var weapons = Monster.CreateWeaponsVector(builder, weaps); Monster.StartPathVector(builder, 2); Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f); Vec3.CreateVec3(builder, 4.0f, 5.0f, 6.0f); var path = builder.EndVector(); // Create our monster using `StartMonster()` and `EndMonster()`. Monster.StartMonster(builder); Monster.AddPos(builder, Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f)); Monster.AddHp(builder, 300); Monster.AddName(builder, name); Monster.AddInventory(builder, inv); Monster.AddColor(builder, MyGame.Sample.Color.Red); Monster.AddWeapons(builder, weapons); Monster.AddEquippedType(builder, Equipment.Weapon); Monster.AddEquipped(builder, axe.Value); // Axe Monster.AddPath(builder, path); var orc = Monster.EndMonster(builder); // Call `Finish()` to instruct the builder that this monster is complete. builder.Finish(orc.Value); // You could also call `Monster.FinishMonsterBuffer(builder, orc);`. return(builder.SizedByteArray()); }