Skip to content

Getting Started with Lua

Levi Webb edited this page Jun 14, 2015 · 14 revisions

notice: this is a very premature introduction to Lua. If you want to get a more comprehensive intro, check out the official Lua docs. Note some functions may behave slightly differently than their standard Lua counterparts.


In Consoles, there's programmable computers that have a very simple Lua API and interpreter (implemented via LuaJ), and can be hooked into game world events, and can interact with the computer's file-system, input, screen sessions, and input.

To start programming, make a computer, and place it down. Create a file using the following:

> touch myprogram

Then you can open up an editor for your new file (for how to use the editor, see this page):

> edit myprogram

Now, you can write valid Lua code and have most of the core language features work out of the box.

When you want to run your program, just type the name of your file:

> myprogram

The Lua programming language

Lua is an easy to learn and simple programming language that is used as a powerful embedded language, and is sometimes used in game programming and servers! There's better places to learn how to program in lua, but I'll try to introduce you a bit.

Functions and Variables

A function is something that you can 'call' in Lua. It can do anything, and sometimes needs you to give it certain information so it can perform the task that function was designed to do. Think as functions like commands in Minecraft, where you have to give it certain information to work, and it will do things when you type it correctly!

For example, the print(str) function prints text out to the console, but you need to put a string (text) in the brackets for it to work:

print("Hello World!")

Notice how we put quotations around Hello World!? Well, we do that for any text in Lua, so we can tell the computer what part is text, and what part is the actual code. The text we gave the print function is called an argument in programming.

There's also functions without any arguments! Like the read() function:

input = read()

We're calling the read() function, which waits for the user to type something into the computer! But there's some new stuff here: the input = part tells us to store the information that this function gave us into a variable. In english, this means:

call the function read(), and then set the variable 'input' to the returned value

The information that a function returns is called a return value, and the read() function returns a string.

So, now the output of the read() function was stored in input, but how do we use that? Well, there's one use:

print(input)

We can use the output of read() that we stored in input as our argument for print(str)!

Conditions

Conditions in programming are checks for something. We can take information from variables and use their information to check for things, and even execute more code.

Check this out this code:

input = read()
if input == "Bob" then
    print("Welcome back, Bob!")
else
    print("You're not supposed to be here >:(")
end

More new stuff! Let's break it down:

  • input = read() reads input from the console - we've done this already
  • if enters an if statement
  • input == "Bob" is called a comparison. We use == as a way of saying is equal to in Lua. In this case, we check if input is equal to "Bob". If it is, our if statement will execute the code after then!
  • print("Welcome back, Bob!") welcomes a player back to their home
  • else tells the computer to do something else if our last if statement failed, such that the user typed Shirley instead of Bob.
  • print("You're not supposed to be here, >:(") Tells the intruder to go away.
  • and finally, end tells us to end our if and else statements.

Loops

In programming, there's something called loops, which basically allows us to loop through the same piece of code multiple times! I'll jump right into an example:

i = 0
while i < 10 do
    i = i + 1
    print("Counting: " .. i)
end

I added a bunch of new concepts here, let's break this down:

  • i = 0 creates a variable named i, and makes it equal to 0.
  • the while keyword tells the computer to enter a loop
  • t < 10 tells the computer to only loop when t is smaller than ten (note how we say smaller than with <)
  • do means to do all of then following things until we hit end!
  • i = i + 1 means to set i equal to one higher than its current value
  • print tells us to print something
  • "Counting: " is a string, but
  • .. i after our string tells us to combine "Counting: " and the variable i by using ..!
  • and finally, end takes us out of our loop.

Phew! There's a lot more than just loops in this piece of code. Check out this one:

for i = 0,9 do
    print("Counting: " .. i)
end

This does exactly the same thing, except we do it a different way:

  • for tells the program to enter a special kind of loop, called the for loop.
  • i = 0,9' tells the for loop to cycle between the values 0-9, and use the variable i` to store the current count
  • print("Counting: " .. i) is just like it was before, a combination of the string "Counting: " and i being printed to the console

Main function

While you can just write your program outside of a function, you can also declare a main(args) function, which will be ran when the program has been loaded, and after the chunk has been called:

function main(args)
    print("This is in the main function!")
end

More functions

Consoles has a lot of functions unique to its computers. Check out this page for details about all the functions you can call for computers.

Where's the hard stuff?

If you're feeling good about your programming skills, you can check out graphics programming to be able to draw to the screen manually.