Lua Programming Tutorial

From Wikibooks, open books for an open world
Jump to: navigation, search

Hello there! Welcome to my Lua Programming tutorial. I have a video tutorial series on YouTube that you can check out here. But, in case you prefer text tutorials, this is the WikiBook for you!

To start off with, I'm going to assume you know what a programming language is, how to set up a text editor, how to make a new file and how to change directories in the terminal/command prompt. So, open up your text editor, and let's get going! So, now that you have your text editor open, make a new file and save it as "whateverNameYouWant.lua". Now, why did we save the file as a .lua file? Well, this says that we are going to make a file that runs Lua code. Just remember to always save your Lua files with a .lua extension. It's basically like the ".rb" extension in Ruby, or ".java" in Java, or ".py" in Python. Also, as a tip, it's good to call your main files "main.lua". This is sometimes needed when using a game development API like LÖVE. Don't worry if you don't know what those things are, you can always google them if you want, but they are not important.

Whew! Now that we have all the boring intro stuff out of the way, lets get on with the show! So, the first program you normally write is a "Hello, World" program. This is not needed, obviously, but to keep with tradition we will do this. So, here is the code for a "Hello World" program:

print("Hello, World!")

Now, what the heck did we just do???? Well, lets break it down:


The "print()" statement(a statement is a line of code) basically says "Ok, I'm going to print out whatever you put inside my parentheses!".

("Hello World")

As you know, the print statement prints out whatever we put in the parentheses, and in this case we put "Hello World". But why did we use the double quotes? Why not use single quotes -- Or just no quotes at all?? Well, this is because when we want to print out words in Lua, we have to use either double quotes or single quotes, otherwise Lua's gonna look at us and say "Woah dude! What's up with your code! What are you trying to print out? I'm gonna give you an error because I'm a big jerk!". Well... maybe Lua wont say that in so many words, but it'll be thinking that. So just remember: When you want to print out words(even just single characters), you need to use either double or single quotes. When printing out numbers, you don't have to use quotes at all, Lua will know what you're talking about! He's a big boy and knows stuff about things...

In Lua, anything with double or single quotes is called a "string". So, "Hello World" is a string. Numbers are... well... numbers! If you do something like:


Lua's going to see this and convert the string that you've made into a number. This will not affect the code you write in the editor, but rather at runtime when you're running your program!

Note that strings and numbers are called "Data Types".


1) Chance the string within the print statement to say whatever you'd like. Then try to print out some numbers, one time with double quotes, one time with single quotes, and one time with no quotes at all. Are your results the same each time? Can you explain why?

Exercises are not necessary but may help you understand the language a lot more, and you'll learn more.

That's basically it for a simple hello world program! Hope you learned something!