This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Doyle3694's profile picture

Variables - what are they and how do we use them? Also covering globals and locals!

Started by Doyle3694, 23 December 2012 - 12:52 AM
Doyle3694 #1
Posted 23 December 2012 - 01:52 AM
Hello everyone, this is the first tutorial I ever make, so bare with me if I make any mistakes. Please leave feedback and if I say something incorrect or missleading, please point it out. Anyways, tutorial follows!

Today, I'm going to teach you about the basics of variables, their useages, and globals and locals.
First of all, there are 3 main types och variables. There are more, but these are the most basic, and knowing these 3, you'll get pretty far.
Strings: These are probably the variables you are most used to. Strings are a bit of text, that is enclosed by quotes(" ").

"So while I'm a string"
I am actually not a string
Another thing to notice is that if your text is not enclosed by quotes, lua will think it is a variablename.

Numbers: These should also be very familliar to you. Numbers are what we use in any math. Lua can count pretty much any math such as + (addition), - (subtraction), *(multiplication), /(divison), ^(power of) and %(mod). There also are some more advanced stuff such as math.sin(). While a string can't perform any math, numbers can. Even if the string is "1",

"1" * "1"
Would give us a syntax error. Why is this? Lua doesn't know how to multiply 2 strings with each other. It wont work like that. Well, what if I wanted to get 2 user inputs and subtract one with each other? read() will give me a string, right? Well yes, read() will give you a string, and for that, we have the lovely function tonumber()
tonumber() will give you a number from a string, if this string is containing a number, that is. If the string however, doesn't contain a number, or if it contains any kind of letter, tonumber() would return nil, which is an undefined variable. If you have ever coded in any other language, nil is what in most languages is refered as null. But what if you want your number to be made a string? not a problem! For that, we have tostring()! tostring() works almost like tonumber(), except in the other direction!

Booleans: These are new to most people who have yet to code in any language. A boolean is a condition, or rather true or false. Booleans are most often used in if statments, which builds upon true or false. So just as well as 4+4 equals 8, 4 == 4 would equal true. == is an operator. == will check if both values given match, and if so, it will return true. We have 6 operators given to us in lua: == (is equal to), ~= (is not equal to), < (is less than), > (is greater than), => (is equal to or greater than), =< (is equal to or less than). Because of conditions and how they work, booleans are often hidden, while numbers and strings are abit more obvious.

Now you might wonder: "Well Doyle, I now know of the 3 main variables, but how do I use them?"
Well the answer is quite simple really:

local string1 = "I'm a string"
local number1 = 13
local boolean1 = false

the template is <variable name> = <variable value>. So if I would like to make the subtractor I talked about earlier, it could look something like this:

print("Welcome to your basic subtractor!")
write("Enter the first num: ")
num1 = tonumber(read())
write("Enter the second num: ")
num2 = tonumber(read())
print("Those 2 numbers subtracted would equal " .. num1-num2)
"Wait a second, whats that .. in the last print?"
In lua, and most other languages, we can't just put a ton of variables in 1 single print and expect it to work, we'll have to seperate them in some way. We need to use something called concatenation, which basically means, adding 2 variables to each other. Concatenations will always yield a string, no matter what. Because the string is the line of text. Even

1 .. 1
Would yield us a string.

"Enough about concatenations, what's that 'local' you used when you declared a variable? Is it important?"
local is kindof a prefix to your variable. A local variable is limited to only your current block(scope). A thumb rule really is to don't define a local in a block, aka in something that ends with a 'end', unless you know what you are doing ofcourse. A example is this piece of code:

local a = "1st block"
if 1 == 1 then
   local a = "2nd block"
end
print(a)
While you would think it should print "2nd block", it would actually print "1st block". Why is this? Where i set a to "2nd block", I used local, which made that variable scoped in to that block. So when I got out of the block, the local variable there was gone. However

local a = "1st block"
if 1 == 1 then
   a = "2nd block"
end
print(a)
will print "2nd block". Because I didn't use a local at the second declaring, a is still scoped out to the first block.

The opposite of a local is called a global. You don't actually need to type

global number = 7

A variable will, by itself, automatically be a global, if you don't specify 'local' that is. A global variable can be edited by any piece of code anywhere on the computer, which could leed to potential problems. So a good rule of thumb is to use locals wherever possible, assuming that you are fully scoped out.

That was all I had for now, you'll probably see more getting added after I get some feedback etc. Another useful function that I forgot to mention is type(), which will return the type of the variable you specify by in the arguments. Anyways, Peace out!
Dlcruz129 #2
Posted 23 December 2012 - 07:22 AM
Very nice!
anonimo182 #3
Posted 23 December 2012 - 11:56 AM
Very complete tutorial covering about all the variables and stuff!
Doyle3694 #4
Posted 23 December 2012 - 11:58 AM
Glad both of you liked it! Any ideas for next tutorial to make? See any tutorial missing?