Palace Client manuals Palace Server ManualsWizards and Owners  ManualPalace Scripting manualHow to make Avatars ManualsPalace BuildersChat Guides

Iptscrae Language Guide



Adding Machine Exercise

The following exercise provides a step-by-step look at the creation of a script called "Adding Machine," which performs addition problems. With this script in place, whenever you type in something like "673 plus 897" your avatar will say "673 plus 897 equals 1570", and at last your parents will see the point behind all that tuition

The Adding Machine script will sit in the OUTCHAT handler of some spot or cyborg, waiting to hear the magic word "plus" in a CHATSTR. Since most of our utterances won't include this word, we can tell right away that the active part of our script will have to be placed within an IF command:

ON OUTCHAT {
<the active part of the script>
} CHATSTR "^(.*) plus (.*)$" GREPSTR IF

The two wildcards in the above sentence - these things: (.*) - tell GREPSTR to grab everything spoken in their places (i.e., outside of the blank spaces surrounding the word "plus"). GREPSTR then saves them. Thanks to this function, we can now use a GREPSUB command to get them back, and assign them to the special symbols $1 and $2 as you will see in the active part of our script, which we can now turn to.

Since $1 and $2 are strings, we need to convert them to integers before we can do math with them. But because we're going to want their string values later (when we speak the answer), we'll create a couple new symbols, and convert those into numbers using the ATOI command:

"$1" GREPSUB FIRSTNUMBER =
"$2" GREPSUB SECONDNUMBER =
FIRSTNUMBER ATOI FIRSTNUMBER =
SECONDNUMBER ATOI SECONDNUMBER =

So far so good. FIRSTNUMBER and SECONDNUMBER are now symbols corresponding to the integer values of these numbers, while $1 and $2 still store the string versions (note that we had to use GREPSUB to get at these values). Now that we have our integers, we can do the addition operation itself, sending our output to a third symbol we'll call "TOTAL":

FIRSTNUMBER SECONDNUMBER + TOTAL =

As you'll see in the following paragraphs, the first part of this line ("FIRSTNUMBER SECONDNUMBER +") uses the + operator to put the sum we need onto the stack. The remaining part ("TOTAL =") grabs this number and assigns it to a symbol. Now we have TOTAL, but it's an integer! Before we can speak it out loud, we have to change it into a character string with the ITOA command:

TOTAL ITOA TOTAL =

At last we're ready to speak. We'll need to use another GREPSUB command to replace $1 and $2 just as before, then we'll slap a simple sentence together with a couple & operators (see below) and stick our TOTAL onto the end of that sentence. Finally we'll replace the original CHATSTR with this whole contraption, so that no one will ever see the original text that triggered our script. Here's the magical line that performs all that trickery:

"$1 plus $2" GREPSUB " equals " & TOTAL & CHATSTR =

That's it! When you put it all together, you get a nifty little script that can easily be modified to perform any mathematical operation.

; "ADDING MACHINE" script
; Put this in your OUTCHAT handler
{
"$1" GREPSUB FIRSTNUMBER =
"$2" GREPSUB SECONDNUMBER =
FIRSTNUMBER ATOI FIRSTNUMBER =
SECONDNUMBER ATOI SECONDNUMBER =
FIRSTNUMBER SECONDNUMBER + TOTAL =
TOTAL ITOA TOTAL =
"$1 plus $2" GREPSUB " equals " & TOTAL & CHATSTR =
} CHATSTR "^(.*) plus (.*)$" GREPSTR IF

The Adding Machine Script
Congratulations! If you've performed this exercise carefully and have made liberal use of the Language Reference section, you're probably beginning to get a feel for the language and its unusual ways. You might even be getting very specific ideas about what you'd like to do with it, and how it might work. Are the words "handler" and "atomlist" actually making sense to you? Are you feeling at ease with such backward-looking formulae as "2 3 + total ="?

If you've reached that point, you just might want to set aside some time and brainstorm out a full design; you're ready to become one with the Iptscrae, and enter the weird world of the Palace owners. That's right -- you can now say that you're about as advanced an Iptscrae programmer as anyone -- so get out there and do it!

And don't forget to invite us to your grand opening!