我有一个关于 boost python 的问题。我一直致力于将项目的一些功能导出到boost python中,但我还没有找到解决以下问题的方法:
我有一组 StatusEffect 对象,我在整个游戏中存储和使用它们。在游戏启动时,我希望能够调用一个 python 脚本来填充/添加到状态效果对象集。我在向 python 公开 StatusEffect 类及其派生类并调用脚本时没有任何问题。
问题是我将 StatusEffect 对象存储在 std::vector 中。 >效果;
除了此处描述的添加静态创建方法的方法之外,我不知道如何创建 boost::shared_ptr
的新实例 http://wiki.python.org/moin/boost.python/PointersAndSmartPointers 鉴于大量的构造函数和我拥有各种各样的派生类,这似乎充其量不是最佳解决方案。我希望能够直接使用 StatusEffect 对象的构造函数创建 boost::shared_ptr 实例,并能够将它们添加到向量中。这可能吗?
答案或一些有用的建议将会有所帮助。我昨天问了一个类似的问题,但不幸的是它没有多大帮助。
提前致谢
I had a question about boost python. I've been working on exporting some functionality of a project into boost python, and I haven't found a way to solve the following problem:
I have a set of StatusEffect objects that i store and use throughout the game. At the game startup, I want to be able to call a python script that will populate/add to the set of status effect objects. I'm having no problems exposing the StatusEffect class and it's derived class to python and calling the script.
The problem is that I'm storing that StatusEffect objects in an std::vector<boost::shared_ptr<StatusEffect> > Effects;
I have no idea how to create new instances of boost::shared_ptr<StatusEffect>
aside from the method of adding a static create method as described here http://wiki.python.org/moin/boost.python/PointersAndSmartPointers Given the large number of constructors and the wide variety of derived classes I have, this seems an unoptimal solution at best. I'd like to be able to create instances of boost::shared_ptr directly using the constructors of the StatusEffect objects, and be able to add those to the vector. Is this possible?
An answer or some helpful suggestions would be helpful. I asked a simialr question yesterday but unfortunately it wasn't of much help.
Thanks in advance
发布评论
评论(1)
我希望我能理解你的问题。如果您使用 如有必要(您可以通过 作为参数,并使用在 python 中创建的
shared_ptr
声明 Python 类,如 http 所示: //wiki.python.org/moin/boost.python/PointersAndSmartPointers,然后boost::python
会自动将你在 python 中创建的StatusEffect
对象转换为 < code>shared_ptr.def
尝试此操作,例如使用const shared_ptr&
或 < code>shared_ptrStatusEffect
实例来调用它。 ,您必须为其创建转换器(从 python 序列开始,然后返回),如文档中所述。有关示例,请参阅 c++ 到 python 转换器模板(第 120 行),python 到 c++ 模板(第 127 行),然后将其用于序列中包含的各种类型(包括shared_ptr's)(第212行)。
然后你可以编写类似
yourObject.listOfStatusEffects=[StatusEffect(),StatusEffect(),StatusEffect()]
I hope I am understanding your question. If you declare your python class with the
shared_ptr
as shown at http://wiki.python.org/moin/boost.python/PointersAndSmartPointers, thenboost::python
will automatically convertStatusEffect
objects you create in python toshared_ptr<StatusEffect>
if necessary (you can try this e.g. by.def
-ing a function which takesconst shared_ptr<StatusEffect>&
orshared_ptr<StatusEffect>
as argument, and call it withStatusEffect
instance created in python.If you want to assign an attribute of type
vector<shared_ptr<StatusEffect> >
, you must create converters for it (from python sequences, and back), that's described in documentation. For an example, see c++ to python converter template (line 120), python to c++ template (line 127), and then using it for various types (including shared_ptr's) contained in the sequences (line 212).Then you can write something like
yourObject.listOfStatusEffects=[StatusEffect(),StatusEffect(),StatusEffect()]