Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Silly question from a web dev who has never written a line of Lua before (or delved into game development). What is it about the language that make it such a good fit for game development? Seems like every time I come across these engines, or read stuff about game development in general, Lua is always the language of choice. Can anyone explain it in a nutshell for me? Thanks


As others have mentioned, it's got a few things going for it.

    1. It's super easy to embed in an existing code base
    2. The language itself is simple and easy to learn
    3. It has native coroutines, which allows game devs to write code without a spider web of callbacks
    4. For what it does, it is fast


One point that wasn't mentioned: It has a performance-oriented alternative implementation, called LuaJIT.

With this, you can run JIT-compiled Lua code at speeds comparable to V8 (the NodeJS runtime), and almost as good as the Java VM (depending on the workload, YMMV). LuaJIT also includes a foreign function interface (FFI) for even closer integration with native code, which makes it almost trivial to use native libraries from Lua.

Essentially, this makes it easy to move logic to lower/higher levels of the stack when/if you need it: There's the three layers that are increasingly more difficult to use but faster (Lua -> FFI -> C/C++), and you can directly use gamedev-oriented C++ or even graphics APIs like WebGPU from Lua, without crippling performance or writing tons of glue code.

Note that I've worked with Lua for many years and I'm definitely biased. I've also worked with JavaScript/TypeScript-based engines, where my favorite is BabylonJS (it's great, but JS/browsers really aren't...). So if you don't want to learn Lua/C++ I can recommend looking into BabylonJS as a starting point - it will probably be easier to get something going thanks to the browser APIs.


My understanding is that it's simply historically fallen into the niche. Lua early on was relatively easy to embed in C codebases, making it a natural fit for scripting C (or C++) game engines and their editors. So many hugely popular game development tools are scripted with Lua at this point that it's somewhat breaking the mold to use anything that _isn't_ Lua in this domain.


Thanks that makes sense


I think it’s less that it’s particularly good for gamedev and more that it’s particularly easy to embed in an engine, and it’s faster than it has any right to be


> ... and it’s faster than it has any right to be

The main Lua interpreter by PUC-Rio is among the fastest bytecode interpreters for a popular scripting language. Very efficient C code.

Wizard programmer Mike Pall then came along and wrote an even faster Lua bytecode interpreter in assembly language. And added JIT for even faster performance of hot functions.


Appreciate it!


I think it’s because Lua’s C API is very very closely coupled to its native keywords and standard library, so it’s not only built for trivial embedding but also for trivial / mechanical integration. Using the Lua API from within C or C++ is almost like the syntactic language isn’t even there and it’s just a convenient library.

For example:

https://www.lua.org/manual/5.4/manual.html#lua_pushfstring

and:

https://www.lua.org/manual/5.4/manual.html#lua_next

It’s also amazingly semantically simple and dynamic, almost like a Lisp; even though it doesn’t have classes, typelevel programming is pretty straightforward.

It also allows you to easily write and distribute native modules (as shared objects) without needing integration code.

It is also insanely portable.


Probably since you can embed it in other languages quite trivially. For instance, in C after about a dozen lines of code you can now pass around data into Lua and back to C and thus give you access to a scripting language with little fuss. It’s also a fairly small and simple language, so adding it in won’t add much more to the overall footprint of the project.


Interesting, thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: