0
\$\begingroup\$

I'm trying to add Lua for scripting to my C++ game engine, which is based on ECS, and I have a rough idea of what I want to do (is it good? probably not) but trying to actually execute it has been rather challenging. The idea I had in mind was allowing scripts to be able to access any entity and their component, so the goal is to have something like this.

local playerEntity = scene:GetEntity("Player")
local playerMoveComponent = playerEntity.Mover

function Update(deltaTime)
    if input.isKeyDown("w") then
        playerMoveComponent:SetDirection()
        -- or
        playerMoveComponent.x = 5
    end
end

The problem is I don't exactly know how to achieve this syntax with sol2 also, my ECS is heavily templated which I just learned makes it difficult to do bindings. For my ECS structure I have a EntityManager I suppose this would be my database which stores the components, components inherit from an interface IComponent, and entities are just a class with an id. if it helps explain things better here's the EntityManager

class EntityManager {
public:
    void startUp();
    void shutDown();

    void update(float timestep);

    Entity newEntity(const std::string& name = "unnamed entity");
    void destroyEntity(Entity entity);
    bool alive(Entity entity);
    Entity getEntityByName(const std::string& name);

    template <typename TComponent, typename... TArgs>
    void addComponent(Entity entity, TArgs&&... args);

    template <typename TComponent>
    void removeComponent(Entity entity);

    template <typename TComponent>
    TComponent& getComponent(Entity entity);

    template <typename TComponent>
    bool hasComponent(Entity entity);

    // ...
private:
    int nextEntityId = 0;

    std::vector<std::unique_ptr<ISystem>> systems;
    std::unordered_map<int, bool> entityAlive;
    std::unordered_map<std::type_index, std::unordered_map<int, std::shared_ptr<void>>> components;
};
```
\$\endgroup\$

0

You must log in to answer this question.