【问题标题】:C++ Expose Already Existing Instance of Objects to a Scripting LanguageC++ 将已经存在的对象实例暴露给脚本语言
【发布时间】:2012-04-19 11:20:16
【问题描述】:

所以,我希望能够在脚本语言中修改已经实例化的 C++ 对象。我一直在用 LuaBind 研究 Lua,用 SWIG 或 Boost::Python 研究 Python,但我看到的只是如何创建对象的新实例,但我想修改已经存在的实例。 示例:

C++:

Player playerOne = new Player();

脚本语言:

playerOne.Transform.x += 5;

这是否可能,如果可以,您会建议使用什么语言/库来实现这一目标?

【问题讨论】:

    标签: c++ python scripting lua


    【解决方案1】:

    在我的主要项目中,我们使用 LuaBind,它运行良好。我们基本上做你要求做的事情。我们有现有的 C++ 对象,我们希望以各种方式扩展其行为,但是在对象本身的 C++ 代码中对行为进行这些更改将是大量工作和风险。

    因此,在您的示例中,您至少需要 2 个 C++ 包装类 - 一个代表“游戏”,允许您编写 API 函数来返回玩家,以及一个包装类,用于包装您的 C++ 玩家类可以返回lua。每个包装器函数都将具有 api 函数/属性,这些函数/属性将摆弄它在内部包装的单个对象,lua 可以调用并向其传递值。这是一篇文章的链接,它为您提供了使用 LuaBind 的非常简单的示例及其外观:

    http://blog.nuclex-games.com/tutorials/cxx/luabind-introduction/

    【讨论】:

      【解决方案2】:

      我最近需要做同样的事情。我也考虑过(并使用过)Boost.Python,但就个人而言(尽管我很喜欢 Boost),我觉得将 Boost 库的一半拖入其中以获得一个功能有点过头了。

      所以,如果您有兴趣,我最近实现了一个非常轻量级的 Python 包装库,名为 ECS:Python。 ECS:Python(Embedded C++ Scripting with Python)专为希望将对象从 C++ 应用程序公开到嵌入式 Python 解释器以进行交互式脚本编写的 C++ 开发人员而设计。

      它的免费 (BSD) 和开源:http://sourceforge.net/projects/ecspython

      【讨论】:

        【解决方案3】:

        没有一种机制可以magic 将值从宿主语言转换为脚本语言。如果您希望在脚本语言中可以访问特定的对象实例,则必须通过某些函数将其赋予给脚本语言。

        这与任何其他正确封装的 C++ 类型没有什么不同。如果对象 A 创建并存储了某个实例 T,那么对象 B 获取它的唯一方法是调用 A 上的返回 T 的函数。

        【讨论】:

        • 我想我可以做一些事情,比如调用像 getPlayers(); 这样的函数。或返回指向玩家的指针列表或其他正确的东西?
        【解决方案4】:

        我在项目中遇到了问题。看看我在 Ogre3d 论坛上的帖子: http://www.ogre3d.org/forums/viewtopic.php?f=5&t=41631&p=332200&hilit=mrmclovin#p405204

        代码示例:

        int main (int argc, char * const argv[])
        {
          try
          {
            // Initialize the python interpreter
            Py_Initialize();
        
            // Create a module dynamically
            object module((handle<>(borrowed(PyImport_AddModule("NameOfMyModule")))));
        
            // Retrieve the module's namespace
            object main_namespace(module.attr("__dict__"));
        
            // Put a c++ class named "Car" exported using boost.python into our module
            main_namespace["Car"] = class_<Car>("Car")
                                                       .def("drive", &Car::drive)......;
        
           // The class car now exists in a dynamic module
           // and that module is accessable everywhere as longs as the python interpreter exists
        
           // Create a instance of Car here
           Car* myCar = new Car(...);
        
           // Now simply add it to the module. Make sure you have exposed class Car before adding instances
           main_namespace["car_instance"] = object(ptr(myCar)); // the boost python ptr() class make sure not to copy the pointee but only copy pointer adress
          }
          catch( error_already_set )
          {
            PyErr_Print();
          }
        
          return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2010-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-09
          • 2016-11-27
          • 2012-01-03
          • 2018-06-19
          相关资源
          最近更新 更多