← Back to context

Comment by aeturnum

1 year ago

I have really enjoyed working with Lua when it's come up and I think it's an extremely good language. In particular, as Noë says, its interface for embedding into C/C++ is clean and flexible. There are performance concerns, but considering how widely Lua is used for game logic in high performance video games clearly those concerns can be addressed.

That said, I think there are other warts that can be confusing and make the language difficult. The Nil-Terminated Arrays section points out one oddity about `nil` - but there are many others. There are a number of confusing corner cases (or were - I haven't used it in a while). As an example, if you have a list of values and you pass that list into a new table, the resulting table may be different than if you add each value in that list in order.

I also think that the flexibility of embedding means that you are often learning the Lua <=> C interface as much as you are learning Lua. So jumping from deploy to deploy is more confusing than most languages.

Nevertheless I fully agree the language is underrated!

> but considering how widely Lua is used for game logic in high performance video games clearly those concerns can be addressed.

It’s ok, but should be much better.

Once upon a time Supreme Commander used Lua. Perf on consoles for SupCom2 was catastrophically bad. We switched to Kore which was effectively Lua but way faster (10x?).

Kore was bought by Havok and rebranded into Havok Script. Havok got bought by Microsoft. I’m honestly not even sure if you can still buy it today. It’s not listed on their website.

These days I think you’d use Luau? I’m not sure. https://github.com/luau-lang/luau

  • LuaJIT is pretty good, but some platforms might not allow it. iOS technically didn't allow it when I used it before, but Apple either didn't _actually_ care or just looked the other way.

> you are often learning the Lua <=> C interface as much as you are learning Lua.

One time I had a memory leak in a game, something like exactly 4 bytes per frame.

It turned out I was not popping the Lua-C parameter passing stack properly after each frame.

I found this out after some unhelpful person suggested that "all programs just leak memory" lol

  • The Lua C API is fantastic from a design perspective, but as you have discovered, it is rather error prone. Fortunately, there are many wrapper libraries that take care of all the stack manipulation. For example, I'm using https://github.com/ThePhD/sol2 to interface with C++ and the API is incredibly ergonomic.

    • How is it fantastic, it’s just barebones stack interface thrown in your general direction.

      Any integration code that doesn’t use an ad-hoc helper library looks like assembly listing (not even in disguise, cause it basically is bytecode but in C).

      8 replies →