简体   繁体   中英

Call Lua function from C++, missing argument

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);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM