Lua脚本推送类函数PN.click()
我正在将 Lua 脚本合并到我的 iPhone 游戏实现中,效果非常好!
纯粹出于美观原因,我希望 Lua 中的函数采用 PN.function() 的格式。目前它们的格式是function()。
我尝试这样注册该函数:
lua_register(lua, "PN.Color", Color);
但它不允许我在 Lua 脚本中调用它。
有人有什么建议吗?
谢谢!
回答了我自己的问题!:
lua_newtable(lua);
int pn = lua_gettop(lua);
lua_pushstring(lua, "Click");
lua_pushcfunction(lua, Click);
lua_settable(lua, pn);
lua_pushstring(lua, "Release");
lua_pushcfunction(lua, Release);
lua_settable(lua, pn);
lua_setglobal(lua, "PN");
I'm incorporating Lua scripting in my iPhone game implementation and it's working great!
For purely cosmetic reasons, I'd like for my functions in Lua to be in the format of PN.function(). Currently they are in the format of function().
I've tried registering the function as such:
lua_register(lua, "PN.Color", Color);
But it won't let me call it in the Lua script.
Anyone have any suggestions?
Thanks!
Answered my own question!:
lua_newtable(lua);
int pn = lua_gettop(lua);
lua_pushstring(lua, "Click");
lua_pushcfunction(lua, Click);
lua_settable(lua, pn);
lua_pushstring(lua, "Release");
lua_pushcfunction(lua, Release);
lua_settable(lua, pn);
lua_setglobal(lua, "PN");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
.
作为 Lua 中的函数名称。如果您尝试将所有 Lua 函数放入名为PN
的全局表中,那么您必须实际执行此操作。请记住:
lua_register
只是一个宏:没有什么可以更具体地说明您不能自己完成。
如果您想要向其中注册 Lua 函数的全局表
PN
,请执行以下操作:lua_getfield(L) 将
。PN
表推入堆栈,LUA_GLOBALSINDEX,“PN”)You cannot use
.
as a function name in Lua. If you're trying to put all of your Lua functions in a global table calledPN
, then you have to actually do that.Remember:
lua_register
is just a macro:There's nothing that say you couldn't do it yourself more specifically.
If you have a global table
PN
that you want to register Lua functions into, you do the following:PN
table onto the stack, usinglua_getfield(L, LUA_GLOBALSINDEX, "PN")
.lua_pushcfunction(L, Color)
.lua_setfield(L, -2, "Color")
.lua_pop(L, 1)
.