1

我正在开发一个游戏引擎,并且正在使用 ChaiScript 向它添加脚本。在我的引擎中,我有一个使用模板的简单实体组件系统。它对每个对象都有一个结构,然后是不同组件的结构。每个组件都包含一个指向其父组件的指针。有一个脚本组件,它是所有 ChaiScript 东西所在的地方。

在 C++ 中,要添加和获取组件,我只需编写:

object->AddComponent<Sprite>("path-to-sprite");

object->GetComponent<Transform>().position.x = 10; (位置只是一个结构,其中包含 x 和 y 位置的浮点数

我的 GetComponent 的定义如下所示:

 template<typename T> T& GetComponent() const {
        auto ptr(componentArray[getComponentTypeID<T>()]);
        return *static_cast<T*>(ptr);
 }

和添加组件:

    template <typename T, typename... TArgs>
    T& AddComponent(TArgs&&... mArgs) {
        T* c(new T(std::forward<TArgs>(mArgs)...));
        c->parentObject = this;
        std::unique_ptr<Component> uPtr{ c };
        components.emplace_back(std::move(uPtr));

        componentArray[getComponentTypeID<T>()] = c;
        componentBitSet[getComponentTypeID<T>()] = true;

        c->Ready();
        return *c;
    }

我希望能够从 ChaiScript 做同样的事情,但使用指向脚本组件所具有的父对象的指针。我该怎么办?到目前为止,我所做的只是将基本函数添加到脚本中,例如 OnUpdate() 和 OnInit(),以及将内容记录到引擎调试控制台的函数。抱歉,如果我没有使用正确的术语,请提前致谢。

4

0 回答 0