sol icon indicating copy to clipboard operation
sol copied to clipboard

Performance question

Open iganinja opened this issue 8 years ago • 4 comments

Hi

I am currently programming a small home brew game and I plan to use Lua along with Sol. My idea is to somewhat use the model exposed by Unity and other game developing packages: the game entities (for example a zombie enemy) will be formed by different components (sprite or graphical representation, collider, etc). These components will be managed by the C++ part, while Lua will be "the client": it just asks for a collider, a sprite, etc. and uses them in some coherent way (for example collider determines the world position of the sprite). This is somewhat analog to how javascript deals with DOM objects provided by the browser.

So to implement that I thought I could create, to expose to Lua, a bunch of functions like the following:

std::size_t newSprite(const std::string& fileName);
void setSpritePosition(std::size_t spriteId, const Vector2D& position);
void removeSprite(std::size_t spriteId);

These functions would be implemented in order to call SpriteController, which with the spriteId would look up in its sprite list and modify the corresponding sprite object.

But on the other hand I can also expose C++ classes to Lua, for example:

class SpriteLua
{
public:
    Sprite(const std::string& fileName)
    {
        mSprite = spriteController.load(fileName);
    }

    void setPosition(const Vector2D& position)
    {
        mSprite->setPosition(position);
    }
    ...
    ...
private:
    Sprite* mSprite;
};

The class approach seems faster and also more convenient to write in the Lua side (just manage Sprite instance instead of spriteSomething + spriteId + parameters, but I don't know the overhead, if it exists at all, of this second approach.

I know this can be a premature optimization case but I think I need to know both approach relative cost, it can decide how to do a substantial part of C++ <--> Lua communication of the game. Also I welcome any other suggestion about this or related issue.

Thanks

iganinja avatar Aug 24 '15 18:08 iganinja