Welcome to Learning Lua

Welcome to my Lua learning journey. I was originally going to include learning Lua as part of my getting up to speed on PICO-8, but I knew PICO-8 uses a subset of Lua and didn't want to conflate or confuse the two. I've decided to break Lua out separately.
So, what is Lua? Lua is an embeddable scripting language. It's used in a lot of games such as World of Warcraft and Roblox, in small devices and as a standalone language. And as I've mentioned PICO-8. It's small and can be compiled from source code by pretty much any C compiler and is actually just a C library.

Please note, this is not meant to be a set of tutorials, but more my notes and thoughts on learning Lua.


Installation


Lua comes preinstalled on RaspberryPi systems. I've decided to use it on my new Mac with the ARM M1 chip. A quick check in terminal shows that Lua is not installed (which isn't a surprise.) The main site for Lua is
https://lua.org. I doubt there is a version that runs on the new system, so I decide to download and build from source. The webpage has a nice script on the right hand side under "Building".

curl -R -O http://www.lua.org/ftp/lua-5.4.2.tar.gz
tar zxf lua-5.4.2.tar.gz
cd lua-5.4.2
make all test

I copy and paste this into Terminal and everything runs fine. Now to install it rather than just testing the build, you have to run:

sudo make all install

The sudo is required since the command needs permission to write to the man directory. Everything runs fine and a quick check shows that Lua is now installed.

Screen Shot 2021-01-09 at 9.19.49 AM

We're good to do. Let's dive right in.


Variables


Variables are straightforward. There are no real types in Lua. You just assign a value to a string.

Screen Shot 2021-01-09 at 8.46.52 AM

Referencing a variable (such as "c", above) returns nil. Unassigned variables return nil. Printing a variable is just as easy.

Screen Shot 2021-01-09 at 8.51.18 AM


Lua has two types of comments: line and block. Line comments start with —

Screen Shot 2021-01-09 at 8.53.36 AM

And block comments which are delimited by —[[ ]].


Screen Shot 2021-01-09 at 8.53.47 AM



Data Types



Lua uses implicit typing. Types are not declared, and you can reassign a different typed-value to a variable. The following is valid:

Screen Shot 2021-01-09 at 8.58.02 AM

Lua values can be:
  • nil - unknown or invalid
  • String - array of characters in double-quotes
  • Number - any real or decimal number
  • Boolean - true or false
  • Function - just what it sounds like
  • Table - what other languages call dictionaries. Key-value pairs.
  • Userdata - structures defined in "C".
  • Thread - parallel code.

You can determine the type of a variable by using a built-in function called type. The function type always returns a string.



Screen Shot 2021-01-09 at 9.06.37 AM


That's enough for now. Next, I'll dive into each of the value types.


This site does not track your information.