Adding a Member Function to a C ++ Class Associated with Lua

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?

+4
source share
3 answers

I think I understand this question and can have an idea about this decision. Technically, there is no connection between the lua monster class and the C ++ monster class. When you call the lua member function on a given lua object, it does not know the specific Monster object in C ++. If you want to call a non-static method of a C ++ object, you cannot use the lua C function to do this. You will need to have user data attached to your lua object that has a pointer to a C ++ object (be very careful with the lifetime of the object), you must use full user data and override __gc in lua with a C function that destroys the object C ++). In this case, you can declare a private static C ++ method in the Monster class that expects this user information, and then returns a pointer and calls a non-stationary member function for this particular C ++ monster object with these arguments. (I hope I understand your question and that my answer is written clearly enough.)

0
source

When you write

 function Monster:pounce(howhigh, bonus) 

this is a shortcut for

 Monster.pounce = function(self, howhigh, bonus) 

So it’s obvious that this is called

 Monster.pounce(badguy, 5, 1); 

how did you do it.

But you want to do something else: on your C ++ module you will get a table called Monster . You do not want to manipulate this table yourself, since it (only?) Contains an entry called create , the Monster constructor.

I have to admit that I'm not completely getting the code you contacted, but provided that the monster method gets access to the meta-furniture, you can insert the pounce method into the meta-furniture.

0
source

it is impossible to call your C-Object / Function directly with all its parameters. You must register the C function in your Lua state. This function should look like this:

 static int myfunc (lua_State *L) { // your code return X; /* X = number of results */ } 

This function receives only the Lua-state parameter. All Luafunction call parameters are on the lua stack. Then you need to jump out of Stack and call your C ++ method.

Function registration is very simple and is performed with two lines of code:

 lua_pushcfunction(l, myfunc); lua_setglobal(l, "myfuncnameinlua"); 

For more information about this, see lua programming in this chapter .

What you want to do, the Lua-Object implementation is a little more complicated, because you need to register a meta-furniture to create a Lua object, but your Lua to C interface is still of an explained type function.

You can also rely on the implementation of Lua-Object in a book by Roberto Jerusulisch in chapter 28

I hope this helps you

-3
source

Source: https://habr.com/ru/post/1384369/


All Articles