下面的代码演示了在C++和lua脚本之间传递数据。
首先在C++中创建一个table,添加元素,然后放置到lua的全局表中。在lua脚本中可以使用C++创建的这个表。
然后在脚本中创建一个表,以脚本返回值的方式返回给C++,在C++中可以读取表中的值。
例子代码需要一个args.lua的lua文件,要手工创建,我把它放到了C盘根目录下。
// cpplua.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <iostream>
/* 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
temp["test1 key"]="test1 value"
temp[6]="test 6"
temp["test 99"]=99
for i,n in pairs(temp)
d