Home | History | Annotate | Download | only in source
      1 Use in Lua    {#flatbuffers_guide_use_lua}
      2 =============
      3 
      4 ## Before you get started
      5 
      6 Before diving into the FlatBuffers usage in Lua, it should be noted that the
      7 [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general
      8 FlatBuffers usage in all of the supported languages (including Lua). This
      9 page is designed to cover the nuances of FlatBuffers usage, specific to
     10 Lua.
     11 
     12 You should also have read the [Building](@ref flatbuffers_guide_building)
     13 documentation to build `flatc` and should be familiar with
     14 [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
     15 [Writing a schema](@ref flatbuffers_guide_writing_schema).
     16 
     17 ## FlatBuffers Lua library code location
     18 
     19 The code for the FlatBuffers Lua library can be found at
     20 `flatbuffers/lua`. You can browse the library code on the
     21 [FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/lua).
     22 
     23 ## Testing the FlatBuffers Lua library
     24 
     25 The code to test the Lua library can be found at `flatbuffers/tests`.
     26 The test code itself is located in [luatest.lua](https://github.com/google/
     27 flatbuffers/blob/master/tests/luatest.lua).
     28 
     29 To run the tests, use the [LuaTest.sh](https://github.com/google/flatbuffers/
     30 blob/master/tests/LuaTest.sh) shell script.
     31 
     32 *Note: This script requires [Lua 5.3](https://www.lua.org/) to be
     33 installed.*
     34 
     35 ## Using the FlatBuffers Lua library
     36 
     37 *Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
     38 example of how to use FlatBuffers in Lua.*
     39 
     40 There is support for both reading and writing FlatBuffers in Lua.
     41 
     42 To use FlatBuffers in your own code, first generate Lua classes from your
     43 schema with the `--lua` option to `flatc`. Then you can include both
     44 FlatBuffers and the generated code to read or write a FlatBuffer.
     45 
     46 For example, here is how you would read a FlatBuffer binary file in Lua:
     47 First, require the module and the generated code. Then read a FlatBuffer binary
     48 file into a `string`, which you pass to the `GetRootAsMonster` function:
     49 
     50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lua}
     51     -- require the library
     52     local flatbuffers = require("flatbuffers")
     53     
     54     -- require the generated code
     55     local monster = require("MyGame.Sample.Monster")
     56 
     57     -- read the flatbuffer from a file into a string
     58     local f = io.open('monster.dat', 'rb')
     59     local buf = f:read('*a')
     60     f:close()
     61 
     62     -- parse the flatbuffer to get an instance to the root monster
     63     local monster1 = monster.GetRootAsMonster(buf, 0)
     64 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     65 
     66 Now you can access values like this:
     67 
     68 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lua}
     69     -- use the : notation to access member data
     70     local hp = monster1:Hp()
     71     local pos = monster1:Pos()
     72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     73 
     74 
     75 ## Text Parsing
     76 
     77 There currently is no support for parsing text (Schema's and JSON) directly
     78 from Lua, though you could use the C++ parser through SWIG or ctypes. Please
     79 see the C++ documentation for more on text parsing.
     80 
     81 <br>
     82