Hello!
I'm Lunar (as you probably realized) and I do occasionally program. For those who don't know how to program, this post will teach you the basics should you want to! Be warned, the computer will only read correct code. Simple mistakes will make your program fail to execute, so make sure that you are doing things right. I will be using the LUA language for this post because IMO it is one of the simplest out there.
NOTE: THIS IS AIMED AT PEOPLE WHO HAVE NO CODING EXPERIENCE WHATSOEVER
ALSO: If this post doesn't go here, could a staff member please tell me? Thank you.
So, what is programming? Programming is the art of creating code that a computer can execute to run various tasks. Again, in this case we will be using the LUA language. If you want, you can download this wonderful mod for Minecraft called ComputerCraft which allows you to program computers to do ingame Minecraft tasks using LUA: computercraft.info. Requires Forge.
At this point, you need to choose between using the mod above (ComputerCraft) or writing lua programs for the command line. If you are using the command line, you will need a text editor of sorts (Notepad++, Atom, I personally use Brackets.)
After you have an IDE (Integrated Development Environment) you will need to make a file where you can write your code.
Now that we have our tools sorted out, let's get into the basics. Open your text editor (or if using ComputerCraft, type "edit <filename>" without quotes, and replace <filename> with whatever you want your file to be called.) We will want to take a look at a few basic concepts.
COMMENTS
Comments are peices in a code file that are not executed. In LUA, you can make a comment by starting a line with "--". This will make lua ignore the line, so you can write yourself notes so you know what your code does with these.
VARIABLES
Variables are, well, variables that can store data. For example, you could store a number like this:
local MyNumber = 5
And then, you can refrence MyNumber and it will say 5. You can also do this with text:
local MyText = "Hello!"
Note that numbers do not require quotes, while strings (text of any sort) do require them. Keep this in mind.
FUNCTIONS
Functions are important. Functions can hold code so that you don't need to keep rewriting it, but instead just "call" the function. You can make a function like so:
function MyFunction(MyNumber, MyText)
print(MyText)
end
But wait! What does print() do?
Print is a function. But if I'm printing text, why does it not need quotes? Because it is a variable.
This may seem complicated, but you'll get the hang of it soon enough. Before we move on, let's take a quick look at using functions.Let's look at a few more useful things in the Lua language before you are tasked with writing some basic code.
OPERATORS, CONDITIONS, AND LOOPS
These are probably the most important parts of the language. An operator is like an operation in arithmetic. For example, we could do basic arithmetic like this:
local MyNumber = 10
local MySum = MyNumber + 15
MySum would then equal 25 (10 + 15 = 25). This also works with other operations like so:
local MyNumber = 30
local MySum = MyNumber + 5
local MySubtraction = MyNumber - 10
local MyProduct = MyNumber * 30
local MyQoutient = MyNumber / 5
So you see division uses "/", multiplication "*", and addition and subtraction their normal operators. Now, for conditions.
Conditions are very useful and important. Here's an example - if the number given is 5, it will print "The number is 5." However, if the number is not five, it will print "The number is not 5."
local MyNumber = 5
if MyNumber == 5 then
print("The number is 5.")
else
print("The number is not 5.")
end
So, I used an "if statement." You can also use this with booleans, true or false values. Like so:
local MyBoolean = true
if MyBoolean then
print("MyBoolean is true!")
end
The "else" is added on for conditions that do not equal the "if statement's" required conditions. You can also use elseif, if you want to test for one of multiple things:
local MyNumber = 7
if MyNumber == 9 then
print("9")
elseif MyNumber = 7
print("7")
else
print("Not sure")
end
You can add as many elseifs as you like, but only one "else" statement.
Lastly, loops. You can do "while:"
local MyBoolean = true
while MyBoolean do
print("MyBoolean is true!")
MyBoolean = false
end
This would print "MyBoolean is true!" once, as it set MyBoolean to false afterwards. You can also do a slightly more complicated one, the for statement.
This is a bit tricky. Here's an example:
for i=1,10 do
print("I have said something "..i.." times!")
end
This will repeat itself 10 times - that's what the 10 is there for. You'll also see I said a variable and some text - using the .. operator. This is an "append" operator - it tells it to add more things on. You can use this like you saw above.
Lastly, the break statement. This terminates a loop like so:
while true do
print("Heh, you can't stop me!")
break
end
But the break statement terminated it!
For now, that's it! I'll add more soon, but it's getting late for me.
I'm Lunar (as you probably realized) and I do occasionally program. For those who don't know how to program, this post will teach you the basics should you want to! Be warned, the computer will only read correct code. Simple mistakes will make your program fail to execute, so make sure that you are doing things right. I will be using the LUA language for this post because IMO it is one of the simplest out there.
NOTE: THIS IS AIMED AT PEOPLE WHO HAVE NO CODING EXPERIENCE WHATSOEVER
ALSO: If this post doesn't go here, could a staff member please tell me? Thank you.
So, what is programming? Programming is the art of creating code that a computer can execute to run various tasks. Again, in this case we will be using the LUA language. If you want, you can download this wonderful mod for Minecraft called ComputerCraft which allows you to program computers to do ingame Minecraft tasks using LUA: computercraft.info. Requires Forge.
At this point, you need to choose between using the mod above (ComputerCraft) or writing lua programs for the command line. If you are using the command line, you will need a text editor of sorts (Notepad++, Atom, I personally use Brackets.)
After you have an IDE (Integrated Development Environment) you will need to make a file where you can write your code.
Now that we have our tools sorted out, let's get into the basics. Open your text editor (or if using ComputerCraft, type "edit <filename>" without quotes, and replace <filename> with whatever you want your file to be called.) We will want to take a look at a few basic concepts.
COMMENTS
Comments are peices in a code file that are not executed. In LUA, you can make a comment by starting a line with "--". This will make lua ignore the line, so you can write yourself notes so you know what your code does with these.
VARIABLES
Variables are, well, variables that can store data. For example, you could store a number like this:
local MyNumber = 5
And then, you can refrence MyNumber and it will say 5. You can also do this with text:
local MyText = "Hello!"
Note that numbers do not require quotes, while strings (text of any sort) do require them. Keep this in mind.
FUNCTIONS
Functions are important. Functions can hold code so that you don't need to keep rewriting it, but instead just "call" the function. You can make a function like so:
function MyFunction(MyNumber, MyText)
print(MyText)
end
But wait! What does print() do?
Print is a function. But if I'm printing text, why does it not need quotes? Because it is a variable.
This may seem complicated, but you'll get the hang of it soon enough. Before we move on, let's take a quick look at using functions.Let's look at a few more useful things in the Lua language before you are tasked with writing some basic code.
OPERATORS, CONDITIONS, AND LOOPS
These are probably the most important parts of the language. An operator is like an operation in arithmetic. For example, we could do basic arithmetic like this:
local MyNumber = 10
local MySum = MyNumber + 15
MySum would then equal 25 (10 + 15 = 25). This also works with other operations like so:
local MyNumber = 30
local MySum = MyNumber + 5
local MySubtraction = MyNumber - 10
local MyProduct = MyNumber * 30
local MyQoutient = MyNumber / 5
So you see division uses "/", multiplication "*", and addition and subtraction their normal operators. Now, for conditions.
Conditions are very useful and important. Here's an example - if the number given is 5, it will print "The number is 5." However, if the number is not five, it will print "The number is not 5."
local MyNumber = 5
if MyNumber == 5 then
print("The number is 5.")
else
print("The number is not 5.")
end
So, I used an "if statement." You can also use this with booleans, true or false values. Like so:
local MyBoolean = true
if MyBoolean then
print("MyBoolean is true!")
end
The "else" is added on for conditions that do not equal the "if statement's" required conditions. You can also use elseif, if you want to test for one of multiple things:
local MyNumber = 7
if MyNumber == 9 then
print("9")
elseif MyNumber = 7
print("7")
else
print("Not sure")
end
You can add as many elseifs as you like, but only one "else" statement.
Lastly, loops. You can do "while:"
local MyBoolean = true
while MyBoolean do
print("MyBoolean is true!")
MyBoolean = false
end
This would print "MyBoolean is true!" once, as it set MyBoolean to false afterwards. You can also do a slightly more complicated one, the for statement.
This is a bit tricky. Here's an example:
for i=1,10 do
print("I have said something "..i.." times!")
end
This will repeat itself 10 times - that's what the 10 is there for. You'll also see I said a variable and some text - using the .. operator. This is an "append" operator - it tells it to add more things on. You can use this like you saw above.
Lastly, the break statement. This terminates a loop like so:
while true do
print("Heh, you can't stop me!")
break
end
But the break statement terminated it!
For now, that's it! I'll add more soon, but it's getting late for me.