We've been looking a lot at verbs. But what about these property things?
As I said earlier, properties are a way to store information on an object. They're like variables in code, except that they don't disappear as soon as the code finishes running. Let's go back to our little cat thing and put a property on it.
Instead of storing the text we want the cat to do when someone strokes it in the verb, and having to edit the verb to change what it does, we could store it in a property on the cat. The happy noise that the cat makes was:
Lets then store this in the property happy_msg (msg being short for message). To add it we do:
@prop cat.happy_msg "purrs happily."
=> Property added with value "purrs happily.".
Now, your code should currently look like:
player:tell("You stroke ", this:title(), ".");
player.location:announce(player:titlec(), " strokes ", this:title(), ".");
rand = random(5);
if (rand == 1)
player.location:announce_all(this:titlec(), " hisses and scratches at ", player:title(), ".");
player.location:announce_all(this:titlec(), " purrs happily.");
We want to change the second to last line to use the value of the happy_msg property, so it would now look like:
player.location:announce_all(this:titlec(), " ", this.happy_msg);
Why the " "? Well, as we didn't put a space at the beginning of the message in the property we need to add it in the code.
Try doing the same thing, but for the times when the cat is angry; I'll leave that up to you.
(Try this code if you get stuck)
A more interesting example
We could also store some sort of happiness level of the cat, so that it wouldn't be just random as to when the cat is happy and when it's unhappy. This is a more interesting, in my opinion at least, use of properties than just as something to store a message that is never changed.
Let's say that if the cat's happiness level is over 0 then it's happy. If it's 0 or less, then it's unhappy.
We'll start the cat off as happy, so lets add a property called
happiness with the value
@prop cat.happiness 1
=> Property added with value 1.
We'll keep the random number from 1 to 5, but lets get another random number - either 1 or 2. If we get a 1, we'll make the cat less happy. If it's a 2, the cat likes the stroking. So, we're going to be using
random() again, as well as setting the property based on what the results are. Ready? Here's what we need to do:
Before line 4 (the if line) we have to add:
happy = random(2);
if (happy == 1)
this.happiness = this.happiness - rand;
this.happiness = this.happiness + rand;
Whut whut whut? What's all that? It's just what we said we'd do. Line 1 - we get a random number. Then in line 2, if the random number is a 1, in the next line we subtract the random value in rand from the cat's current happiness. Otherwise (line 4), we add it to the happiness level (line 5).
We also need to change the test to see which message we should use. The cat is now happy if it's happiness level is above 0, so we need to check:
if (this.happiness < 0 )
All done. Give it a try! :)
Here's what your code should look like at this stage.
For the fast learners, (or to find the people who paid particularly good attention in the 'if' section), here's a quicker way to do it using the
test ? true-code | false-code construction. See if you can figure it out.
The first two lines, and the message announcing lines remaing the same. The middle (lines 4 through 9) can be compacted to:
rand = random(5);
this.happiness = this.happiness + ((random(2) == 1) ? (0 - rand) | rand);
I've coloured the matching brackets so you see how the code is executed a bit more easily hopefully. If you don't follow this, don't worry about it, it doesn't do anything different to the if version of the code, except save a little typing. (Note that adding a negative number is the same as subtracting a positive number)