将成员函数添加到 C++绑定到 Lua 的类

发布于 2024-12-19 03:55:39 字数 928 浏览 0 评论 0原文

我一直在研究如何将 C++ 类绑定到 Lua 以在游戏引擎中使用,并且遇到了一个有趣的问题。我一直在关注这个网站上的教程:http://tinyurl.com/d8wdmea。看完教程后,我意识到他建议的以下代码:

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

只会将 pounce 函数添加到 Monster 的特定实例中。因此,我将他建议的脚本更改为以下内容:

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

但是,当我调用 pounce 函数时,脚本会中断。经过进一步测试,我能够成功调用 pounce 函数的唯一方法是将该函数作为 Monster 类的静态成员调用(该函数的代码保持不变):

Monster.pounce(badguy,5,1);

语法上, badguy:pounce(5,1)是正确的,但没有正确调用该函数。我只是做错了什么,还是这是 lua 和 c++ 之间绑定的限制/我如何绑定这两种语言?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-12-26 03:55:39

认为我理解这个问题,并且可能知道解决方案。从技术上讲,lua Monster“类”和 C++ Monster 类之间没有联系。当您在给定的 lua 对象上调用 lua“成员函数”时,它不知道 C++ 中的特定 Monster 对象。如果要调用 C++ 对象的非静态方法,则不能使用 lua C 函数来执行此操作。你需要在你的 lua 对象上附加一个用户数据,该对象有一个指向 C++ 对象的指针(要非常小心对象的生命周期 - 你必须使用完整的用户数据并使用销毁的 C 函数覆盖 lua 中的 __gc C++ 对象)。在这种情况下,您可以在需要此用户数据的 Monster 类上声明一个私有静态 C++ 方法,然后使用给定的参数强制转换指针并调用此特定 C++ Monster 对象的非静态成员函数。 (我希望我理解你的问题,并且我的答案写得足够清楚。)

I think I understand the question, and may have an idea of the solution. The is technically no link between the lua Monster 'class' and the C++ Monster class. When you call a lua 'member function' on a given lua object, it has no knowledge of the particular Monster object in C++. If you want to call a non-static method of a C++ object you cannot use a lua C-function to do this. You would need to have a user-data somewhere attached to your lua object that has a pointer to the C++ object (be very careful about object lifetimes - you must use full-userdata and override the __gc in lua with a C-function that destroys the C++ object). In this case, you can declare a private static C++ method on the Monster class that expects this userdata, and then casts the pointer and calls the non-static member function for this particular C++ monster object, with the given arguments. (I hope I understand your question, and that my answer is written clearly enough.)

〆一缕阳光ご 2024-12-26 03:55:39

当你写

function Monster:pounce(howhigh, bonus)

这个时,这是一个快捷方式,

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

调用它

Monster.pounce(badguy, 5, 1);

所以显然像你那样

是有意义的。但您想做一些不同的事情:从您的 C++ 模块中,您获得一个名为 Monster 的表。您不想操作此表本身,因为它(仅?)包含一个名为 create 的条目,这是一个 monster 的构造函数。

我必须承认,我没有完全理解您链接到的代码,但假设怪物的方法是通过元表访问的,您可以在该元表中插入方法pounce

When you write

function Monster:pounce(howhigh, bonus)

this is a shortcut for

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

So obviously calling this by

Monster.pounce(badguy, 5, 1);

as you did makes sense.

But you want to do something different: From your C++ module you get a table named Monster. You don't want to manipulate this table itself, as it (only?) contains an entry named create, a monster's constructor.

I must admit that I don't completely get the code you linked to, but assuming a monster's method are accessed by a metatable, you could insert the method pounce in that metatable.

旧情勿念 2024-12-26 03:55:39

无法使用其所有参数直接调用 C 对象/函数。您必须将 C 函数注册到您的 Lua 状态中。这个函数必须是这样的:

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

这个函数只接收 Lua-State 作为参数。 Lua函数调用的所有参数都位于lua堆栈上。然后你必须从堆栈中弹出并用它调用你的 C++ 方法。

函数的注册非常简单,只需两行代码即可完成:

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

您可以在 本书“lua编程”的这一章

你想要的东西要做到这一点,实现 Lua 对象有点复杂,因为你必须注册一个元表来创建一个 Lua 对象,但你的 Lua 到 C 的接口仍然是解释类型的函数。

您还可以在 Roberto Ierusalimschy 的书中第 28 章中学习如何实现 Lua 对象,

我希望这会帮助你

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:

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

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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文