- Part 1. Intro to ECS and basic principles
- Part 2. Implementing the basics
- Part 3. Controlling entities in Lua by calling C++ functions.
If you haven’t read the first part of the tutorial, I suggest you to read it, so you have a clear idea what’s going on. See the second part for the implementation details.
This article uses LuaBridge and if you’re not familiar with it, I suggest you to check out my tutorials about it. (Pt1, Pt2). If you’re using another Lua/C++ binding: that’s fine, you can totally implement everything I’m talking about with other bindings.
Intro
One of the coolest things in Lua is that you can call C++ functions from it. You can even register your own classes and call their member functions!
Suppose you have an entity class which contains a list of components:
class Entity { ... private: std::vector<std::unique_ptr<Component>> components; };
Suppose that you have a function which lets you get a component by its name.
What if you want to change animations in some Lua script? You can do it like this:
function someLuaFunction(entity) ... local graphics = entity:getComponent("Graphics") if(graphics) then graphics:setAnimation("someAnimation") end ... end
I believe that this is not a greatest way to do things, because exposing components to Lua is not really necessary. Woudn’t it be nice to hide all the implementation details in C++? Here’s how the code above may look:
function someLuaFunction(entity) ... entity:setAnimation("someAnimation") ... end