Chapter IV ·
Lua.ex integration
/
Lesson 20 of 27
Host integration
Reading & writing state
You'll learn: Define globals from Elixir with
Lua.set! and read them back with Lua.get!.
Lua.set!(lua, [:greeting], "hi") writes a global. Lua.get!(
lua, [:total]) reads one. Paths nest into tables:
Lua.set!(lua, [:cfg, :api_key], …) writes to cfg.api_key.
After eval!, anything the script wrote to a global is
readable from Elixir.
Elixir · your app
Reference only
lua =
Lua.new()
|> Lua.set!([:discount_pct], 15)
|> Lua.set!([:prices], [10, 20, 30])
{_, lua} = Lua.eval!(lua, source)
Lua.get!(lua, [:final_total])
# => 51.0
Output
idle
Hit Run to execute.
Try it:
Add a nested-path read on the Elixir side:
Lua.set!(lua, [:cfg, :rate], 0.08), then use cfg.rate in the Lua snippet to compute tax.