Learning to Love Lua
[p]Hi everyone! Welcome to another Tuesday dev blog. Today we’re going a bit “under the hood” and discussing the Lua scripting language – what it is, what it does in Dandelion Void, and why we chose it![/p][p][/p][p]Lua (Portuguese for “Moon”) is an ultra-lightweight programming language with only 23 keywords. Its stylistic simplicity and high performance make it popular as an embedded scripting language in video games. If you've played Garry's Mod, Project Zomboid, Don't starve, Roblox, or any of a dozen other games, you may have already enjoyed the benefits of Lua without knowing it![/p][p][/p][p]Before we get started, some housekeeping: some of our team will be out of office next week, so our next dev blog will be in two weeks on Tuesday September 30th.[/p][p][/p]
[/p][p]As an interpreted language, Lua skips the compilation step. There’s no delay to see your changes take effect, and you can even edit files while the game is running. This is a godsend when you’re doing rapid iteration on a game design feature, or trying a lot of different solutions for a bugfix. [/p][p][/p][p]One of the other advantages Lua has over C# is that you don’t need to pre-declare all your data fields in advance; you can just make them up on the fly! This removes some pre-planning steps and makes it fast easier to write Lua in an “off the cuff” style.[/p][p][/p][p]These qualities aren’t just why we enjoy working in Lua – they’re also the reason it’s the programming language of choice for modders![/p][p][/p]
The 3-layer Cake
[p]When we say that Lua is embedded as a scripting language, this means that there are actually three programming languages involved in Dandelion Void. [/p][p][/p][p]The first is the C++ code used to write the Unity game engine. This comes to us already compiled, so we don’t touch it directly. Next up is C#, the standard programming language for Unity games. Things that you might consider our core technical systems – assembling renderers for entities, loading and unloading chunks, etc. – are all written in C# by us.[/p][p][/p][p]And then all the way at the top there’s Lua, where most of our “gameplay code” lives. Lua is what tells your hunger to tick down and eventually diminish your health, Lua is what determines the context menu options when you right click on a piece of furniture, and Lua is what tells the music system to play the combat theme when enemies approach.[/p][p][/p][p]Now, why would we go through the trouble of splitting ourselves between both C# and Lua? There are a few answers![/p][p][/p]Advantages of Lua
[p]C# is a compiled language. In practical terms, this means that every time you want to make a change and test it in the game, you need to wait a few seconds (around 5 to 30 depending on the project) for the code to compile. You also can’t recompile while your game is running, so if you want to make a change you have to stop the game, wait for the code to recompile, and start again. This can really slow down development![/p][p]As an interpreted language, Lua skips the compilation step. There’s no delay to see your changes take effect, and you can even edit files while the game is running. This is a godsend when you’re doing rapid iteration on a game design feature, or trying a lot of different solutions for a bugfix. [/p][p][/p][p]One of the other advantages Lua has over C# is that you don’t need to pre-declare all your data fields in advance; you can just make them up on the fly! This removes some pre-planning steps and makes it fast easier to write Lua in an “off the cuff” style.[/p][p][/p][p]These qualities aren’t just why we enjoy working in Lua – they’re also the reason it’s the programming language of choice for modders![/p][p][/p]
Lua in Game Development.
[p]Engineering and game design are different skillsets, and at large companies these roles are frequently performed by separate people. The simplest form of this team dynamic would be for a designer to come up with the ideas and then ask the engineer to implement them, but this is pretty inefficient. Important details can be lost in the game of telephone, and each person has to find something else to do while the other is working.[/p][p][/p][p]Lua and other scripting languages offer a nice solution. Lua code is far simpler than C++ or C#, and it’s pretty easy for designers to pick up. Once a designer is comfortable in the scripting language they can implement their ideas directly and test the results in real time, while the engineers can continue to support them by adding new hooks from Lua into the main engine of the game. Having Lua separated from the rest of the codebase is also a safety measure, as it means designers won’t be tempted to tamper with sensitive engine code that could lead to huge bugs or crashes.[/p][p][/p][p]The same qualities that make Lua good for designers also makes it good for modders. Lua’s simple elegance makes it accessible to hobbyists of many skill levels, whether or not one has a formal computer science education. The lack of a compilation step or time-consuming infrastructure also means that if you only have a few hours each day or week to spend on modding, that precious time isn’t being eaten up by loading and busywork.[/p][p][/p][p]Perhaps most importantly, Lua is good for modders because modders are used to using it in other games! Our game director Brian got his start as a modder, and creating a modding-friendly environment has always been a top priority for us. If you’ve already modded Roblox, Garry’s Mod, Project Zomboid, or similar games, you’ll already have a head start for modding Dandelion Void! [/p][p][/p]break, return, end
[p]I have to admit, I rolled my eyes a little when Brian told me he was integrating a Lua scripting layer into our game. Each of our engineers has over a decade of experience working with C#, why put something else in our way? [/p][p][/p][p]But the more time I’ve spent working in Lua, the more I’ve come to appreciate its loose, gestural style. Writing C# and C++ is like building with wooden planks; you have to measure and cut all your material before you’re ready to build, and if you change your plans halfway through, you might need to just start from scratch. Lua is like working with clay; you can just get your hands dirty, squish it into the shape you want, and embrace happy accidents! There’s a certain element of spontaneity to Lua that captures the original joy of programming that drew us to this trade in the first place.[/p][p][/p][p]I’ve been having quite a lot of fun working in our Lua codebase, and I hope you do too! Until then, take care and have a great week – we’ll see you back on the 30th![/p]