In this article we will see the reverse process that we saw in the video: How to Incorporate C/C++ Functions in Lua.
We will see how to run and obtain variable values from Lua code in C++. This concept is widely used in Game Development.
If you don’t have Lua installed, you can still test it, just download Lua from this address: http://www.lua.org/download.html and download the package: lua-5.4.6.tar .gz.
Unzip and use -I include
for the src
of the unzipped directory.
01. Basic code
For the first example, let’s create a code in C++ that prints a Hello World on the screen, but with the Lua print
:
To compile use:
02. Handling errors
What if I have a Lua syntax error?
03. Running external files
file.lua
Replace luaL_dostring
with luaL_dofile
04. Get variables from Lua
If you have a variable screen = "1920 x 1080"
in the file file.lua
and want to know its contents, just use: lua_tostring(L., -1)
:
lua
cpp
Marcos Oliveira
Software developer