How to Get Started with Lua

Lua is a text-based programming language, which is a lot of fun. The possibilities of this program are virtually endless. It takes a bit of time to get the hang of, but with a bit of practice making and running programs becomes addictive!...
Method 1 of 2:

Example

aBc=42 (This will make a variable called aBc and will store the number 42 i it. You can also modify the content of a variable.) A=1 A=(A+32) */This will first set A as 1, and then add 32 to it. You can even do multiplication *, division /, and subtraction -. You can also add another variable to a variable./* A=1 D=3 A=(D-A) */This will subtract A from D, and storing it in A/* 
  1. How to Get Started with Lua Picture 1How to Get Started with Lua Picture 1
    Variables can be whatever you want, like these examples.
Troll123='i like pie' */This will store the string 'i like pie' in variable 'Troll123'/* 
  1. How to Get Started with Lua Picture 2How to Get Started with Lua Picture 2
    There are things called strings in lua. These can be stored in variables like numbers, but they are text. All strings must have quotes 'around them.
  2. How to Get Started with Lua Picture 3How to Get Started with Lua Picture 3
    See these examples:
A='Some cheese' */This will store the string 'Some cheese' in the variable A. These are mainly used to tell the user things./* 
  1. How to Get Started with Lua Picture 4How to Get Started with Lua Picture 4
    This is the main command for talking to the user in the program. you can print variables or strings.
  2. How to Get Started with Lua Picture 5How to Get Started with Lua Picture 5
    It will type the text or number you want onto the screen.
  3. How to Get Started with Lua Picture 6How to Get Started with Lua Picture 6
    For strings, all you need to do is type 'print' then a space, then the string in quotes, as in this example.
print "Hello world!" 
  1. How to Get Started with Lua Picture 7How to Get Started with Lua Picture 7
    For variables, you need to type 'print' and then the name of the variable in brackets, as shown.
A=42 abc='Hello World!' print (abc) print (A) 
  1. How to Get Started with Lua Picture 8How to Get Started with Lua Picture 8
    This will print whatever is in the variable. You don't have to have a set variable, either. You can print an equation, as shown.
A=3 print (A+42) */This will print the number 42+3, which is 45./* 
  1. How to Get Started with Lua Picture 9How to Get Started with Lua Picture 9
    Halfway through a string you want to print, if you type n then it will print the rest of the text on a new line. For example:
print "Hello everyone!nHow was your day?" */will print 'Hello everyone!' On one line then 'How was your day?' On the next./* 
  1. How to Get Started with Lua Picture 10How to Get Started with Lua Picture 10
    You need a way of letting the user enter information for the program to use. When this is in the program and the computer gets up to it, the computer will stop and not go onto anything else in the code until the user has entered in some information. This is where the user input command comes in. Here is what it looks like:

{{{1}}}

  1. How to Get Started with Lua Picture 11How to Get Started with Lua Picture 11
    It looks a bit complicated, but don't worry, it's not. all you have to worry about is the 'A' at the start. This is the variable in which the information is stored. It can be anything, and once it's entered you can do whatever you want with it.
D=io.stdin:read'*l' print (D) */This will let you type in whatever you want, and then it will print it out again. It stores whatever the user types in into the variable 'D', and when told to print out the variable 'D' it prints out whatever the user typed in./* 
  1. How to Get Started with Lua Picture 12How to Get Started with Lua Picture 12
    Try to find out what this program will do, just as some practice.
print "Type in a number." n=io.stdin:read'*l' print "Now type in a second number." g=io.stdin:read'*l' print "Those two numbers added together are:" print (n+g) 
    1. What does this program do and how does it work?
      How to Get Started with Lua Picture 13How to Get Started with Lua Picture 13
  1. How to Get Started with Lua Picture 14How to Get Started with Lua Picture 14
    Comments are lines of code put into the program to help you know what sections of the code do what. As long as one of your lines of code starts with two dashes -- then the program will ignore whatever comes after it on that line. They are purely there to help you keep track of your program.
  2. How to Get Started with Lua Picture 15How to Get Started with Lua Picture 15
    For example, if you wanted to mark a section of code which is used to add two numbers together you would add this comment.
Method 2 of 2:

Calculating section

  1. How to Get Started with Lua Picture 16How to Get Started with Lua Picture 16
    The computer would ignore this line, as long as it starts with the two dashes.
  2. How to Get Started with Lua Picture 17How to Get Started with Lua Picture 17
    These are sections of code which allows you to only run that section of code if certain variables are certain things. Whenever you want in your code you can add one of these, and then enter some code into it. It will only do that code if the variables you say are equal to a certain thing. Once you have finished the code in the statement you must type a line of code which simply says 'end' without the quotes and it will tell the computer that that is the end if the code in the statement.
  3. How to Get Started with Lua Picture 18How to Get Started with Lua Picture 18
    on the line of code that starts with 'if' you then have a space, then the name of a variable, then what operation (== means equal to, ~= means not equal to, => means bigger than etc) then the line must end with 'then'.
A=1 if A==1 then print "Hello world!" A=0 end 
  1. How to Get Started with Lua Picture 19How to Get Started with Lua Picture 19
    You can even have statements inside other statements, but just be sure you have the right amount of 'end's at the end. You can also have more than one variable, by saying either 'and' or 'or'.
A=1 B='cheese' if A==1 and B=='cheese' then print "Hello world!" B=1 A=737 if A=>2 or B~='cheese' then print "Hello again!" end end 
  1. How to Get Started with Lua Picture 20How to Get Started with Lua Picture 20
    This can be useful for a variety of things, for example this. It will ask you what your favourite animal is, then says it likes that animal too. If you type in something completely different then it will say it doesn't understand you. Try to see how it works.
print "What do you prefer:ndogsncats" a=io.stdin:read'*l' if a=='dogs' then print "I like dogs too!" end if a=='cats' then print "I like cats too!" end if a~='dogs' and a~='cats' then print "I do not understand." end 
  1. How to Get Started with Lua Picture 21How to Get Started with Lua Picture 21
    This is very similar to the 'if' statement, but instead it will keep looping through the code inside it until it is told to stop. This is how you use it.
B=0 while B<=21 do B=(B+1) print (B) end 
  1. How to Get Started with Lua Picture 22How to Get Started with Lua Picture 22
    This program will print the numbers from 1 to 20 and then it will stop, because it got above 20. A lot of Lua programs are just massive 'while' loops with a whole lot of 'if' statements in them.
  2. How to Get Started with Lua Picture 23How to Get Started with Lua Picture 23
    You use this in your 'if' statement, at the end. If the variables don't match to anything on the 'if' line, then it goes onto the 'else' section.
A=42 if A==12 then print "This text shouldn't come up." else print "But this text should." end 
4 ★ | 1 Vote