The Beginning
page 4
Programming for the Genius-Impaired
A series of lessons for those who want to learn how to create their own Lightwave Plugins with Lscript.
God Rolls Dice

The key, the focus, the very heart of the guess-a -number-game is the picking of a number between one and ten.

And, since the game would get (more) very boring if the number was always the same, we need to pick a number at random, or more to the point, the computer needs to pick a number at random.* 

There is a command in LScript to do just that.  Once again, I urge you to download the documentation for LScript, which can be found on the Newtek Website. Be sure to check out Appendix B for more LScript resources.


The command we're looking for is random(), and it's used like this:

compnumber = random(1,10);

We have some stuff to talk about now. The random(1,10) thing is pretty easy to figure out. It's going to generate a random number, with the lowest possible value being 1, and the highest possible value being 10. Simple. But what is that compnumber thing all about?

The Wonderful World of Variables

Think of a variable as being a bucket with a name attached to it.

Over to the left here, we have a bucket named compnumber. We can put anything we want in that bucket, decimal numbers, whole numbers, negative numbers, you name it. We can also put text in the bucket, but when we do that, it's called a string (thing of a bunch of letters strung together). In the example above, we told the computer to fill our bucket, compnumber, with a number between one and ten.

In this case, it filled it with a 5. That number will stay in the bucket until the program quits, or we decide to change it.  And the great thing is, we can look at our bucket anywhere in the program, at any time and see what's in it.

Your program can have any number of variables, and they can be called anything you want to call them, as long as they aren't any of the words that LScript reserves for it's own use (like random!). These reserved words can be found in the documentation. In general, any command is going to be reserved.

 

Capitalization matters! compnumber is not going to be treated as the same variable as CompNumber, or CoMpNuMbEr. They're all different buckets!

 

So how do we put a number into one of these variable buckets? Quite simply, we put the variable name on the left side of an equal sign, and we put the thing we want it to be equal to on the right. Here are some simple examples:

myVariable = 10;

moreVars = "Hello, World!";

anotherVarThingy = 3.14159;

sumVariable = 15 +12;

compnumber = random(1,10);

So, we've talked a bit about variables, and we've looked our first real command. I think we ought to start making this look like a program!
<<PREV 1 2 3 4 5 6 7 8 9 A B NEXT>>