I worked on how to bind C ++ classes to Lua for use in the game engine, and I had an interesting problem. I follow the tutorial on this site: 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);
I would add the pounce function only to this particular instance of the monster. So I changed the script that he suggested 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 to call the function as a static member of the Monster class (the code for the function remains unchanged):
Monster.pounce(badguy,5,1);
Syntactically, badguy: pounce (5.1) is correct, but it does call the function incorrectly. Am I just doing something wrong, or is it a binding restriction between lua and C ++ / how do I bind two languages?
source share