About Us
Products
Purchase
Downloads
Support
Forums
Contact Us
Site
 Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Using Lua in CMUD
  -1: Introduction
  0: For the Novice
  1: Numbers and Maths
  2: Words
  3: Variables
  4: Simple Functions
  5: Tables
  6: Branching
  7: More Flow Control
  8: Writing Functions
  9: CMUD Examples
  10: Beyond This Guide
Related Links:
  0: For the Novice
  2: Words
  5: Tables
3: Variables [[cmud_tut_lua_3]] 
Imagine you're writing a script to repeatedly quote Mary Poppins.


print("Supercalifragilisticexpialidocious!")
print("Supercalifragilisticexpialidocious!")

Supercalifragilisticexpialidocious!
Supercalifragilisticexpialidocious!

It can get to be a bit of a grind if you have to type out a string or a number every time you want to use one, especially if it's something that long and confusing. To save your fingers from too much typing, Lua lets you store the string in the computer's memory and give it a nickname. These nicknames are called variables and the process of storing away a string or a number in the memory is called assignment. The thing that you've stored away in the variable is called its value. Here's that same example but using a variable.


PoppinsQuote = "Supercalifragilisticexpialidocious!"
print(PoppinsQuote)
print(PoppinsQuote)

Supercalifragilisticexpialidocious!
Supercalifragilisticexpialidocious!


NB: Lua is case sensitive. The nickname PoppinsQuote is different to poppinsquote and POPPINSQUOTE. Be careful!

When you used print on the PoppinsQuote variable, Lua actually printed "Supercalifragilisticexpialidocious!". You could think of the variable PoppinsQuote as being a sort of box, containing the value "Supercalifragilisticexpialidocious!".

You can do anything to a variable that you could do to the thing it contains. Here's another example:


PoppinsQuote = "Supercalifragilisticexpialidocious!"
print(PoppinsQuote .. " Even though the sound of it is something quite atrocious!")

Supercalifragilisticexpialidocious! Even though the sound of it is something quite atrocious!

You can change the value of a variable any time you like just by assigning a new value.


length=40
print(length/5)

length=25
print(length/5)

8
5

Variables can't contain another variable. See what happens when you try.


var = 7
var2=var
print(var)
print(var2)

var="seven"
print(var)
print(var2)

7
7

seven
7

If var is a box containing the number 7, then var2=var makes a copy of it, another box that contains the number 7.

Changing the contents of one box doesn't change the contents of another. var2 doesn't contain var, it contains the same thing that var does.

Finally, we can define more than one variable at once, like this:


var1,var2 = 7,"seven"
print(var1)
print(var2)

7
seven

This can save you some space when you're assigning lots of variables at once, but also comes in really handy when we learn some harder stuff.

Tim Toady

You need to be careful to avoid confusion when you're talking about variables - there are Lua variables and there are CMUD variables. From the perspective of a Lua script, a CMUD variable is a place to store values across sessions. Normally, all your Lua variables are lost when CMUD exits, but CMUD variables are saved to disk. If you put whatever's inside your Lua variables into a CMUD variable, they'll be preserved for next time.

I'll talk about the specifics of doing this later on.
Viewer Comments [0 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net