Running A Lua 5.2 Script From C++
Running A Lua 5.2 Script From C++
Pistachio Brainstorming
Ideas about science and game development
PAGES
RUNNING A LUA 5.2 SCRIPT FROM C++
Home > Gamedev > Tutorials > Running a Lua 5.2 script from C++ About this blog
[A] | [A+]
Piperine
HuffmanCode
ARCHIVES
April 2014
January 2014
Lua is an extensible programming language to extend other programming languages — called host languages.
April 2013
This is extremely powerful for many reasons. My favorite is that
January 2013
“ It allows to change the behavior of a compiled program without having to recompile it.
Unfortunately, I have not found many tutorials on the net regarding the basic embedding of Lua 5.2 — which is a
December 2012
August 2012
February 2012
July 2011
bit different from 5.1, 5.0 and 4.X — in C++, including these topics:
May 2011
March 2011
1. How to run a Lua script from C++ February 2011
2. How to pass information from Lua to C++ and viceversa November 2010
September 2010
3. How to call C++ functions from Lua
August 2010
4. How to dynamically link libraries of C functions to Lua
July 2010
5. How to implement a simple class in Lua June 2010
April 2010
6. How to pass objects from C++ to Lua and viceversa
December 2009
This is just the transcription of my personal notes about the first topic. Hopefully, in the future I will also November 2009
transcript the notes I have taken regarding the rest of topics. Most of the information has been taken from the October 2009
Lua Wiki sample code and updated (if needed) using the Lua 5.2 reference manual. September 2009
July 2009
I am using Lua version 5.2.1 downloaded from its official page and compiled in Microsoft Visual C++ Express June 2009
May 2009
2010. I am not covering the compilation process of Lua — it is well documented online — nor the meaning of the
Lua code beyond its basic behavior.
CATEGORIES
Running a Lua script from C++
This is the most basic action you can do with embedded Lua in a C++ program. Imagine you have the typical
Gamedev (9) General (43)
Hello World program in Lua, something like
and you want to run it from inside a simple C++ program. The procedure is the following:
1. Create a Lua state — like a Lua virtual machine in which our script will be running.
The project must link to the Lua libraries, that have been compiled previously, and must have access to the Lua
header directory, since we will be including the <lua.hpp> file. We also assume that the C++ executable is
generated in the same directory as the Lua script and that the script is called <helloworld.lua>.
http://acamara.es/blog/runningalua52scriptfromc/ 1/5
23/1/2015 Running a Lua 5.2 script from C++ | Pistachio Brainstorming
#include <lua.hpp>
Detailed explanation
Let’s check the code by pieces. The first piece is to create the new Lua state:
There is no mystery to it. The only step is to store in a pointer the newly created Lua state returned by
luaL_newstate(). Afterwards we can use that pointer to refer to the Lua state.
The second piece is to load the needed libraries. This is a bit more tricky. Every Lua library — even the ones
generated by yourself — are opened by a C++ function and stored in the Lua state as a global table with a given
name. Therefore, our first step is to define the name and opening functions for the desired libraries. Since our
helloworld script is so simple, only the base Lua library is needed. We define “base” as the name of the base
library, which is opened by the luaopen_base() function.
{ NULL, NULL}
};
Then, we load every selected library by calling its loading function with our Lua state pointer. The lua_settop()
http://acamara.es/blog/runningalua52scriptfromc/ 2/5
23/1/2015 Running a Lua 5.2 script from C++ | Pistachio Brainstorming
Then, we load every selected library by calling its loading function with our Lua state pointer. The lua_settop()
sentence just ensures that we discard any variables that may be populated into the Lua stack.
The third piece is to run our script which is selfexplained in this code:
You only have to provide the Lua state in which you want the script running and the filename — with full path if it
is not in the same directory of your C++ executable — of the Lua script.
This will remove any memory used by the Lua state — except for some tables in rare cases.
That is the one of the most simple examples of embedding Lua into C++ programs. Stay tuned for further, more
complicated examples. Read the next tutorial!
About SOY_YUMA
View all posts by Soy_yuma →
POST A COMMENT
Submit Comment
9 Responses
http://acamara.es/blog/runningalua52scriptfromc/ 3/5