• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c 使用lua 示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static int say_hello(lua_State *L)
{
	const char *s = lua_tostring(L,1);
	printf("hell,%s\n",s);
	return 0;
}

int main(void)
{
	lua_State *L = luaL_newstate();;
	luaL_openlibs(L);
	
	//写入全局变量

	lua_pushnumber( L, 211 );
	lua_setglobal( L, "hp" );

	//lua_pop(L,2);

	//读取全局变量
	lua_getglobal( L, "hp" );   
	int res = lua_tointeger(L, -1);
        printf("%d\n",res);

	lua_pop(L,1);

	//设置一个有名称的table
	lua_newtable(L);

	lua_pushinteger(L, 10025);
	lua_setfield(L, 1, "id");

	lua_pushinteger(L, 3);
	lua_setfield(L, 1, "good_count");

	lua_pushinteger(L, 100);
	lua_setfield(L, 1, "count");

	lua_pushinteger(L, 15);
	lua_setfield(L, 1, "outlet_count");

	lua_setglobal(L,"customer_table");
		
	//读取table中的值
	int result;
	lua_getglobal(L,"customer_table");
	lua_pushstring(L,"count");
	lua_gettable(L,-2);
	result = lua_tointeger(L,-1);
	printf("%d\n",result);
	lua_pop(L,1);
	
	lua_pushstring(L,"outlet_count");
	lua_gettable(L,-2);
	result = lua_tointeger(L,-1);
	printf("%d\n",result);
	lua_pop(L,1);

	//函数相互调用	

	lua_register(L,"say_hello",say_hello);


	const char* dosomething = " \
		function dosomething(words)\
			say_hello(words) \
		end \
	";

	luaL_dostring(L,dosomething);
	 lua_getglobal(L, "dosomething");
	lua_pushstring(L,"yexuqiang");
        res = lua_pcall(L, 1, 1,0);
	printf("res:%d\n",res);

	
	lua_close(L);	
	return 0;
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua性能优化技巧[一 前言]发布时间:2022-07-22
下一篇:
StepByStep(Lua输入输出库)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap