Before we start
Lua is a lightweight scripting language that still has many uses today.
Roblox Studio uses it and even has special APIs for they're scripts.
It is also very suitable for a beginner's language.
This is the contents:
- Part 1:
- Comments
- I/O
- Data types
- Variables
- Tables
- Part 2:
- Basic operators
- If statements
- Loops
- Functions
- Part 3:
- OOP in Lua
- Exception handling
- Sources used
Lua Crash Course part 1
In this tutorial, I will explain the basics of the Lua programming language.
I will try to be clear and concise with everything so that you can easily learn this.
More stuff:
- D language crash course
- Making a menu bar with HTML and CSS
So let's begin.
Comments
What are comments?
Comments are part of a program that's not run.
A single line comment starts with --
and will make the rest of the line not run.
Example:
-- Single line comment
A long comment begins with --[[
and ends with ]]
.
Example:
--[[
Long comment.
Can extend from many lines to part of on line.
Throws error if not ended with:
]]
Yes, beware. The program will throw an error if the long comment is not ended.
I\O
The first program you will probably write for any given programming language is something that writes something similar to "hello world" in the console.
This is extremely simple in Lua. You use the print
function
print("hello world!")
This is the same as Python and Swift.
We have done output to the console. Now let's do console input.
We accomplish that with io.read
io.read()
Unlike other programming languages, you cannot give a prompt string in the parenthesis
Basic Data types
Lua is dynamically typed.
Because of that, you can change the variable type anytime.
But the values have seprate types. Here are a few basic ones:
boolean
- A boolean value (true
/false
)number
- Any regular numberstring
- A sequence of charactersnil
- Nothingtable
- A Lua table (explained later)
You can also see what data type a thing currently is with the type
function:
type(<thing it will check the type of>)
Example:
type("this is a string type btw")
Variables
Variables are an essential part of Lua (and many other programming languages.)
Well what are they?
Think of them as values stored under a name.
They have the following syntax:
<name> = <value>
Since lua is dynamically typed, variable types can change at any time.
And also, as you guessed, you can use the type
function with variables too.
Example for declaring variables:
variablesAreCool = true
numExample = 791.32
stringExample = "this is an example of".." string concatenation" -- string concatenation combines things and makes them into a new string.
fancyBoolean = 3 != 4 -- this is a condition. They are common in control flow (explained in next part.)
So now you have a bunch of variables and values. Good job!
Tables
What are tables?
Tables are basically your way to make arrays in Lua.
Unlike most other programming languages, tables have indices starting at 1.
If the stuff in there has a name, then it doesn't have an index.
Example:
tableExample = {4, "im number 2", false}
You can also have OOP based on tables, which will be explained in a future tutorial.
And so ends Part 1 of my Lua crash course. Tell me how to improve this and of typos and stuff.
Please Don't cycle farm.
@AbhayBhat im not cycle farming
@nk1rwc, if you make a "crash course" or something, make it all in one tutorial. That will result in higher quality posts.
Of course, I cannot stop you but this is just my request/ recommendation: Make 1 Long post instead of many small ones.
@AbhayBhat three != many
i have tried and failed to make a series with many tutorials. this series is just three tutorials
@nk1rwc, oh well. Completely your choice..
@nk1rwc,I really liked your D tutorial though!
@AbhayBhat thx just busy working on the next part of the lua crash course
@AbhayBhat My stance is if there’s literally a 200 page book on the “basics” of it, break it up. If it can be covered in ~2 hours: don’t.
How do you get the input from
io.read()
to the rest of the code?