Lua编程示例(五): C语言对Lua表的读取和添加(lua调用C#)深度揭秘

随心笔谈9个月前发布 admin
64 0


#include “stdafx.h”

lua_State *L;

void load_lua(char *filename){
L=luaL_newstate();
luaL_openlibs(L);
if((luaL_loadfile(L,filename) || lua_pcall(L,0,0,0))!=0){
luaL_error(L,”loadfile error! \n %s”,lua_tostring(L,-1));
}
}

double getfield(lua_State *L,char * key){
double res;
//默认栈顶是table,将key入栈
lua_pushstring(L,key);
lua_gettable(L,-2); //查找键值为key的元素,置于栈顶
if(!lua_isnumber(L,-1)){
luaL_error(L,”num get error! %s\n”,lua_tostring(L,-1));
}
res=lua_tonumber(L,-1);
lua_pop(L,1); //删掉产生的查找结果
return res;
}
void setfield(lua_State *L,char *key,double value){
//默认栈顶是table
lua_pushstring(L,key);
lua_pushnumber(L,value);
lua_settable(L,-3); //将这一对键值设成元素
}

struct mycolor{
char *name;
unsigned char red,green,blue;
}Color[]={

© 版权声明

相关文章