Minecraft PC IP: play.cubecraft.net
Status
Not open for further replies.

LunariusFlare

Well-Known Member
Aug 9, 2016
100
26
103
United States
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.
 

MagnificentSpam

Forum Expert
Mar 16, 2016
2,306
2,239
298
Hint, hint.

You can put code in a block to make it easier to read.

Code:
local MyNumber = 7

if MyNumber == 9 then
print("9")
elseif MyNumber = 7
print("7")
else
print("Not sure")
end

Hint, hint.

You can indent parts of your code to make it easier to read. :p

Code:
local MyNumber = 7

if MyNumber == 9 then
  print("9")
elseif MyNumber = 7
  print("7")
else
  print("Not sure")
end

I'm sorry, just got triggered there.
 
  • Like
Reactions: Ping Ether

Ping Ether

Forum Veteran
Jun 2, 2016
2,713
6,170
459
20
Italy
Hint, hint.

You can put code in a block to make it easier to read.

Code:
local MyNumber = 7

if MyNumber == 9 then
print("9")
elseif MyNumber = 7
print("7")
else
print("Not sure")
end
Hint, hint.

You can indent parts of your code to make it easier to read. :p

Code:
local MyNumber = 7

if MyNumber == 9 then
  print("9")
elseif MyNumber = 7
  print("7")
else
  print("Not sure")
end

I'm sorry, just got triggered there.
Hint hint, uhm, so wasn't this about nobody using LUA? lel
 

MagnificentSpam

Forum Expert
Mar 16, 2016
2,306
2,239
298
Should also have brackets enclosing the code inside the loops and if-else statements to be on the safe side.

Also, even though it's in LUA, the only difference is syntax and a couple functions. Everything else (concepts, how code works) is almost the same.
Not really. In lua code blocks are between do and end instead of { and }. The curly braces are afaik only used for list(table) initialisation. In the example every line between then and the next else/end is only executed if the condition applies.

Hint hint, uhm, so wasn't this about nobody using LUA? lel
I have had to use lua two times so far, once to write a telegram bot and once to write a blobby volley 2 ai. Although for the telegram bot I cheated and just called a python script to do most of the work.
 

Gainfullterror

Forum Professional
Mar 24, 2016
5,040
10,898
624
22
Breakfast for Dinner Club HQ
www.cubecraft.net
I'm not sure why you would post a thread about LUA, nor if anyone in the entire world even codes in LUA.
A better idea would be to make a thread on Java.
Besides that, I guess its a nice thread.
LUA doesn't have much to do with CCG, but there are plenty of people in the modding community ready to learn LUA since the "Computer craft" mod can be used to automate complex things to your liking.

Someone even made a minecraft version of checkers with it!
 

betty's oldies

Forum Expert
Not really. In lua code blocks are between do and end instead of { and }. The curly braces are afaik only used for list(table) initialisation. In the example every line between then and the next else/end is only executed if the condition applies.


I have had to use lua two times so far, once to write a telegram bot and once to write a blobby volley 2 ai. Although for the telegram bot I cheated and just called a python script to do most of the work.
My bad, but it's still the same concept. Curly brackets in Java/C++ and do/end enclose the code.

I don't know LUA - only know some C++ and java.
 

Sigton

Well-Known Member
Oct 10, 2016
39
10
83
Scotland
Sigton.github.io
Just saying, no-one uses Lua. This causes problems with docs etc.
If your wanting a simple language; learn Python. I've made decent games in Python, and its really easy to learn.
It uses a really simple syntax designed to resemble English, and it has similar OOP to Lua.

Sigton
 

LunariusFlare

Well-Known Member
Aug 9, 2016
100
26
103
United States
As for people saying I should've reviewed Java, I'm more experienced with Lua and I think (and I think many would agree) it's a simple langauge.
 
Status
Not open for further replies.
Members Online

Team online

Latest profile posts

A picture of my cat sleeping having kitty dreams
1000053259.jpg
kfccaleb wrote on WayneX4's profile.
You can now have a follow after I played with you!
How do you make spoilers with a name?
Reesle wrote on ZachTart26's profile.
Welcome to the Official Cubecraft Forums! If you need help with something, feel free to DM me or a staff member anytime! Have a great day! :D
Top Bottom