新项目,准备把行为树挪过来,需要解析行为树的xml配置。因为现在的项目使用的是lua配置,所以想可不可以使用嵌套table来实现xml的功能。
1 lua table 常用方法
lua与其他语言交互都是使用栈进行的,所以熟练掌握各种api对应的栈操作很重要。对于table比较重要的几个api和相应操作如下:
1.1 lua_gettable
lua_getglobal(L, "mytable") <== push mytable
lua_pushnumber(L, 1) <== push key 1
lua_gettable(L, -2) <== pop key 1, push mytable[1]
1.2 lua_settable
lua_getglobal(L, "mytable") <== push mytable
lua_pushnumber(L, 1) <== push key 1
lua_pushstring(L, "abc") <== push value "abc"
lua_settable(L, -3) <== mytable[1] = "abc", pop key & value
1.3 lua_rawget:
用法同lua_gettable,但更快(因为当key不存在时不用访问元方法__index)
1.4 lua_rawset:
用法同lua_settable,但更快(因为当key不存在时不用访问元方法__newindex)
1.5 lua_rawgeti必须为数值键
lua_getglobal(L, "mytable") <== push mytable
lua_rawgeti(L, -1, 1) <== push mytable[1],作用同下面两行调用
--lua_pushnumber(L, 1) <== push key 1
--lua_rawget(L,-2) <== pop key 1, push mytable[1]
1.6 lua_rawseti必须为数值键
lua_getglobal(L, "mytable") <== push mytable
lua_pushstring(L, "abc") <== push value "abc"
lua_rawseti(L, -2, 1) <== mytable[1] = "abc", pop value "abc"
1.7 lua_getfield必须为字符串键
lua_getglobal(L, "mytable") <== push mytable
lua_getfield(L, -1, "x") <== push mytable["x"],作用同下面两行调用
--lua_pushstring(L, "x") <== push key "x"
--lua_gettable(L,-2) <== pop key "x", push mytable["x"]
1.8 lua_setfield必须为字符串键
lua_getglobal(L, "mytable") &l