1/* 2 * Copyright 2015 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17// To run, use the `javascript_sample.sh` script. 18 19var assert = require('assert'); 20var flatbuffers = require('../js/flatbuffers').flatbuffers; 21var MyGame = require('./monster_generated').MyGame; 22 23// Example how to use FlatBuffers to create and read binary buffers. 24function main() { 25 var builder = new flatbuffers.Builder(0); 26 27 // Create some weapons for our Monster ('Sword' and 'Axe'). 28 var weaponOne = builder.createString('Sword'); 29 var weaponTwo = builder.createString('Axe'); 30 31 MyGame.Sample.Weapon.startWeapon(builder); 32 MyGame.Sample.Weapon.addName(builder, weaponOne); 33 MyGame.Sample.Weapon.addDamage(builder, 3); 34 var sword = MyGame.Sample.Weapon.endWeapon(builder); 35 36 MyGame.Sample.Weapon.startWeapon(builder); 37 MyGame.Sample.Weapon.addName(builder, weaponTwo); 38 MyGame.Sample.Weapon.addDamage(builder, 5); 39 var axe = MyGame.Sample.Weapon.endWeapon(builder); 40 41 // Serialize the FlatBuffer data. 42 var name = builder.createString('Orc'); 43 44 var treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 45 var inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure); 46 47 var weaps = [sword, axe]; 48 var weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps); 49 50 var pos = MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0); 51 52 MyGame.Sample.Monster.startMonster(builder); 53 MyGame.Sample.Monster.addPos(builder, pos); 54 MyGame.Sample.Monster.addHp(builder, 300); 55 MyGame.Sample.Monster.addColor(builder, MyGame.Sample.Color.Red) 56 MyGame.Sample.Monster.addName(builder, name); 57 MyGame.Sample.Monster.addInventory(builder, inv); 58 MyGame.Sample.Monster.addWeapons(builder, weapons); 59 MyGame.Sample.Monster.addEquippedType(builder, MyGame.Sample.Equipment.Weapon); 60 MyGame.Sample.Monster.addEquipped(builder, weaps[1]); 61 var orc = MyGame.Sample.Monster.endMonster(builder); 62 63 builder.finish(orc); // You may also call 'MyGame.Example.Monster.finishMonsterBuffer(builder, orc);'. 64 65 // We now have a FlatBuffer that can be stored on disk or sent over a network. 66 67 // ...Code to store to disk or send over a network goes here... 68 69 // Instead, we are going to access it right away, as if we just received it. 70 71 var buf = builder.dataBuffer(); 72 73 // Get access to the root: 74 var monster = MyGame.Sample.Monster.getRootAsMonster(buf); 75 76 // Note: We did not set the `mana` field explicitly, so we get back the default value. 77 assert.equal(monster.mana(), 150); 78 assert.equal(monster.hp(), 300); 79 assert.equal(monster.name(), 'Orc'); 80 assert.equal(monster.color(), MyGame.Sample.Color.Red); 81 assert.equal(monster.pos().x(), 1.0); 82 assert.equal(monster.pos().y(), 2.0); 83 assert.equal(monster.pos().z(), 3.0); 84 85 // Get and test the `inventory` FlatBuffer `vector`. 86 for (var i = 0; i < monster.inventoryLength(); i++) { 87 assert.equal(monster.inventory(i), i); 88 } 89 90 // Get and test the `weapons` FlatBuffer `vector` of `table`s. 91 var expectedWeaponNames = ['Sword', 'Axe']; 92 var expectedWeaponDamages = [3, 5]; 93 for (var i = 0; i < monster.weaponsLength(); i++) { 94 assert.equal(monster.weapons(i).name(), expectedWeaponNames[i]); 95 assert.equal(monster.weapons(i).damage(), expectedWeaponDamages[i]); 96 } 97 98 // Get and test the `equipped` FlatBuffer `union`. 99 assert.equal(monster.equippedType(), MyGame.Sample.Equipment.Weapon); 100 assert.equal(monster.equipped(new MyGame.Sample.Weapon()).name(), 'Axe'); 101 assert.equal(monster.equipped(new MyGame.Sample.Weapon()).damage(), 5); 102 103 console.log('The FlatBuffer was successfully created and verified!'); 104} 105 106main(); 107