lua-users home
lua-l archive

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


Try just leaving the function on the stack, and use lua_pushvalue() to make a copy of it.

If this is inconvenient, perhaps because the per-frame call happens in a callback, you can create the callback with the function as an upvalue.


On 23-Jul-05, at 6:30 AM, Framework Studios: Hugo wrote:

Hi all!
 
I was just wondering if there is a way to speed-up calling the same function over and over again from C++ to LUA.
 
My point is, let's say we want to call the function "PerFrame()" (just for sake of argument), it would be something like:
 
    lua_pushstring(VM, "PerFrame");
    lua_gettable(VM, LUA_GLOBALSINDEX);
    lua_call(VM, 0, 0);
 
The first two lines are to seek a pointer to the actual function on the stack. It seems rather slow to have to convert the string to function every call. Is there a way to simply get whatever is on the stack (eg. a function) and push it onto the stack later on?
 
I'm thinking something like this:
 
Only one time we do:
 
    lua_pushstring(VM, "PerFrame");
    lua_gettable(VM, LUA_GLOBALSINDEX);
 
followed by something like:
 
    PerFrameFunction = lua_getfromstack(VM, 1);    // so I made up "lua_getfromstack" here...
 
...and per-frame we do something like:
 
    lua_pushtostack(VM, PerFrameFunction);
    lua_call(VM, 0, 0);
 
It would be nice to be able to do the same for a function in a table whilst reserving 'self'.
 
Did I miss how to do this or isn't it possible (yet? :) with LUA?
 
        Thanks,
                    Hugo