Cpp Lua Data Passing

lua-users home
wiki

Difference (from prior major revision) (minor diff, author diff)

Changed: 1c1
This tutorial illustrates through example code how to pass data between a C++ and a lua script.
This example code illustrates how to pass data between a C++ and a lua script.

Removed: 4,5d3
The code on this page is nearly identical to the code on CppLuaArgumentPassing?.


Changed: 8c6
{{{
{{{

Changed: 12,13c10,11
0 45
1 99
1 45
2 99

Changed: 23c21
{{{
{{{

Changed: 26,28c24,26
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

Changed: 30c28
#include &lt;iostream&gt;
#include <iostream>

Changed: 50c48
for (; lib-&gt;func; lib++)
for (; lib->func; lib++)

Changed: 52c50
lib-&gt;func(l); /* open library */
lib->func(l); /* open library */

Changed: 66c64
std::cout &lt;&lt; "[C++] Passing 'arg' array to script" &lt;&lt; std::endl;
std::cout << "[C++] Passing 'arg' array to script" << std::endl;

Changed: 71,72c69,70
// set first element "0" to value 45
lua_pushnumber( state, 0 );
// set first element "1" to value 45
lua_pushnumber( state, 1 );

Changed: 76,77c74,75
// set second element "1" to value 99
lua_pushnumber( state, 1 );
// set second element "2" to value 99
lua_pushnumber( state, 2 );

Changed: 81c79
// set the number of elements minus 1 (index to the last array element)
// set the number of elements (index to the last array element)

Changed: 83c81
lua_pushnumber( state, 1 );
lua_pushnumber( state, 2 );

Changed: 90c88
std::cout &lt;&lt; "[C++] Running script" &lt;&lt; std::endl;
std::cout << "[C++] Running script" << std::endl;

Changed: 96c94
std::cout &lt;&lt; "bad" &lt;&lt; std::endl;
std::cout << "bad" << std::endl;

Changed: 99c97
std::cerr &lt;&lt; "[C++] script failed" &lt;&lt; std::endl;
std::cerr << "[C++] script failed" << std::endl;

Changed: 101c99
std::cout &lt;&lt; "[C++] These values were returned from the script" &lt;&lt; std::endl;
std::cout << "[C++] These values were returned from the script" << std::endl;

Changed: 106,110c104,108
case LUA_TNUMBER: std::cout &lt;&lt; "script returned " &lt;&lt; lua_tonumber( state, lua_gettop( state ) ) &lt;&lt; std::endl; break;
case LUA_TTABLE: std::cout &lt;&lt; "script returned a table" &lt;&lt; std::endl; break;
case LUA_TSTRING: std::cout &lt;&lt; "script returned " &lt;&lt; lua_tostring( state, lua_gettop( state ) ) &lt;&lt; std::endl; break;
case LUA_TBOOLEAN:std::cout &lt;&lt; "script returned " &lt;&lt; lua_toboolean( state, lua_gettop( state ) ) &lt;&lt; std::endl; break;
default: std::cout &lt;&lt; "script returned unknown param" &lt;&lt; std::endl; break;
case LUA_TNUMBER: std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl; break;
case LUA_TTABLE: std::cout << "script returned a table" << std::endl; break;
case LUA_TSTRING: std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl; break;
case LUA_TBOOLEAN:std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl; break;
default: std::cout << "script returned unknown param" << std::endl; break;

Changed: 122c120
{{{
{{{!Lua

Changed: 125c123
for i=0,table.getn(arg) do
for i=1,table.getn(arg) do

This example code illustrates how to pass data between a C++ and a lua script. Read the comments and text output (cout and print statements) in the following two pieces of code.

After Running the Program

This is the output after running the code listed below.
[C++] Passing 'arg' array to script
[C++] Running script
[lua] These args were passed into the script from C
1       45
2       99
[lua] Script returning data back to C
[C++] These values were returned from the script
script returned 1
script returned 9
script returned a table

The C++ code

extern "C"
{
   #include <lua.h>
   #include <lauxlib.h>
   #include <lualib.h>
}
#include <iostream>

// a list of libs to expose for use in scripts
static const luaL_reg lualibs[] = 
{
  {"base", luaopen_base},
  {"table", luaopen_table},
  {"io", luaopen_io},
  {"string", luaopen_string},
  {"math", luaopen_math},
  {"debug", luaopen_debug},
  {"loadlib", luaopen_loadlib}, 
  /* add your libraries here */
  {NULL, NULL}
};

// load the list of libs
static void openstdlibs( lua_State *l ) 
{
  const luaL_reg *lib = lualibs;
  for (; lib->func; lib++) 
  {
    lib->func(l);  /* open library */
    lua_settop(l, 0);  /* discard any results */
  }
}

int main()
{
   int status;
   lua_State* state = lua_open();

  
   openstdlibs( state );
   status = luaL_loadfile( state, "args.lua" );

   std::cout << "[C++] Passing 'arg' array to script" << std::endl;

   // start array structure
   lua_newtable( state );

   // set first element "1" to value 45
   lua_pushnumber( state, 1 );
   lua_pushnumber( state, 45 );
   lua_rawset( state, -3 );

   // set second element "2" to value 99
   lua_pushnumber( state, 2 );
   lua_pushnumber( state, 99 );
   lua_rawset( state, -3 );
  
   // set the number of elements (index to the last array element)
   lua_pushliteral( state, "n" );
   lua_pushnumber( state, 2 );
   lua_rawset( state, -3 );

   // set the name of the array that the script will access
   lua_setglobal( state, "arg" );


   std::cout << "[C++] Running script" << std::endl;
   
   int result = 0;
   if (status == 0)
      result = lua_pcall( state, 0, LUA_MULTRET, 0 );
   else
      std::cout << "bad" << std::endl;
   
   if (result != 0)
      std::cerr << "[C++] script failed" << std::endl;

   std::cout << "[C++] These values were returned from the script" << std::endl;
   while (lua_gettop( state ))
   {
      switch (lua_type( state, lua_gettop( state ) ))
      {
         case LUA_TNUMBER: std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl; break;
         case LUA_TTABLE:  std::cout << "script returned a table" << std::endl; break;
         case LUA_TSTRING: std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl; break;
         case LUA_TBOOLEAN:std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl; break;
         default: std::cout << "script returned unknown param" << std::endl; break;
      }
      lua_pop( state, 1 );
   }
   lua_close( state );
   return 0;
}

The Lua Script "args.lua"

io.write( "[lua] These args were passed into the script from C\n" );

for i=1,table.getn(arg) do
   print(i,arg[i])
end

io.write("[lua] Script returning data back to C\n")

local temp = {}
temp[1]=9
temp[2]=8
temp[3]=7
temp[4]=6
temp[5]=5

return temp,9,1

RecentChanges · preferences
edit · history
Last edited June 2, 2020 2:26 pm GMT (diff)