I'm trying to add scripting with lua to my C++ game engine, and I'm pretty stumped on how to tackle the executing/processing part. I started by creating a ScriptManager which is responsible for preparing sol2 and eventually any binding stuff. Next, since I'm using ECS I created a ScriptComponent which stores the path to the script, and this where I'm stumped.

I suppose the next step would be to execute the scripts attached to entities, but I don't think I want to just loop through entities and execute the script over and over I probably want to execute it once and then call like Update(). An alternative approach I can think of would be to have the ScriptManager listen to events and call the appropriate lua functions, but I feel like the ScriptSystem is the better solution.

I know there's a plethora of ways to go about this, so I'm not trying to look for for a one size fits all solution just trying to gather some suggestions on how I could or how others do it.

  • \$\begingroup\$ The architecture for a scripting system depends on the requirements for the scripts, and those requirements depend on the game genre. Can you tell us what kind of game you are developing the engine for? \$\endgroup\$
    – Philipp
    Commented Jan 30 at 9:16
  • \$\begingroup\$ @Philippim It’s super basic—just a player that can move around, jump on stuff, and maybe have some coins they can collect. What I had in mind with Lua (this might not be the best usage) was being able to get and modify components so I could check if a key was pressed and then modify the move component or respond to collision, and stuff like that. \$\endgroup\$
    – Konjointed
    Commented Jan 30 at 15:15

1 Answer 1


If your game engine is based on the ECS paradigm, then it could make sense if that paradigm is represented in the scripting system as well.

Which would mean that Lua is used to define and implement systems and the (non-standard) components those systems operate on.

For example, if the NPC "Bob" requires a script to have him patrol between the points A and B, then you would have a Lua script that defines both a "BobTheNPCComponent" (which might be created implicitly in case that the system does not need to store any data in it), as well as a "BobTheNPCSystem" that operates on all entities with the components Position and BobTheNPCComponent. Then the content designer who wants to assign that script to the Bob Entity would do so by assigning the script-defined "BobTheNPCComponent" to that entity, which will then cause that entity to be processed by the scripted "BobTheNPCSystem".

How could the script bindings for that look?

You basically need two C++-implemented functions exposed to the Lua scripting context:

  • registerComponent that takes a string for the name of the component and a table describing its members.
  • registerSystem that takes a table of component names, a LUA callback function to execute for each entity with all those components and (if that's part of your ECS architecture) a parameter that says when that system is executed (once, every update, on specific events, or whatever).

The Lua script containing those function calls would be executed once and then call those functions to register its system. The C++ side would then create the corresponding systems and components and call the callback function passed to registerSystem for each entity and pass the component tuple to it.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .