Scripting languages: how and why

What is a “scripting language”?

Languages such as C and C++ are “systems languages“; complex lower-level programming languages that allow the programmer a lot of flexibility and low level access to the system but are very intolerant of mistakes.  These languages assume that you know what you’re doing, and as such make little-to-no effort to protect you from  mistakes.  Due to the lower-level nature of these languages it can also take quite a lot of time and coding to implement more complex functionality.

A so-called “scripting language” is a higher-level language; in most cases still a complete and very capable language, but with the different goals of aiding programmer productivity and protecting you from possible mistakes.  Often a lower-level systems language will be used to implement performance-critical functionality, whilst a scripting language is used to implement higher-level logic and create or control many of the features the end-user sees.

Why use a scripting language?

There are a number of possible benefits to using a scripting language:

  1. Less bugs/errors.  Scripting languages usually help to protect the programmer from the more common errors, meaning you spend less time tracking down potential problems and end up with safer, less buggy code.  It is also less likely that an undiscovered bug in your code will have nasty results in the end-product.
  2. Easier to test.  Because the scripting language does not have to be re-compiled every time a change is made, it is easier to test new code and immediately verify that the results are correct.  You can make a change to the script and continue running the software.
  3. Faster development time.  Thanks to higher-level features, reduced time spent handling errors, and ease of testing new functionality, it often takes less time to produce correctly working code in a scripting language.
  4. More help.  Many artists and designers, or even less experienced coders are able to write and modify simple scripts, meaning they do not have to bother a programmer every time they need a small change or additional feature.
  5. Allow modification or extensions.  If you expose the scripting interface your end-users will also be able to create modifications or extensions for your software with little-to-no extra work on your part.

Are there any disadvantages?

Of course, a scripting language isn’t right for every situation.  In a smaller application it may not be worth the time and effort needed to implement scripting.

There can also be performance concerns.   Scripts aren’t for everything, and it can sometimes be a mistake to implement performance-critical functionality in a scripting language; this is why a systems programming language is often used for those tasks.

What languages could I use?

Three popular and capable choices in the video game industry (and elsewhere!) are Lua, Python, and JavaScript.  These are all good languages with plenty of documentation and libraries to help you get up and running.  Let’s take a quick look at each option:

Lua

Lua is a fast and lightweight but still capable and expressive scripting language that is relatively easy to embed into your lower-level C or C++ code.   The language is supported by an active and helpful community of users and has a plethora of documentation and examples available.  Including Lua will not bloat your executable size unnecessarily, and Lua’s performance is amongst the best of embeddable scripting languages.  You’ll also be amongst good company using Lua, as it is one of the most popular scripting languages in the video game industry.

View the official website at http://www.lua.org/ for more information.

You might get started using the documentation on the official website, with this tutorial series, or with the excellent book “Programming in Lua” written by the creator or the Lua language.

Python

Python is also a very popular language, and has been used in numerous commercial titles.   The simple syntax of this language is sometimes described as “executable pseudocode”, and again there is a wealth of documentation and examples available.  Python is also supported by an active and helpful community — but they can sometimes be harder to get information out of about embedding Python into lower-level code, usually preferring to suggest you should instead consider “extending” Python.

View the official website at http://www.python.org/ for further details.

You might get started with Python using the official documentation on the website (you might be particularly interested in the section on embedding), or with the Boost.Python documentation.  To learn Python itself Bruce Eckel has graciously made his book “Thinking in Python” freely available online.

JavaScript

JavaScript is often thought of as a language that implements functionality on websites — and that’s true — but it is also a fast and capable scripting language that you can embed into your applications.  JavaScript, or variations of it, are used in a number of commercially available games and engines.  Thanks to it’s use on the web there are also plenty of tutorials and examples available.  Rather than a single major package there are a number of alternatives available, but you might start by considering Google’s V8 JavaScript engine, developed for use in their Chrome web browser, or Mozilla’s SpiderMonkey.  Each has plenty of documentation provided, and I’ll leave it as an exercise to the reader to find suitable resources for your choice of JavaScript variant.

Other languages

Some other languages you might consider using include AngelScript, Squirrel, and Ruby.  Again, I’ll leave it to the reader to further investigate these or other options if the above suggested languages have not seemed suitable.