1

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.

2
  • 2
    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 Commented May 21, 2019 at 6:51
  • @Vlad Thank you for the link but I could not find what I need from the C API section. Commented May 21, 2019 at 6:57

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.