Reduce and Simplify Lua Usage
Think long and hard about every instance where you use Lua over C++, understand why the flexibility of an interpreted script is more valuable than writing C++, why waste a precious context switch from C++ to Lua if you don't actually gain anything for it?
- Is it easier to tweak it in a Lua script than in C++, is your recompilation time for your engine/game really a bottleneck in iteration?
- Does the code actually change frequently, or is it something that you may only write once and revise sparingly?
- Is this a critical component that will get called many times per entity in your game loop, and in turn, have a performance hit? If so, it's probably better suited for C++ just to avoid running in a VM and having to eat the context switching costs.
- Is this something that you want users/modders to be able to tweak themselves? If so, Lua may be a fitting candidate, if the performance hit is worth it.
- Are you implementing scripting too early in your project? What if you are wasting your time developing the scripting system or scripts themselves? Scripting things makes them more flexible to be loaded and configured without program recompilation, but it comes with a heavy price. You must spend time working on your script integration, you must spend time debugging your scripts without your native debugger, you must spend extra time working on optimizations trying to make up for the cost of using a scripting language.
I think you might be happier replacing your entire Lua system and stockpile of scripts with C++ until you actually think you fit the criterion of using a scripting language over native C++. Using 4 functions in 20 some scripts per entity in your game loop does not seem remotely necessary unless you really need every component to have the flexibility of Lua at every step (I personally think that is never necessary).
However, if you still consider Lua necessary, I do have the following tips:
- Reduce Context Switches between Lua and C++: Don't call a Lua function from C++ that loops through a list of object and calls C++ functions that call Lua functions. That back and forth between C++ and Lua is difficult for the CPU to handle.
- Precompile Lua scripts, Cache references to functions and Lua files so Lua doesn't waste time finding Functions and reinterpreting files.