Hello Lua

Hans Bruins
6 min readMar 20, 2018

--

The programming language Lua was created in Brazil in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes on the PUC-Rio, a university in Rio de Janeiro, where it is still housed in "LabLua".

The name Lua is derived from Portuguese and means moon. One of its predecessors was the data-description language named SOL (Simple Object Language) which is Portuguese for Sun. Since the name Lua is a noun, and not an abbreviation, it should not be written in capitals like LUA, but as Lua.

What is Lua?

Lua is a pretty flexible scripting language; it supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description. It is powerful, efficient, lightweight, embeddable and Lua has a deserved reputation for performance.

Lua is not an interpreted language but generates bytecode from a program (like, for instance Java). This bytecode runs on the Lua Virtual Machine. And while most virtual machines are so called stack-based, Lua VM is different in that it is register-based, and one of the firsts VMs that has a wide use. Being register-based is more CPU-like and similar to how hardware works. The advantage of a register-based architecture is that it avoids to much copying of values and also reduces the total number of instructions per function. Being register-based is probably the reason for its good performance.

The Lua VM has automatic memory management and garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Where can you use Lua?

Since Lua is such a flexible language, it is used in several markets, for instance;

  • Wikipedia uses Lua as its template scripting language
  • The widely used VLC media player is scriptable in Lua
  • Lua is used in industrial applications like Adobe’s Photoshop Lightroom, in embedded systems like the Ginga middleware for digital TV
  • and last but not least: games, Lua is currently the leading scripting language in games. For instance, it is used in the creation of World of Warcraft and Angry Birds.

Let's try it out!

You can try out Lua online (at the Lua Demo) but a fun way to learn Lua is to use a game engine which programming language is Lua. Examples of that are LÖVE, Defold and of course Pico-8.

This article isn’t meant as a how-to-learn Lua tutorial but as an introduction, so that you have an idea what Lua is about. And for this, I will use the Pico-8. The Pico-8 is a fantasy console which I wrote about in a previous article: “Eenie, Meenie, Pico”. In short, it is a program that emulates a complete game console that has limited 8-bits specifications, has all the needed programming tools build-in and its programming language is Lua.

The reason I choose the Pico-8 is that you can use it directly out-of-the-box, no setup or external editors needed, all tools are incorporated, the learning curve is pretty low and it is fun to use. In contrary to the previous mentioned Lua environments it is a paid solution. Personally I think it's worth it but you can always go with one of the others.

It is common to call the first program you create in a new language the "Hello world" program; but, for the programming language Lua, let’s call it "Hello Lua". And, yes, that is a pun at hallelujah.

You can find the Pico-8 here. Install it and run it. You are now ready to start.

After Pico-8 boots up, you will be greeted with a ">" prompt. Type

HELP

to get an overview of the possible commands. Now press ESC and voila, you are in the Lua code editor of Pico-8. Pressing ESC is something that works in all Pico-8 programs; you can stop programs and examine the code this way. Back in the days there was something similar (Run/Stop) you could do with BASIC programs on the C64.

Now, with the editor open, try typing some of these commands followed by enter:

CLS() -- clears the screenPRINT(“HELLO LUA”)RECTFILL(80,80,120,100,12)
CIRCFILL(70,90,20,14)
FOR I=1,4 DO PRINT(I) END

Note that Pico-8 is designed to only display upper-case characters — you can just type normally, there is no need to use Shift or Caps-lock. The editor also has syntax colouring for you code.

If we have a quick look at the top right corner in the editor you'll see some icons. These are the other build-in tools for creating sprite cells, maps, sounds and music.

Now, press ESC again to go back to the prompt, type

CLS

and then

RUN

And, there it is, your first Lua program on the Pico-8!

If you want to store your program for later, use the SAVE command:

SAVE HELLOLUA

If you want to have a bit more interaction with your program, you can use the BTN() function for reacting to keypresses. Also, to have more graphical possibilities we can use two special callback functions called _UPDATE and _DRAW.

The following program allows you to move a circle around with the cursor keys. Press ESC to switch to the code editor and type or copy & paste the following code:

X = 64 Y = 64FUNCTION _UPDATE()IF (BTN(0)) THEN X=X-2 END
IF (BTN(1)) THEN X=X+2 END
IF (BTN(2)) THEN Y=Y-2 END
IF (BTN(3)) THEN Y=Y+2 END
ENDFUNCTION _DRAW()RECTFILL(0,0,127,127,5)
CIRCFILL(X,Y,7,8)
END

Now press escape to return to the console and type

RUN

to see it in action. Again, if you want to store your program, use:

SAVE REDCIRC

These examples are pretty BASIC (haha) but give you a feel for the editor and language. To investigate more interesting examples go to the DEMOS directory. If this doesn't exist already then type

INSTALL_DEMOS

after which you'll find a DEMOS directory with several example programs to explore. For instance, you will find a Hello world example that is a little bit more fancy:

And if you still want more, type

SPLORE

which will open the gateway to explore all programs (called cartridges) on the Pico-8 BBS. With the arrow keys you can scroll through the options and content. Remember that you can use the ESC key to switch to the code editor if you want to examine the created code of a program.

Closing thoughts

I will leave you with 1 final thing: the Tweetjams that you can find on the Pico-8 BBS. As the name suggests, the challenge here is to come up with a (Pico-8) Lua program that fits in a classic tweet of 140 characters. You can also find them on Twitter.

Some pretty briljant programs can be found here, sometimes using all kind of tricks that you won't find in the manual. It is a treasure trove for finding new ways to program. This jam thread really has the old 'demo scene' vibe.

An example of such a tweet you will find below, in this case the program is 139 chars. (I reformatted it in the Pico-8 code editor for a little bit more readability).

t=0 function _draw()cls(2)t+=0.01 for k=0,16 do for n=1,9 do h=k/16+t circ(64+cos(h+t/3)*n*8,64+sin(h)*(n*n+cos(t)*16),n,11-n/3)end end end
Tweetcart Sun by Benjamin Soulé

So, there you have it, a very small introduction to Lua and some suggestions to have fun while learning a new language!

~If you liked the article, click the 💚 below so more people can see it! Also, you can follow me on Medium, so you get updates regarding future articles!~

--

--

Hans Bruins

Thinker, inker, keyboard extrovert, truth-seeker and ignorance disintegrator