Programming & Scripting Tutorials

Lua: Hello World!




Ok, so now we have our programming environment setup (from the previous lesson) – let's actually get scripting! Create a new file in SciTE and save it with the '.lua' file extension – then type any code that's in this tutorial and click "Run Program" (the button that has a play icon) to see it run!
The first thing we're going to do is create a simple 'Hello World' program, which outputs exactly that. It's extremely simple, and uses only one line to output the text to the screen.
Before I actually show you the line though - I'm just going to walk you through everything that you need to know to fully understand that line (after all, there is no real point in you writing a line of code that you don't understand!).

A function is the name we give to any named piece of code. Luckily for us, Lua comes with a bunch of functions, which can do basic stuff for us. We can call a function in Lua by writing it's name, and then by putting some brackets, which may or may not have some stuff in. If a function doesn't need any values to do it's job, then we can leave the brackets empty – but if a function needs some values to do its job, we have to give it some in the brackets. Any values that a function takes to do it's job are called parameters.
Functions usually take certain types of data – for example a function which outputs data, will likely take what is called the string data-type. The string data-type simply consists of multiple letters/characters (a simple sentence for example) and is shown in Lua by surrounding the string in double quotes.
For example: "This is a string."
We don't have to worry (generally) about data-types too much in Lua though, since Lua does most of the hard work for us!
So let's just say we had a function called 'print' which outputs text to the screen and it took a parameter of a string, which it will output. If we want to call the function (the name for actually getting the code inside the function to execute) - we might write something like this:

print("Outputting this string!")


In fact, this is the name of a function built into Lua!
The print function takes a string and outputs this to the screen! So in the case of the 'Hello World' application we were hoping to create, we could simply write something like this:

print("Hello World!")


See – Lua isn't that hard after all!
Remember that we could also add a semicolon at the end of the line if we wanted to. My roots as a C++ programmer get to me here, and so I prefer my code to have semicolons to make my code feel tidy – It's entirely up to you whether you think it looks better with or without one to end the line:

print("Hello World!");


It's worth noting that the 'print' function automatically puts a newline at the end of whatever your printing out. So if we used the print function twice (by simply calling the function twice), there would be a line break between the two pieces of text:

print("Hello World!");
print("Printing something else!");


If you don't want this behavior when outputting text, you have two options.
You can either stick the strings together before outputting them (and only call the print function once) or use a different function for the job!

If you want to stick the strings together (or stick any other data-type you want to a string), you are doing what is often called string concatenation (this basically just means joining strings together). You can do this in Lua by using the operator (an operator is simply something that represents a certain action – in this case, the operator represents string concatenation) for string concatenation, which is two dots ('..'). So to in our case, we could do something like this:

print("Hello World!" .. "Printing something else!");


Or because they are both constant strings (the values we've entered in the code aren't going to change when we run it) – we can simply put them both inside one set of double quotes:

print("Hello World!Printing something else!");


The string concatenation operator can be useful though – especially when you aren't only dealing with string data-types (for example if we wanted to join a number to the string) and even more so when we get onto working with variables (values that can change!).

print("I'm number #" .. 1 .. "! I'm so awesome!");


The other way I mentioned of avoiding the extra line-break in print (although to be honest, it's often nice to have the line-break) is using another function entirely.
There is another function for outputting strings in Lua, which is 'io.write' (Input Output Write) – this is basically the same as 'print' except it doesn't output a newline at the end of the string! For example, the below code will output on the same line:

io.write("Hello World!");
io.write("Printing something else!");


And as a sidenote, if you ever want to put a newline into strings – you can do it by writing '\n'. For example, if we wanted the 'print' like behavior, but with 'io.write' – we could do something like this:

io.write("Hello World!\n");
io.write("Printing something else!\n");


This Lua tutorial was written by


Back to Lua

Advertisement: