• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Use in Lua    {#flatbuffers_guide_use_lua}
2=============
3
4## Before you get started
5
6Before 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
8FlatBuffers usage in all of the supported languages (including Lua). This
9page is designed to cover the nuances of FlatBuffers usage, specific to
10Lua.
11
12You should also have read the [Building](@ref flatbuffers_guide_building)
13documentation 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
19The 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
25The code to test the Lua library can be found at `flatbuffers/tests`.
26The test code itself is located in [luatest.lua](https://github.com/google/
27flatbuffers/blob/master/tests/luatest.lua).
28
29To run the tests, use the [LuaTest.sh](https://github.com/google/flatbuffers/
30blob/master/tests/LuaTest.sh) shell script.
31
32*Note: This script requires [Lua 5.3](https://www.lua.org/) to be
33installed.*
34
35## Using the FlatBuffers Lua library
36
37*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
38example of how to use FlatBuffers in Lua.*
39
40There is support for both reading and writing FlatBuffers in Lua.
41
42To use FlatBuffers in your own code, first generate Lua classes from your
43schema with the `--lua` option to `flatc`. Then you can include both
44FlatBuffers and the generated code to read or write a FlatBuffer.
45
46For example, here is how you would read a FlatBuffer binary file in Lua:
47First, require the module and the generated code. Then read a FlatBuffer binary
48file 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
66Now 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
77There currently is no support for parsing text (Schema's and JSON) directly
78from Lua, though you could use the C++ parser through SWIG or ctypes. Please
79see the C++ documentation for more on text parsing.
80
81<br>
82