怎么将C中数据传入lua中
#include<stdio.h>#include<Windows.h>#include<string>usingnamespacestd;extern"C"{#incl...
#include <stdio.h>
#include <Windows.h>
#include <string>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
lua_State * L;//解释器
static int average(lua_State *L)
{
return 1;
}
int main()
{
L=lua_open();
luaL_openlibs(L);
luaL_dofile(L,"c:\\test112.lua");//netstatutemodel1
int a=12;
lua_pushnumber(L,a);
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
lua_pop(L,1);
lua_close(L);
system("pause");
return 0;
}
、、、、、、、、、
lua 中 print(a) 只要传个int a 过来就可以了, 展开
#include <Windows.h>
#include <string>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
lua_State * L;//解释器
static int average(lua_State *L)
{
return 1;
}
int main()
{
L=lua_open();
luaL_openlibs(L);
luaL_dofile(L,"c:\\test112.lua");//netstatutemodel1
int a=12;
lua_pushnumber(L,a);
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
lua_pop(L,1);
lua_close(L);
system("pause");
return 0;
}
、、、、、、、、、
lua 中 print(a) 只要传个int a 过来就可以了, 展开
推荐于2016-06-30 · 知道合伙人互联网行家
关注
展开全部
利用lightuserdata和metatable来协调解决这个问题。也即将数据指针给lua,另外告诉它一个如何操作这些数据的metamethods即可。
typedef struct
{
int x;
int y;
int z;
}TData;
static int getAttribute(lua_State* L)
{
TData *data = (TData*)lua_touserdata(L, 1);
std::string attribute = luaL_checkstring(L, 2);
int result = 0;
if(attribute == "x")
{
result = data->x;
}
else if(attribute == "y")
{
result = data->y;
}
else
{
result = data->z;
}
lua_pushnumber(L, result);
return 1;
}
static struct luaL_reg dataLib[] = {
{"__index", getAttribute},
{NULL, NULL}
};
void getMetaTable(lua_State* L, luaL_reg* methods)
{
lua_pushlightuserdata(L, methods);
lua_gettable(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
/* not found */
lua_pop(L, 1);
lua_newtable(L);
luaL_register(L, NULL, methods);
lua_pushlightuserdata(L, methods);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
}
}
int main()
{
const char* filename = "test.lua";
lua_State *lua = lua_open();
if (lua == NULL)
{
fprintf(stderr, "open lua failed");
return -1;
}
luaL_openlibs(lua);
TData input = {1, 2, 3};
lua_pushlightuserdata(lua, &input);
getMetaTable(lua, dataLib);
lua_setmetatable(lua, -2);
lua_setglobal(lua, "input");
if (luaL_dofile(lua, filename))
{
luaError(lua, "load file %s failed", filename);
}
lua_getglobal(lua, "data");
int output = lua_tointeger(lua, 0);
std::cout << output << std::endl;
return 0;
}
typedef struct
{
int x;
int y;
int z;
}TData;
static int getAttribute(lua_State* L)
{
TData *data = (TData*)lua_touserdata(L, 1);
std::string attribute = luaL_checkstring(L, 2);
int result = 0;
if(attribute == "x")
{
result = data->x;
}
else if(attribute == "y")
{
result = data->y;
}
else
{
result = data->z;
}
lua_pushnumber(L, result);
return 1;
}
static struct luaL_reg dataLib[] = {
{"__index", getAttribute},
{NULL, NULL}
};
void getMetaTable(lua_State* L, luaL_reg* methods)
{
lua_pushlightuserdata(L, methods);
lua_gettable(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
/* not found */
lua_pop(L, 1);
lua_newtable(L);
luaL_register(L, NULL, methods);
lua_pushlightuserdata(L, methods);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
}
}
int main()
{
const char* filename = "test.lua";
lua_State *lua = lua_open();
if (lua == NULL)
{
fprintf(stderr, "open lua failed");
return -1;
}
luaL_openlibs(lua);
TData input = {1, 2, 3};
lua_pushlightuserdata(lua, &input);
getMetaTable(lua, dataLib);
lua_setmetatable(lua, -2);
lua_setglobal(lua, "input");
if (luaL_dofile(lua, filename))
{
luaError(lua, "load file %s failed", filename);
}
lua_getglobal(lua, "data");
int output = lua_tointeger(lua, 0);
std::cout << output << std::endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int a=12;
lua_pushnumber(L,a);//你将a压入栈之后,并没有将a的值赋予给lua中的变量a,所以lua中a的值没变.所以接下来你获取a的值不会是12
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
不知道你是否想的是这样:
int a=12;
lua_pushnumber(L,a);
//设置lua的全局变量值:
lua_pushnumber(L,a);
lua_setglpbal(L, "a");
//取全局变量的值:
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
lua_pushnumber(L,a);//你将a压入栈之后,并没有将a的值赋予给lua中的变量a,所以lua中a的值没变.所以接下来你获取a的值不会是12
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
不知道你是否想的是这样:
int a=12;
lua_pushnumber(L,a);
//设置lua的全局变量值:
lua_pushnumber(L,a);
lua_setglpbal(L, "a");
//取全局变量的值:
lua_getglobal(L,"a");
a=(int)lua_tointeger(L,-1);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询