The Beginning | page 9 |
Programming for the Genius-Impaired
A series of lessons for those who want to learn how to create their own Lightwave Plugins with Lscript. Someone to Watch Over Me |
One thing the computer is
really good at is keeping track of things. So for our next amazing feat
of programming prowess, we're going to start keeping track of how many
times the user has to guess to get the right answer. What's a game if
you don't keep score?
Near the top we added the line guessnumber = 0;. We are going to keep track of the number of times the user guesses with the variable guessnumber, so this just sets the number of guesses equal to zero at the start of the game. After the users enter a guess in a1 = ctlinteger("Your Guess" ,yourguess); we need to add one to the total number of guesses they have made. There a couple of ways to do this: guessnumber = guessnumber + 1; and ++guessnumber; In the first version, we're telling the program that the variable called guessnumber needs to be set equal to the current value of guessnumber, plus one. In the second version, we use a shorthand version that just says, add one to whatever is stored here. Next in the game, we want to tell the users what guess they are currently on. This gives us a new challenge: putting numbers and letters together. Now, that sounds pretty simple, but you have to understand that programming languages treat text and numbers quite differently. In this world, numbers are things that can be added, subtracted, multiplied and divided, etc. Text, on the other hand, can't very well be multiplied! What's Apple divided by Footstool, what's the square root of pie? There is even a special word for text in the context of computer languages: strings. A string is just a bunch of text. Think of it as stringing together a bunch of characters. We've already used variables that store numbers:
Wait a second, that last one is a number! Bzzzt! Wrong answer. Once we put a number in quotes, it's just another string. In fact anything we put in quotes is a string. Now, in our requester, we want to add a line that says, for instance, "This is guess number 4," with the number coming from the variable guessnumber. Usually LScript is pretty flexible about displaying this kind of information. Take this example:
This is legal in lscript. It will display everything in quotes exactly as it appears, followed by the number stored in guessnumber. But the ctltext() command inside our requester is pickier. It would see the comma in the example above as a cue for a line break, and our attempt would look something like this:
This is ugly. Can't go around writing ugly programs. But there is a fix:
The string() command takes everything in parenthesis and makes it into a text string. The "+" is taking the place of the comma in the earlier examples. Once all the information is stored in string format in the variable guessmessage, we can display it in the requester using ctltext. INSERT PICTURES HERE You made it! It's over! You now officially know enough about LScript to cause real trouble. I hope this tutorial gives you the confidence to start fooling around on your own. Remember, keep a copy of the LScript documentation handy. Programming is kind of like a salad bar, you just choose all the bits and pieces you need, toss 'em in a bowl and cover them with blue cheese dressing. Or something like that. A good way to get a feel for programming is to take an existing program and add things to it. For starters, try adding to this program*. Can you change the winning message section so it makes fun of you if you need more than 4 guesses? And praises you if you guess it in one guess? What happens if the user enters a guess of 3000? -47? Susan? Programs usually check for these errors, and ask the user to re-enter information if it is wrong. Can you add that feature? If you're feeling really cocky, modify it so the requester asks for the user's name on the first pass, then calls the user by name on all the subsequent tries. Start small, fiddle around a bit. Have fun! And welcome to the world
of programming! Sean Moyer * When we add something to a program that enables the program to do something it couldn't do before, this is called a feature. Adding a few well chosen features to a program can make the program a better, more useful tool. Adding lots and lots of features just because you can is called "feature creep," and can lead to "bloatware". If you take bloatware, and continue adding features so you can keep selling the same program all over again, year after year, you may be in violation of a Microsoft business patent...};^) |