This is the second of my Lua series tutorials, which focuses on data transfer between C + + and Lua. If you don't yet know how to call Lua scripts in C + +, refer to this article. This article mainly introduces the delivery of basic data types, such as shaping (int), string (string), number, and bool value.
Load and run LUA scripts
Since the previous tutorial has already introduced how to embed Lua in C + +, this section simply describes how the program is used, and the configuration is skipped.
Creating a LUA virtual machine
code as follows:
Lua_state *lua_state = Lual_newstate ();
Load LUA Libraries
static const Lual_reg lualibs[] =
{
{"Base", luaopen_base},
{"Io", luaopen_io},
{NULL, NULL}
};
Const Lual_reg *lib = lualibs;
for (; Lib->func!= NULL; lib++)
{
Lual_requiref (Lua_state, Lib->name, Lib->func, 1);
Lua_pop (lua_state,1);
}
Running LUA scripts
std::string scriptpath = fileutils::getinstance ()->fullpathforfilename ("Hello.lua");
int status = Lual_loadfile (Lua_state, Scriptpath.c_str ());
Std::cout << "return:" << status << Std::endl;
int result = 0;
if (status = = LUA_OK)
{
result = Lua_pcall (lua_state, 0, Lua_multret, 0);
}
Else
{
Std::cout << "Could not load the script." << Std::endl;
}
Here we are using lual_loadfile instead of previous lual_dofile, in fact Lual_dofile is just a macro definition:
#define LUAL_DOFILE (L, fn) \
(Lual_loadfile (L, fn) | | lua_pcall (l, 0, Lua_multret, 0))
We first call Lual_loadfile to determine if the Lua script is loaded successfully, and then call Lua_pcall to execute the LUA script.
C + + calls LUA functions
First, we define a LUA function in Hello.lua:
--Add two numbers
function add (x, y)
return x + y
End
The function definition of LUA is keyword with functions and ends with end, and its arguments are of no formal parameter type, and in addition, LUA functions can return multiple values. But we've only returned one value here.
Next, let's see if this function is called in a C + + program:
int Luaadd (lua_state *lua_state, int x, int y)
{
int sum;
Get the Add function in Lua and put it on the top of the LUA stack
Lua_getglobal (lua_state, "add");
Pressing two parameters into the LUA stack
Lua_pushnumber (lua_state, x);
Lua_pushnumber (lua_state, y);
Call the LUA function, where 2 is the number of arguments, and 1 is the number of returned values.
Lua_call (Lua_state, 2, 1);
Read the return value from the top of the stack, note that the parameter here is-1
sum = Lua_tointeger (lua_state,-1);
Finally, we remove the return value from the top of the stack.
Lua_pop (lua_state, 1);
return sum;
}
Then we can call it in the program:
code as follows:
std::cout<< "2 + 1=" << luaadd (lua_state,4,1) <<std::endl;
Note that this method call is to be followed by the Lua_pcall call.
Manipulating LUA Global variables
C + + Gets the value of the LUA global variable
First, we define a global variable inside the Hello.lua.
code as follows:
Then we access it in C + +:
Lua_getglobal (lua_state, "myname");
std::string myname = lua_tostring (lua_state,-1);
Lua_pop (lua_state, 1);
std::cout<< "Hello:" <<myname<<std::endl;
This time we are using the Lua_getglobal to press the MYNAME global variable onto the LUA stack and then use lua_tostring to get the value.
C + + to modify the value of LUA global variables
This time we're using Lua_setglobal to pass the data to LUA:
Lua_pushstring (Lua_state, "World");
Lua_setglobal (lua_state, "myname");
At this point, we can print the values passed in by calling print (MyName) at the very beginning of the Hello.lua.
C + + Pass table to Lua
code as follows:
Lua_createtable (lua_state, 2, 0);
Lua_pushnumber (lua_state, 1);
Lua_pushnumber (Lua_state, 49);
Lua_settable (Lua_state,-3);
Lua_rawset (Lua_state,-3);
Lua_pushnumber (Lua_state, 2);
Lua_pushstring (Lua_state, "Life is a Beach");
Lua_settable (Lua_state,-3);
Lua_rawset (Lua_state,-3);
Lua_setglobal (lua_state, "Arg");
Here we pass a table to LUA, this table is {beach, and "Life is a"}. The index of the LUA table starts at 1, and then we can access this table in the Lua script:
For I=1, #arg do
Print ("", I, Arg[i])
End
The #arg here is to get the length of the table and then use Arg[i] to get the value of the table's index i.
LUA returns multiple values to C + +
The first is the LUA code:
Local TEMP = {9, "Hehehej"}
--Temp[1]=9
--temp[2]= "You cowboy!"
Return temp,9,1
Then the C + + code:
Std::stringstream Str_buf;
while (Lua_gettop (lua_state))
{
Str_buf.str (std::string ());
Str_buf << "";
Switch (Lua_type (lua_state, Lua_gettop (lua_state))
{
Case Lua_tnumber:
Str_buf << Script returned the number: "
<< Lua_tonumber (lua_state, Lua_gettop (lua_state));
Break
Case Lua_ttable:
Str_buf << "script returned a table";
Break
Case lua_tstring:
Str_buf << script returned the string: "
<< lua_tostring (lua_state, Lua_gettop (lua_state));
Break
Case Lua_tboolean:
Str_buf << Script returned the Boolean: "
<< Lua_toboolean (lua_state, Lua_gettop (lua_state));
Break
Default
Str_buf << "script returned an Unknown-type value";
}
Lua_pop (lua_state, 1);
Std::cout << str_buf.str () << Std::endl;
}
The final output is:
[C + +] Values returned from the script:
Script returned the Number:1
Script returned the Number:9
Script returned a table
[C + +] Closing the Lua State
The order of return values in Lua is table,9,1 and is reversed in C + +. Because we are using the stack as a data structure to pass the information, and the stack is advanced.
In the next article, we'll introduce a table called Lua in C + +.