0

for example i have this:

static int callFunction(lua_State* L)
{
 int p = lua_gettop(L);
 if (p == 1 && lua_isfunction(L, -1)) {
  /*
   * now i need something like "get the function thats in the first parametre
   */
 }
 return 0;
}

now i need to get the function thats in the first parametre of the function in this C++ code, sry for not being clear, i suck at explaining.

4
  • 1
    Are you asking how to find a function from a string? Commented Nov 11, 2010 at 22:38
  • You might check out the lunar.h implementation - it may have the answer to your question. lua-users.org/wiki/CppBindingWithLunar Commented Nov 11, 2010 at 22:43
  • 1
    You have the function. You've verified that you have one argument, you've verified that that argument is a Lua function, it's sitting there on the stack waiting for you. Now what do you want to do with it? Commented Nov 12, 2010 at 2:40
  • Exactly, i want to store it in a vector or something Commented Nov 12, 2010 at 11:38

3 Answers 3

1

If you need to call the function, you can use lua_call. Lua however won't allow you to take any sort of useful pointer to Lua functions. If you want to store a function in Lua, then you will have to use the Lua registry to store it.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to store a "pointer" to a Lua function in C++, you could just store the /name/ of the Lua function and then do as DeadMG says and call it with lua_call, as here: http://pgl.yoyo.org/luai/i/lua_call .

If you are stuck with the code you have already, it's kind of a sticky problem; I'm not sure you can get the name of the Lua function from the stack you have. In other words, you may need to modify the code one level up from what you posted.

Comments

0

I'm guessing that you might want to look into the lua_tocfunction() function.

1 Comment

What would the index parameter be then?

Your Answer

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