Printing[]
So you've decided to start programming in a little lua?
First thing you should know how to print lines in lua, otherwise it will be harder to debug your programs.
Function | Info | Arguments |
---|---|---|
print(string) |
prints text on the screen example: print("Best Worlds!") |
string = text displayed on screen |
write(string) | writes text on the screen but doesn't go to the next line when finished | string = text displayed on screen |
Tags: Minecraft lua creating software
Variables[]
Now you may be wondering how you are to have a value that you can change in different parts of your program
Well, it's pretty simple as all you really need to do is type your variable name, then a "=", and then assign the variable to a string or integer value, like so:
var0 = "Put whatever you want here!" var1 = 12345
Note that if you have a string assigned to variable, you MUST surround it with quotes. You don't however, need quotes around a number.
Functions[]
A function is a piece of code that can be called over again after it's been made. Here's an example of a function:
function abc() --Enter your code here to make the function end
Functions can also have variables and look like this:
function abc(d,e,f,g,h) --Enter your code here to make a function end
You can also assign functions to variables like so:
abc = function(d,e,f,g,h) --Enter your code here to make the function end
Functions are useful when you are going to use a series of commands several times, and you don't want to constantly retype the same chunk of code. For example, if I wanted to make a turtle turn around, then move up, then down, then turn back around, and do it all twice, I could write:
turtle.turnLeft() turtle.turnLeft() turtle.up() turtle.down() turtle.turnRight() turtle.turnRight() turtle.turnLeft() turtle.turnLeft() turtle.up() turtle.down() turtle.turnRight() turtle.turnRight() end
OR you can do this:
function dance() turtle.turnLeft() turtle.turnLeft() turtle.up() turtle.down() turtle.turnRight() turtle.turnRight() end dance() dance()
See how the second option is a lot shorter? This was a basic example, but the prinicple remains.
[EXTRA EDIT] A shorter still example is right here:
for i = 1, 2, 1 do turtle.turnLeft() turtle.turnLeft() turtle.up() turtle.down() turtle.turnRight() turtle.turnRight() end
Logic and loops[]
Logic is the core aspect of computers, and will be used in virtually all of your programs.
Let's say you want something to execute another piece of code if it's true,
You would use an IF statement which basically is where if the statement is true it'll execute the code between the "if" statement and the "end" break
An if statement compared to a real life statement
Real Life | In Lua |
---|---|
If you want to go to the park then let's go to the park. |
if wantsToGoToPark==true then goToPark() end |
So you see that it's very easy to remember how to do an if statement. You can also simplify this by removing the "== true" as the if statement checks only if the condition (wantsToGoToPark==true) evaluates to true.
Now you may be wondering how you'd write out a mathematical equation, that's simple you just write it out as you normally would, for example;
Greater than
if x>3 then -- Code goes here end
Less than
if x<3 then -- C
end
e
end
end
nd
end
reater han o
qual to
if x>=3 then -- Code goes here end
Less than or equal to
if x<=3 then -- Code goes here end
However if ,for example, you want to get "true" when the statement is "false", you'd do something like this:
if x~=3 then -- Code goes here end
Now you may be wondering how to do both sides without rewriting the entire code. To do so, just use an "else" statement like so:
if x>3 then -- Code goes here else -- Code goes here end
Gates[]
If you know how to write domains like this: x>4 OR x<6 then you pretty much know how to do an "OR" gate already, for example:
if x>4 or x<6 then -- Code goes here end
Be careful! If the condition on the first side is true, the other side isn't checked unless the first equation is false. For example:
x = 6 if x>5 or x<10 then --The right side of the equation isn't checked because x>5 is true -- Code goes here end
An "AND" gate is similar, it's just that both logics have to result in true:
if x>4 and x<6 then -- Code goes here end
if v1 == v2 and v3 ~= v4 then -- Code goes here end
If you want to do something if the condition is met and something else is not then you must add an else clause
if v1 == v2 then -- Code goes here else -- Code goes here end
But what if you want 3 or more possibilities? Then you need to use the elseif clause. The elseif clause is only executed on certain conditions. For example;
if v1 == v2 then --code elseif v1 > v2 then --But if not, check if this inequality/equation is true. Then do this. --code else --But if not, do this. --code end
So when are we learning about Loops? Well there are 2 mainly used loops:
while loops and for loops
A while loop is a special kind of loop that repeats until its condition is met. So if you want an infinite loop, use this while loop:
while true do --This is an infinite loop, it never stops!
--Code
end
But if you want a loop that stops on a certain condition:
con = 5 met = 0 while met < con do met = met+ 1 sleep(1) print met end
If you want to read a players action like for a password, you would use the "read" function like so:
write "Enter Password: " -- this displays "Enter Password: "(and reads only past here) password = read() -- allows an Input into the variable named "password", which is a string