0% found this document useful (0 votes)
9 views3 pages

Running A Lua 5.2 Script From C++

The document provides a tutorial on how to run a Lua 5.2 script from a C++ program, detailing the steps to create a Lua state, load necessary libraries, execute the script, and close the Lua state. It includes a simple example of a 'Hello World' Lua program and the corresponding C++ code required to execute it. The author aims to share personal notes and plans to cover additional topics related to Lua and C++ integration in future posts.

Uploaded by

cewis48329
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Running A Lua 5.2 Script From C++

The document provides a tutorial on how to run a Lua 5.2 script from a C++ program, detailing the steps to create a Lua state, load necessary libraries, execute the script, and close the Lua state. It includes a simple example of a 'Hello World' Lua program and the corresponding C++ code required to execute it. The author aims to share personal notes and plans to cover additional topics related to Lua and C++ integration in future posts.

Uploaded by

cewis48329
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

23/1/2015 Running a Lua 5.

2 script from C++ | Pistachio Brainstorming

Pistachio Brainstorming
Ideas about science and game development

ABOUT THIS BLOG PIPERINE HUFFMANCODE Search...

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 vice­versa 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 vice­versa
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

Stories (3) Tutorials (15)


‐‐ Simple Hello World Lua program
print('Hello World!')

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.

2. Load the needed Lua libraries to run the script.

3. Run the Lua script.

4. Close the Lua state.

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>.

The full code will look like:

http://acamara.es/blog/running­a­lua­5­2­script­from­c/ 1/5
23/1/2015 Running a Lua 5.2 script from C++ | Pistachio Brainstorming

#include <lua.hpp>

int main(int argc, char* argv[])


{
// create new Lua state
lua_State *lua_state;
lua_state = luaL_newstate();

// load Lua libraries


static const luaL_Reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL}
};

const luaL_Reg *lib = lualibs;


for(; lib‐>func != NULL; lib++)
{
lib‐>func(lua_state);
lua_settop(lua_state, 0);
}

// run the Lua script


luaL_dofile(lua_state, "helloworld.lua");

// close the Lua state


lua_close(lua_state);
}

The output of the program is just what we expect:

Detailed explanation
Let’s check the code by pieces. The first piece is to create the new Lua state:

// create new Lua state


lua_State *lua_state;
lua_state = luaL_newstate();

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
hello­world 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.

// load Lua libraries


static const luaL_Reg lualibs[] =
{
{ "base", luaopen_base },

{ 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/running­a­lua­5­2­script­from­c/ 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.

const luaL_Reg *lib = lualibs;


for(; lib‐>func != NULL; lib++)
{
lib‐>func(lua_state);
lua_settop(lua_state, 0);
}

The third piece is to run our script which is self­explained in this code:

// run the Lua script


luaL_dofile(lua_state, "helloworld.lua");

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.

The last piece is to close the Lua state:

// close the Lua state


lua_close(lua_state);

This will remove any memory used by the Lua state — except for some tables in rare cases.

And that is it!

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!

Tags: C++ GAMEDEV LUA PROGRAMMING TUTORIAL

About SOY_YUMA
View all posts by Soy_yuma →

By Soy_yuma | 14th August 2012 | SHOW COMMENTS (9)

DONDE VAMOS A LLEGAR CON PASSING VARIABLES FROM LUA


TTS COMO #HIJOSTROSPIDOS 5.2 TO C++ (AND VICE­VERSA)

POST A COMMENT

Submit Comment

Notify me of follow­up comments by email.

Notify me of new posts by email.

9 Responses
http://acamara.es/blog/running­a­lua­5­2­script­from­c/ 3/5

You might also like