用于脚本定义类型的自定义RTTI [英] Custom RTTI for use in script-defined types

查看:100
本文介绍了用于脚本定义类型的自定义RTTI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++开发一个允许Python脚本的游戏引擎。下面是一个例子,说明如何在Python中定义函数,然后在某个事件发生时由事件管理器调用。

  #导入在C ++中定义的一些类
从cpp.event导入PlayerDiesEvent,EventManager

def onPlayerDeath(event):
pass

em = EventManager )
em.connect(PlayerDiesEvent,onPlayerDeath)
em.post(PlayerDiesEvent(...))#将调用`onPlayerDeath`

用户还可以从cpp定义新类型的事件:

 事件导入事件,事件管理器

class CustomEvent(Event):
pass

def onCustomEvent(event):
pass
$ b b em = EventManager()
em.connect(CustomEvent,onCustomEvent)
em.post(CustomEvent())

然而,我也希望消息传递系统使用继承,它目前没有。这里是我的意思:

 从cpp.event导入PlayerDiesEvent,EventManager 

class CustomEvent(PlayerDiesEvent) :
pass

def onPlayerDeath(event):
pass

def onCustomEvent(event):
pass

em = EventManager()

em.connect(PlayerDiesEvent,onPlayerDeath)
em.connect(CustomEvent,onCustomEvent)

#通知这两个函数,因为`CustomEvent`是一个`PlayerDiesEvent`
em.post(CustomEvent(...))

#这只会通知`onPlayerDeath`
em.post(PlayerDiesEvent 。))

tld; dr:允许我更容易创建一个自定义RTTI系统与继承工作?如果没有,任何想法我如何实现这更容易,也许更一般(最好是一种方式是不知道我的实际用例)?

解决方案

我不熟悉boost :: python;它可能有一些功能,使这更容易。我只知道一个自定义的RTTI系统工作相当不错:



Qt 元对象系统,其中通过与元对象相关联来添加反射和一定程度的动态行为每个类都知道该类的祖先和方法。它就像Python的类对象。虚拟方法用于从实例获取元对象。 自定义编译器用于生成这些。



Qt的 Python bindings 然后还为Python类创建这些元对象,使它们看起来与元对象系统几乎完全相同。 p>




你需要比Qt的一般解决方案少一点(你不需要关于方法/属性的信息)。因此,不是一个自定义编译器,你可以使用一个C ++类定义宏来存储类的祖先的信息。然后在创建Python子类时更新 __ metaclass __ 中的信息,并在分发消息时查询它。


I'm developing a game engine in C++ that allows Python scripting. Here's an example of how functions can be defined in Python that are then called by the an event manager whenever a certain event occurs:

# Import some classes defined in C++
from cpp.event import PlayerDiesEvent, EventManager

def onPlayerDeath(event):
    pass

em = EventManager()
em.connect(PlayerDiesEvent, onPlayerDeath)
em.post(PlayerDiesEvent(...)) # Will call `onPlayerDeath`

The user can also define new types of events:

from cpp.event import Event, EventManager

class CustomEvent(Event):
    pass

def onCustomEvent(event):
    pass

em = EventManager()
em.connect(CustomEvent, onCustomEvent)
em.post(CustomEvent())

However, I would also like the messaging system to work with inheritance, which it currently does not. Here's what I mean:

from cpp.event import PlayerDiesEvent, EventManager

class CustomEvent(PlayerDiesEvent):
    pass

def onPlayerDeath(event):
    pass

def onCustomEvent(event):
    pass

em = EventManager()

em.connect(PlayerDiesEvent, onPlayerDeath)
em.connect(CustomEvent, onCustomEvent)

# This notifies both functions, because `CustomEvent` is a `PlayerDiesEvent`
em.post(CustomEvent(...))

# This notifies only `onPlayerDeath`
em.post(PlayerDiesEvent(...))

tld;dr: Is there any C++ library that would allow me to more easily create a custom RTTI system that works with inheritance? If not, any ideas how I might implement this more easily and perhaps more generally (preferably in a way that is agnostic to my actual use case)?

解决方案

I'm not familiar with boost::python; it might have some feature that makes this easier. I just know of a custom RTTI system that works pretty well:

Qt has a meta-object system, where reflection and a degree of dynamic behavior is added by having a metaobject associated with each class that knows about that class's ancestors and methods. It's kind of like Python's class objects. A virtual method is used to get the metaobject from instances. A custom compiler is used to generate these.

Qt's Python bindings then also create these metaobjects for Python classes, making them look almost identical to native QObjects classes as far as the meta-object system is concerned.


You need quite a bit less than Qt's general solution (you don't need info about methods/properties). So instead of a custom compiler, you could go with a C++-class definition macro that stores information about the class's ancestors. And then also update the information from the __metaclass__ when a Python subclass is created, and query it when you're dispatching a message.

这篇关于用于脚本定义类型的自定义RTTI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆