1
votes

I'm trying to rename some functions in Lua so I can call them more easily.

For example, of.getHours() function should be able to be called by ofGetHours().

So after initializing the Lua state, I loaded a Lua string which assigns these global function variables like the following:

luaL_dostring(L, "ofGetHours = of.getHours ofGetMinutes = of.getMinutes");

This method works fine but I would like to do this using the Lua C API to increase the performance and make my code more readable.

I think I need to use lua_setglobal() but I don't know how to properly do it. Any help would be greatly appreciated.

1
It would be good if you started with Programming in Lua book, which has all the answers you will need: lua.org/pil/contents.html - Vlad
@Vlad Thank you for the link but I could not find what I need from the C API section. - Zack Lee

1 Answers

2
votes

Provided the os value is a global table, your code might look something like this:

lua_getglobal(L, "of");
lua_getfield(L, -1, "getMinutes");
lua_setglobal(L, "ofGetMinutes");
lua_getfield(L, -1, "getHours");
lua_setglobal(L, "ofGetHours");
lua_pop(L, 1);