I have been working on how to bind C++ classes to Lua for use in a game engine, and I have run into an interesting problem. I have been following the tutorial on this website: http://tinyurl.com/d8wdmea. After the tutorial, I realized that the following code he suggested:
local badguy = Monster.create();
badguy.pounce = function(self, howhigh, bonus)
self.jumpbonus = bonus or 2;
self:jump(howhigh);
self:rawr();
end
badguy:pounce(5, 1);
Would only add the pounce function to that specific instance of a Monster. So I changed the script that he suggested to the following:
function Monster:pounce(howhigh, bonus)
print("in pounce function");
print(bonus);
self.jumpbonus = bonus or 2
self:jump(howhigh);
self:rawr();
end
local badguy = Monster.create();
badguy:pounce(5,1);
However, when I call the pounce function, the script breaks. After further testing, the only way I was able to successfully call the pounce function was by calling the function as a static member of the Monster class (the code for the function stays the same):
Monster.pounce(badguy,5,1);
Syntactically, badguy:pounce(5,1) is correct, but isnt correctly calling the function. Am I just doing something wrong, or is this a limitation of the binding between lua and c++/how I am binding the two languages?
it is not possible to call your C-Object/Function directly with all of its parameters. You have to register a C-Function into your Lua-State. This function has to look this way:
This function receives only the Lua-State as parameter. All the parameters of the Luafunction call laying on the lua stack. You have to pop then from the Stack and Call your C++ method with it.
The registration of a function is quiet simple and done with two lines of code:
You can find more information about that in this chapter of the Book "programming in lua"
The thing you want to do, implementing an Lua-Object is a bit more complicated, because you have to register a metatable to create a Lua Object but your Lua to C interface are still functions of the explained kind.
You can also lean to implement a Lua-Object in Roberto Ierusalimschy's book on chapter 28
I hope this will help you