C++給lua傳遞表

C++程序:

int main()
{
	lua_State *L = luaL_newstate();
	if (L == NULL)
	{
		return 1;
	}
	luaL_openlibs(L);

	//向lua傳遞表
	lua_newtable(L);//創建一個表
	lua_pushstring(L, "name");//壓入key
	lua_pushstring(L, "xiaoming");//壓入value
	lua_settable(L, -3);//寫入表中

	lua_pushstring(L, "age");
	lua_pushinteger(L, 20);
	lua_settable(L, -3);

	lua_setglobal(L, "student");//將棧頂元素設置全局變量

	int ret = luaL_dofile(L, "main.lua");
	if (ret)
	{
		printf("Lua doFile Error !\n");
	}
}

lua程序:

print("student name :"..student["name"])
print("student age :"..student.age)

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章