Jump to content
  • Advertisement
Sign in to follow this  

How to call a method in LUA (the right way?)

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So far I've been able to understand LUA easy enough. I've figured out how to create simple methods and have the main script call them. I've got includes working (err... DoFile's) and I've managed to surface my methods into the scripting environment. Now I've come to some trouble. I've read (almost) the entire Reference Manual on LUA, the one for LUA 5.0, and they make mention of Calling Functions (Section 3.14) and give the following as a code example:
    lua_pushstring(L, "t");
    lua_gettable(L, LUA_GLOBALSINDEX);          /* global `t' (for later use) */
    lua_pushstring(L, "a");                                       /* var name */
    lua_pushstring(L, "f");                                  /* function name */
    lua_gettable(L, LUA_GLOBALSINDEX);               /* function to be called */
    lua_pushstring(L, "how");                                 /* 1st argument */
    lua_pushstring(L, "x");                            /* push the string "x" */
    lua_gettable(L, -5);                      /* push result of t.x (2nd arg) */
    lua_pushnumber(L, 14);                                    /* 3rd argument */
    lua_call(L, 3, 1);         /* call function with 3 arguments and 1 result */
    lua_settable(L, LUA_GLOBALSINDEX);             /* set global variable `a' */
    lua_pop(L, 1);                               /* remove `t' from the stack */
Immediately after that they state "We did this example using only the raw functions provided by Lua's API, to show all the details. Usually programmers define and use several macros and auxiliary functions that provide higher level access to Lua. See the source code of the standard libraries for examples.". Hmm... Now I'm lost, I can't find source in the standard libraries that gives me an example :(. On top of that I want to make sure that the method I'm about to call actually exists. So my questions are: 1) How can I tell if a method exists inside of a script (w/o having to manually parse it before hand)? 2) What is the proper way to call a method once I know it exists? Thanks, - Jeremy PS: I'm guessing that I can make use of light userdata for storage of global variables that will be accessed by the engine and by the scripting engine. Is this a semi-safe assumption (knowing that threading must be taken into account)?

Share this post

Link to post
Share on other sites
Well after much looking I found the answers I was looking for. Here they are in case others are having the same problem:

bool Lua::functionExists(const char* funcname){
lua_pushstring(mL, funcname);
lua_rawget(mL, LUA_GLOBALSINDEX);
bool res = lua_isfunction(mL, -1);
lua_pop(mL, 1);
return res;

And then to Call a function:
void Lua::call(const char* funcname, int nargs, int nretvalues) throw(LuaException){
lua_pushstring(mL, funcname);
lua_rawget(mL, LUA_GLOBALSINDEX);
if (lua_isfunction(mL, -1)){
lua_insert(mL, -(nargs+1));
if (lua_pcall(mL, nargs, nretvalues, 0))
throw LuaException(lua_tostring(mL, -1));
lua_pop(mL, nargs);
throw LuaException(i18n("Lua function '%1' not found.").arg(funcname));
}catch(LuaException& e){
lua_pop(mL, 1); // pop the error message or the func name.

Actually I found quite a nice wrapper that gave me the info I needed. For all you C people out there its at: http://meique.luaforge.net/doxygen/classmeique_1_1Lua.html.

- Jeremy

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!