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
1: Numbers and Maths [[cmud_tut_lua_1]] 
Remember how I said that all scripts in CMUD have to be contained in a setting? I wasn't telling the whole truth - you can actually write scripts right on the command line. Pressing Ctrl+R will toggle whether CMUD will check for scripts on the command line. When you hit that shortcut, you'll see a cross appear through the computer icon in the bottom right to show you that script parsing has been turned off.

When you start CMUD, script parsing is on, but it's using the wrong scripting language. By default, CMUD thinks you're talking in zScript. If you typed in some lovely Lua, CMUD will just say "buh?" because it still thinks you're using zScript. You need to tell CMUD that you're speaking in Lua and you do that by pressing Ctrl+'. While you're in "Lua mode", typing say goddamn, why has the price of haddock gone up? will give you a nice Lua error, but it also means that you'll be able to type real Lua code onto the command line without having to create any settings. This is great for testing new code and learning how things work, but pretty useless for actually doing anything practical. But since testing and learning are the whole point of this guide, I don't think that'll be a problem.

So, to get started, open the untitled session and press Ctrl+' to enable Lua mode.

Numbers

Start by typing this on the command line:


print(4+3)

I'm sure you can guess what this is going to do before you even press enter. print just writes out whatever you tell it to, just like your real printer. You could do exactly the same thing by typing print(7) or print(10-3).

Arithmetic

+ and - are used for addition and subtraction. To multiply, you use * and if you want to divide, use /. Here are some more examples:


print(1.5+2.5)
print(5*2)
print(4.6-1.6)
print(4/3)

4
10
3
1.3333333333333


You now have the makings of a real calculator. If you want to do some more complex problems, you can use parenthesis (brackets to you and me).


print(258+(682*(266/3))*-24)

-1451038


Test Yourself

Try some maths problems to test your skills.

  • How many minutes are there in a year?
  • How many seconds in a decade?
  • Roughly how many hours will you spend sleeping during your whole life?

Once you've finished playing with numbers, it's time to move onto words.

Tim Toady

There are other arithmetic operators too. ^ will do exponentiation, so if you wanted to write seven squared you'd write 7^2. % is modulus - this gives the remainder after dividing by a number. So 7%3 is 1 and 6.5%2.5 is 1.5. There are also lots of other maths functions that I'll explain later.

One quick note about numbers, too. Many other programming languages see numbers with decimal points and numbers without as two different things - they call them floats and integers. This is because inside the computer, integer maths is handled very differently to maths involving floats (called floating-point maths). In the olden days, floating-point maths was much slower than integer maths - that's not really the case any more, so Lua does away with integer maths totally and just uses floating-point. Much easier.

I'll come back to this a bit more in Chapter 4's Tim Toady section.
Viewer Comments [0 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net