7 ms·
Why is Lua so popular among game devs?
by tferris 14y ago
Why is Lua so popular among game devs?
- Jimmie 14y agoIt's fast, dynamic and easy to embed in a C/C++ game for scripting. The language itself is very simple to learn too.
- kevingadd 14y agoIt has most of the positive traits of JavaScript with less of the negative traits. A few examples: Strings in Lua are interned, which makes it possible to efficiently use string keys in all sorts of places instead of numeric enumerations. Lua's garbage collector has support for running incrementally with a configurable pause time, so you can run it in the background every frame to avoid those nasty GC pauses you still get in most modern browser games from stop-the-world collections. Debugging is built into the language with a user-accessible API that lets you write your own debugger or diagnostic tool, in addition to using third-party ones, instead of having to rely on the vendor to provide one. Lua uses a single container type for storing data, with an efficient representation (densely packed elements are stored in an actual array; sparsely packed elements and elements with non-integral keys are stored in a hashmap). As a result, operations on sequences/containers only ever have to think about one data type, and you still get the near-optimal performance you would in JS if you manually used Array/Object where most appropriate.
- lucian1900 14y agoActually, JS Arrays are implemented as hashmaps as well.
- simonster 14y ago> Strings in Lua are interned, which makes it possible to efficiently use string keys in all sorts of places instead of numeric enumerations. Interning is an implementation detail. I don't know how they are implemented off the top of my head, but string keys are pretty damn fast in most JS JITs, since they're used to get e.g. methods of objects. > Lua's garbage collector has support for running incrementally with a configurable pause time, so you can run it in the background every frame to avoid those nasty GC pauses you still get in most modern browser games from stop-the-world collections. Again, this is an implementation detail. Firefox is about to get incremental GC. While the pause time isn't configurable, it doesn't really matter as long as it's short by default. > Debugging is built into the language with a user-accessible API that lets you write your own debugger or diagnostic tool, in addition to using third-party ones, instead of having to rely on the vendor to provide one. Gecko and WebKit both have debugging APIs. I will concede that it's a PITA that they're different. > Lua uses a single container type for storing data, with an efficient representation (densely packed elements are stored in an actual array; sparsely packed elements and elements with non-integral keys are stored in a hashmap). As a result, operations on sequences/containers only ever have to think about one data type, and you still get the near-optimal performance you would in JS if you manually used Array/Object where most appropriate. Again, this is an implementation detail. In JS, Array is a subclass of Object and in most JITs I think it's implemented the same way. The only difference is how they serialize to JSON and that Array has a length property. Lua has an advantage in that it gives arrays both sparse and dense slots, but this isn't necessarily impossible to do in JS either (https://bugzilla.mozilla.org/show_bug.cgi?id=586842 https://bugzilla.mozilla.org/show_bug.cgi?id=586842)
- Derbasti 14y agoLua tables are not an implementation detail. They are the one and only complex data type in Lua and everything (including OO) is built on top of them.
- simonster 14y agoObjects are the only complex data type in JavaScript (sans E4X). Arrays are objects (typeof (new Array()) === "object"), and prototypical inheritance is built on top of objects.
- pygy_ 14y agoThe implementation of the Lua and now LuaJIT2 VMs are part of the reason for the language success. It's had good performance and low memory overhead for almost two decades (the incremental gc came in 2006). Javascript has only been coming to age recently.
- VMG 14y agoSo what? People seem to like Lua for the details.
- zura 14y agoAgree, Lua has its pearls, but I think, today it is used in larger scale projects than it was initially intended. Language-wise, I prefer UnrealScript or similar, with a "classic" OO and preferably with a static typing.
- hermanhermitage 14y agoVery easy to embed and small footprint. Language evolution has been implementation driven more so than say Javascript. As such its been able to reshape itself continually. Mike's LuaJit is an incredible piece of work. Its easily extended.