Let's look at another little piece of code.
a = 10 b = 5 c = a + b print(c)
Can you guess what that does? It pushes two numbers into 'a' and 'b' and then adds those together, plonking the result into 'c' and then prints it out (15 if you're finding the maths tough).
Yes, you can use code like a calculator with the only difference being that some of the signs you learned in kindergarten arithmetic are slightly different.
In Lua code (which is what we're learning here):
a + b means 'add a to b' a - b means 'subtract b from a' a / b means 'divide a by b' a * b means 'multiply a and b' a % b means 'divide a by b... and give me the remainder!' a = b means 'make a contain the same thing as b'
Why the changes from English? Why make things more confusing?!
Well, taking multiplication, 'x' is a letter. As for 'divide', there is no 'divide sign' on a keyboard!
The equals-sign is a different matter that we're not going to get into yet. Just know that when you see '=' it means 'make the thing on the left of the sign equal to the thing on the right of the sign'.
Apart from that, things work much the same as writing out basic arithmetic.
c = 12 / 2 * 3
The answer is, of course, 18.
But we can also force the computer to group calculations together in a specific order if we want by using brackets. Why would we do this? Mostly just to be clear what we want.
a = (12 / 2) * 3 b = 12 / (2 * 3) print(a) print(b)
This gives us 18 and 2.
In the case of 'a' the process is 12 divided by 2 (which is 6) times 3, which is 18.
With 'b' it's 12 divided by (2 times 3), which is 12 divided by 6, giving the answer 2.
If that looks complicated just take a bit of time and do it step-by-step. Nobody's timing you.
When you are coding, pieces of code you want to be treated as text are called 'strings'. When you see the word 'string' people are just talking about "A piece of text like this".
a = "My dog is " b = "very sweet" c = "a pain in the butt" print(a .. b)
This is going to tell you what I think of my dog. The '..' in there means 'join this text to the other bit of text'. Notice I left a space at the end of "My dog is ". If I hadn't done that, the result would have been:
My dog isvery sweet
Ugh. The words 'is' and 'very' are squashed together which is enough to ruin my day.
If you want to print out a piece of text on more than one line, then you can use a 'special code' in your text:
It looks like this when used:
print("This is a\npiece of text\nI am displaying on 3 lines")
The output from this would be:
This is a piece of text I am displaying on 3 lines
As I'm sure you've guessed, that '\n' thing says 'Start a new line please!' and allows you to write a one-line sentence in your code and have it turn out looking like third grade poetry. Woo!
Mixing Words and Text
You now have the power to do things with words and numbers. For example:
x = "My age is:\n" y = 109 z = "\nI'm feeling pretty creaky!" print(x .. y .. z)
Which will give you:
My age is: 109 I'm feeling pretty creaky!
We've just learned about:
We've been using the print function for a while now, so it's about time we started learning about what functions are!
I hope this was useful. If you want to try it out yourself, you can use this web-site and type or copy the snippets of code there.
Fluttermind’s director, Dene Carter, is a games industry veteran of over 25 years, and co-founder of Big Blue Box Studios, creators of the Fable franchise for the XBox and XBox 360.