1

I'm working on a game engine, and I'm working on adding scripting to it using ChaiScript. In my engine, I have a simple Entity Component System that uses templates. It has a struct for each object, and then structures for different components. Each component contains a pointer to it's parent. There is a script component, which is where all the ChaiScript stuff sits.

In C++, to add and get components I simply write:

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

object->GetComponent<Transform>().position.x = 10; (position is simply a structure containing a float for the x and y positions

My GetComponent's definition looks like this:

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

And AddComponent:

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

I want to be able to do that same from ChaiScript, but using the pointer to the parent object that the script component has. How would I go about this? All I've done so far is add basic functions to the script, such as OnUpdate() and OnInit(), as well as a function to log stuff to the engine's debug console. Sorry if I didn't use correct terminology, and thanks in advance.

veridis_quo_t
  • 342
  • 3
  • 14
  • IIRC ChaiScript doesn't have support for templates, but you can always instantiate your templates when binding things to the engine (e.g. `GetComponent()` could become `getTransformComponent()`.) – 0x5453 May 22 '20 at 04:35
  • Yeah that's the solution I figured I'd use if it didn't have support for templates... Just thought I'd check if it did! – veridis_quo_t May 22 '20 at 06:48

0 Answers0