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");
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_registeris just a macro:There’s nothing that say you couldn’t do it yourself more specifically.
If you have a global table
PNthat you want to register Lua functions into, you do the following:PNtable onto the stack, usinglua_getfield(L, LUA_GLOBALSINDEX, "PN").lua_pushcfunction(L, Color).lua_setfield(L, -2, "Color").lua_pop(L, 1).