1

I have strange problem, with calling Lua function from C++. I have in Lua:

Player = 
{
    Number = 0.43,
    Text = "SomeText",
}

function Player:Func(a, b)  
    return (a * b);
end

Before lua_pcall my stack looks:
table
function
3
4

I call this function with:

lua_pcall(L, 2, 1, 0)

And I get error from Lua:

attempt to perform arithmetic on local 'b' (a nil value)

When I change in Lua script

return (a * b);

to

return a;

There is no error, but from lua_tonumber(L, -1); I get value 4 (my second argument in C:/), so it looks that my second argument in C is first in Lua. Do you know what I made wrong in my code ?
How I construct stack:

lua_getglobal (L, "Player");
lua_pushstring(L, "Func");
lua_gettable(L, -2);
lua_pushnumber(L, 3.0);
lua_pushnumber(L, 4.0);
2
  • Obviously your stack is not what you think it is. So why don't you show us the code that sets up the stack? Commented May 27, 2012 at 21:09
  • 4
    Do you maybe need to push a hidden this pointer (or Lua equivalent)? Commented May 27, 2012 at 21:18

1 Answer 1

3

Ben's comment is the key - Read the Object-oriented programming section in "Programming In Lua", page 150.

http://www.lua.org/pil/16.html

The effect of the colon is to add an extra hidden parameter in a method definition and to add an extra argument in a method call.

So you need to push an "Account" object as the first parameter, or (more easily in this case) change function Player:Func(a, b) to function Player.Func(a, b)

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.