Lua Tutorial #1 – The Setup

Author Kevin

I’m guessing by now that my regular visitors have grown tired of watching my new blog change WordPress themes from day-to-day and occasionally go berserk as I fiddle with all the blog settings and break the PHP code. With that said, I’ve decided to set things right by writing a series of tutorials on the Lua scripting which is loosely based on the course that I teach at the Guildhall at SMU.

Lua Logo
Lua is a rather popular scripting language for game design. If you don’t believe me just check out this list of games that use Lua. Clearly, anyone serious about game programming and especially learning scripting for level design, which is where Lua shines brightest, should take a moment out of their busy schedule and get too know Lua.

Ok, enough with the sales pitch.. let’s just jump in and gets this party started!

Installing Lua for Windows

The first thing I need you to do is download Lua and get it installed on your system and the fasted way to do this is to simply install the Windows for Lua binaries which not only contains Lua already compiled and ready to go but includes  numerous other libraries that you may find helpful in the future. The version that I downloaded was LuaForWindows_v5.1.4-40.exe. Other than downloading it, the install is the standard fare… click next, next, next, etc… you know the drill.

Install the ConTEXT editor

Next, we need a text editor (preferably free) aimed at programmers which will make coding and running our Lua scripts a bit more organized. The text editor I use for my Guildhall course is ConTEXT which allows me customize the highlight of my Lua code and execute scripts from the editor with a simple push of a button. The version of ConTEXT that I downloaded was ConTEXTv0_986.exe. If there is another text editor you would prefer to use, please feel free to use it, but keep in mind that these tutorials will be written as if you were using ConTEXT.

Once you’ve downloaded and installed both Lua and the ConTEXT editor please follow these steps to finish the setup.

  1. First off, we need to setup ConTEXT to execute our Lua scripts by pressing the F11 key.
    1. Open ConTEXT and select from the menu Options->Environment Options.
    2. On the Environment Options dialog select the Execute Keys tab.
    3. Click the Add button and add a new extension called “Lua”
    4. Select the new Lua extension in the tree view and set the Execute path to “C:\Program Files (x86)\Lua\5.1\lua.exe” This path maybe different on your computer depending on your OS version where you installed Lua For Windows.
    5. Set the Parameters edit box to “%n”.
    6. Click the Apply button and close the Environment Options dialog.
  2. Finally, add my custom Lua highlighter file to ConTEXT. This is optional, but it makes the code look really sweet instead of plain ol’ black-on-white and who doesn’t want that? Further instructions are included in the zip file.

When done, the Environment Options dialog of ConTEXT should look like this:

Testing Our Development Setup

The last thing we’re going to do is test our development setup by creating a new Lua script and attempting to execute it from ConTEXT. To accomplish this, follow theses steps:

  1. Open ConTEXT and create a new file by selecting from the menu File->New.
  2. In this new file type the following line: print( “Hello Lua!” )
  3. Save the file by selecting from the menu File->Save and name it “hello.lua”. You can save it anywhere you want.
  4. Finally, hit the F11 key to execute the script.
  5. If everything works, you should see the the words “Hello Lua!” printed out in ConTEXT’s Output Console.

When done, ConTEXT should look something like this:

ConTEXT + Lua Test

In the next tutorial, we’ll start leveraging our new development setup to learn the basic syntax of Lua. If you have any troubles getting any of this working, please feel free to reply below.

9 Responses to “Lua Tutorial #1 – The Setup”

  1. Patrick says:

    Hey professor. You ought to charge them. :)

    • Kevin says:

      Patrick, I think you’re missing the point of publishing to a blog. It’s a good thing for you that I can’t change your final grade! Anyway, shouldn’t you be doing your homework or something?

  2. Horace Zaman says:

    Gosh, I’ve been looking about this specific topic for about an hour, glad i found it in your website!

  3. Laurent says:

    Thanks for these. It’s really interested.

  4. I just bookmarked your post, thanks.

  5. Mikael says:

    A nice little tutorial to set up a tutorial. I´ve got a question, or rather a problem. I downloaded LuaGL and want to run the libs in conTEXT. But it don´t work, it gives me errors and keeps viewing a string error. The code used is not faulty. I would appreciate help to load libs in ConTEXT, thanks.

    • Kevin says:

      Well, I have no experience with LuaGL. Do yo have a trivial LuaGL script you could provide that I could test with? What does this error, you mentioned, say?

  6. Mikael says:

    Yes.  here is a link:
    You should download the Win32 and Win64 dll libs and of course the examples to test, whether it works or not.
    exception message: [string.
    I think it didn´t load the libs, because the error is in the 1 line and it points at that the program doesn´t know the commands.
    Kind regards, Mikael

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.