I'm curious about how games like Garry's Mod and ROBLOX allow users to run Lua scripts and interact with the game engine in real time. I know the game engines are typically written in C or C++, but how is Lua actually embedded so that scripts can be executed dynamically? Also, why do many games choose to use Lua for smaller calculations, instead of just sticking to C or C++ for everything?
1 Answer
Lua is an interpreter written in C, making it easy to embed it within your C/C++ game engine. Instead of having a standalone main function, the Lua library becomes part of your program, allowing you to execute Lua code by calling C functions with script strings. You also register specific functions that Lua scripts can call, allowing for interaction between the script and the game engine.

I've been using Lua at work for some internal tools. Basically, the interpreter gets integrated into your C++ code, and you can expose certain functions for Lua to use. It's a lot more efficient than recompiling your whole engine every time you want to make changes to a script.