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:
  1: Numbers and Maths
4: Simple Functions [[cmud_tut_lua_4]] 
We've only really met one function so far, and that's our friend print. Lua has are many more built-in functions - there's a complete list here. There are too many for me to go through here, and certainly too many to remember off the top of your head (why would you need to when there's already a nice reference manual that explains them?). But I'll go through the basics with you.

Asking questions

The first new function I want to introduce you to is called zs.func.prompt. Quite a daunting name, isn't it? Don't be phased - all it does is ask you for a value. Try this:


a=zs.func.prompt()
print(a)

You can also specify the question you want to ask:


name=zs.func.prompt("What's your name?")
print("Ah, so your name is " .. name .. "?")


Strings

Let's start by looking at some simple string functions. You don't need to memorise them - just look them up in the manual if you forget. All string functions start string., like so.


var1 = "straw"
var2 = "reviled"
var3 = "Something a little more complex."

print(string.reverse(var1))
print(string.reverse(var2))
print(string.reverse(var3))

print(var1)
print(var2)
print(var3)

warts
deliver
.xelpmoc erom elttil a gnihtemoS

straw
reviled
Something a little more complex.

The string.reverse function doesn't change the variable's value, it just makes a new, backwards version of the string.

string.len (short for length) is another string function.


name=zs.func.prompt("What is your name?")
print("There are " .. string.len(name) .. " characters in your name, " .. name .. ".")

What is your name? Fang Xianfu
There are 11 characters in your name, Fang Xianfu.

The length is a number, but Lua will convert it into a string for you just like any other number.

This script isn't counting the letters, it's counting the characters. Why don't you see if you can get it to ask for the first and second names separately and then add the lengths together and return the proper length of that person's name?

Did you manage it? Scripting isn't so hard, is it?

A couple of other string functions you might want to try are string.upper and string.lower. They change the case of whatever string they're given.


var="a StRiNg"
print(string.upper(var))
print(string.lower(var))

A STRING
a string

Try writing a script to simulate your boss or your dad when they're angry. Anything you enter in the popup is shouted back at you, like WHAT DO YOU MEAN, "I WANT MY OWN OFFICE."!? YOU'RE FIRED! - perhaps the Dad version could ground you.

Parameters

When you use a function, you say that you're calling that function, and whatever's inside the brackets is its parameters or arguments. So when I do print("Hello!"), you could say that I'm calling the print function with the string "Hello!" as its parameter.

Notice that I said parameters and not parameter back there. Many functions let you give more than one parameter - you do this by separating them with a comma. string.rep is an example of a function that takes more than one parameter.


print(string.rep("Hello! ",5))

Hello! Hello! Hello! Hello! Hello!

Pretty simple - the first parameter is the string to repeat, and the second parameter is the number of times to repeat it.

Returns

If parameters are the things you give to the function, what the function gives to you as a result is that function's return. The string.rep function above returns the string "Hello! Hello! Hello! Hello! Hello! ". The string.lower function returns a lower-case version of its parameter.

Remember how we can assign more than one variable at once?


var1 , var2 = "something" , "something else"

Functions don't have to return just one thing. For example, the string.find function takes two parameters - a string to look in, and a string to look for - and returns two values - the number of characters from the start of the string to the start of the match, and the number to the end of the match.


start,finish = string.find("bread cheese sausage herrings butter bacon","herrings")
print(start)
print(finish)

22
29

This allows you lots of flexibility. If, for whatever reason, you aren't interested in the end value, you can leave it out.


match=string.find("An example","example")
print(match)

4

And if you're not interested in the start value, you can just use a variable you're never actually going to refer to. Most people use a single underscore, _.


_,match=string.find("Another example","ample")
print(match)

15


That's all there really is to functions. I'm going to explain some of the more complex maths functions in the Tim Toady section, so if you're confident in your maths (or want to learn), you might want to read it. The section on random numbers should be especially useful. You shouldn't need any of that stuff though - and if not, then it's on to tables.

Tim Toady

In the same way string functions start with string., maths functions start with math.

Remember what I said about integer and floating point maths back in Chapter 1's Tim section? Sometimes it's useful to simulate integer maths, like making 9/4 equal 2. It might be useful if, say, crabs cost £4 and you want to buy as many as you can with £9. If you want to simulate integer maths, you can use the math.floor function to round down. The math.ceil function will round up, too.


print(9/4)
print(math.floor(9/4))
print(math.ceil(9/4))

2.25
2
3

Another useful maths function is math.abs, which returns the absolute value of the number:


print(math.abs(7-4))
print(math.abs(4-7))

3
3

Feel free to experiment with math.sqrt, math.log, math.tan, math.cos and math.sin. math.pi is also useful. If you don't know what they're for, don't worry - if you do, they work pretty much how you'd expect. Look them up in the Lua manual for more help.

Random Numbers

The function to generate a random number is math.random. If you call it with no parameters, just an empty set of brackets, it'll return a number between or equal to 0 and 1. If you call it with one parameter, it'll return a number between or equal to 1 and that number. If you call it with two numbers, it'll return a number between or equal to those numbers. I won't give output for these examples since they'll be different every time!


print(math.random())
print(math.random(100))
print(math.random(2))
print(math.random(999999999999999))

Extra Credit: Why is math.random(1) nonsense?

Many programming languages give results between 0 and one minus whatever number you give. Lua doesn't - Lua starts counting at 1. Make sure you understand the range of numbers you might get back when you use math.random.

There'll be times when you want math.random to return the same sequence of random numbers more than once. The classic example is a game that defines a random game world each time you play - you might want to be able to save a world you like, or share it with a friend. The way you do this is by setting the seed that's used to work out the random numbers. You do that with math.randomseed, like so.


math.randomseed(2007)
print(math.random(50))
print(math.random(50))
print(math.random(50))

math.randomseed(2007)
print(math.random(50))
print(math.random(50))
print(math.random(50))

11
29
41
11
29
41

To set the seed back to something (very nearly) random, use os.time as the seed - it returns the number of seconds since 1970.


math.randomseed(os.time())

 User comments 
Tech: Thu Aug 21, 2008 9:16 pm    

Updated documentation based on comments. Deleted old comments as a result.
Viewer Comments [1 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net