lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, first I'm new here, never used a mailing list before so bare with my retardation. Also, I don't know C++ worth a crap but I've been learning by hands on tinkering trying to play with a particular C++ function. Shoot I'll just spit it right now:

I am trying to call a function in a Lua table, from C++. Examples. (this works fine)
let funcname equal a const char representative of a function name.

lua_pushstring(L, funcname);
lua_gettable(L, LUA_GLOBALSINDEX);
if(lua_isnil(L,-1))
{
    printf("Tried calling invalid Lua function '%s'\n", funcname);
    return;
}

Lunar<Unit>::push(L, pUnit);
lua_pcall(L, 1, LUA_MULTRET, 0);
return;

The above code snippet works fine provided that 'funcname' is of course a valid function on the global stack. It properly calls the function passing it a userdata argument. However, if I was to pass into 'funcname' a string that represents a method, for example "bugs:attackUnit", it doesn't seem to call the function. I wrote some code to split the string into two parts, then push the first part to the stack, check its relevancy, then push the second part to stack, check its relevancy as a function, then call that function in the same way as above, doesn't work, example:

string str = funcname;
int md1 = str.find(':');

if ( md1 > 1 ) {
    string method = str.substr(0, md1);
    string funname = str.substr(md1+1);
   
    lua_getglobal(L, method.c_str() );
    if ( lua_istable(L, -1) ) {
   
        lua_pushstring(L, funname.c_str());
        lua_rawget(L, -2);
       
        if ( lua_isfunction(L, -1) ) {
       
            Lunar<Unit>::push(L, pUnit);
            lua_pcall(L, 1, LUA_MULTRET, 0);
           
            return;
           
        } else {
            printf("Invalid function '%s:%s'", method.c_str(), funname.c_str());
            return;
        }
       
    } else {
        printf("Invalid table '%s'\n", method.c_str());
        return;
    }
}

it doesn't seem to work and I don't quite understand why as I'm passing it all the same information (so to speak) as the first example. The only difference I can think of is that the Lua stack has an extra element in it, and that being the table from the getglobal(L, method.c_str()). So if my wee brain is right my stack should be something like:

pUnit <- userdata
funname <- function in table method ( method[funname] )
method <- Lua table

where as the first example would be like:

pUnit <- userdata
funcname <- Lua function

What am I doing wrong, should I be popping 'method' off the stack, first before pushing my pUnit to the stack, then calling the function? I've tinkered with this for hours and although it isn't a big deal it does help me understand the Lua stack and how to work with Lua in C++.
Basically, all I want to do is call a function from a table and pass it a userdata as an argument. I've done the RTFM but that thing is pretty much useless unless you are already seasoned. I've gone over countless forums looking for examples but ALL I find are how to get data from a lua table in C++. Which I can do fine, without any issues but when it comes to calling a function from within a Lua table, nadda, or at least the day I spent looking and reading just wasn't enough... aarrrrgh.

Any help would be much appreciated!

Mike