The Beginning
page 5
Programming for the Genius-Impaired
A series of lessons for those who want to learn how to create their own Lightwave Plugings with Lscript.
Deep Magic from the Dawn of Time

Ok. This is probably the toughest page in the whole tutorial. You can can take it. Once you get past this one, the rest is a cakewalk.

main
{    

compnumber = random(1,10);

}

Not much of a program so far. We've just covered step one on our list (remember back on page two?).  To add the next two steps, where the computer says "I'm thinking of a number..." and ask for the user's guess, we need to add something to our program called a requester.

A requester is a window that pops up and asks for user input. It can ask for numbers or text, or special things like colors or distance. We'll make it one step at a time and see what it does.
 

main
{

    compnumber = random(1,10);

    reqbegin("Pick a Number");

        ctltext("","Pick a number between 1 and 10");

    if(reqpost())
    {
        //  We'll be putting stuff here later
    }
    else
        return;
    reqend();
}

You can save this as a script and test it.

It will produce a requester like the one to the right. It doesn't ask the user for anything yet, we'll do that next. For now, lets just look at the commands that make the requester.

 

We start with reqbegin("Your title goes here");. This tells the program to open up a requester, and whatever you put in quotes becomes the title bar for the requester.

Next is the ctltext("A Title goes here","Your Text goes here");.
This tells the program to add a line of text to your requester. You can add a title off to the side by putting words in the first set of quotes, but we don't want that right now, so we leave those quotes empty, like this "". 

The next seven lines control the behavior of the "OK" and "Cancel" buttons. But to figure these out, we need to take a moment to talk about control structures.

So far, our program has been like a short story. We start reading at the top, read down the page, and before you know it, we're done..

That works fine for a story, but it doesn't work that well for a program. In a program, we have to be able to make decisions, and sometimes skip stuff, and other times repeat stuff over and over again. Programs are stories that do stuff. When we need to make decisions, we use Conditional Statements.

Conditional Statements

The conditional statement we need to talk about first is if/else. You already know this one. Let me remind you:
 

Parents:  If (baby cries) feed baby else sleep some more

Kids: If (eyes open) play StarCraft else sleep some more

Fellow Drones: If (Boss nearby)  work else play StarCraft

Freelancers: If (Bank account is greater than or equal to Rent)  pay rent else don't answer phone


Let's put the last one in true LScript form:
 

if(bankacc >= rent)
{
    rent = "paid";
}

else

{
    rent = "unpaid";

    answerPhone = "FALSE";
    doodoo = "deep";
}

So this is called a conditional statement, because what happens is based on some condition being met (or not met). There are a number of conditions we can test for. The different symbols we use to make these comparisons are called operators:

Operators
= Is Made To Equal var = 7
== Is Equal To someVariable == 0
!= Is Not Equal To someVariable != 1
! Is Not !pants
> Is Greater Than var > 6
>= Is Greater Than or Equal To 12 >= var
< Is Less Than var < pants
<= Is Less Than or Equal to pants <= 500

Wait a minute!  If two equals signs means "Is Equal To," what do all those single equal signs in the program mean? What's the difference?

Well, Johnny, I'm glad you asked. The single equal sign means we are telling a variable that it needs to be equal to something. When we say somevariable = 0; we are telling somevariable to equal zero.  In other words, somevariable Is Made To Equal 0.

If we use two equal signs; somevariable == 0, we are asking a question, does somevariable equal zero? We almost always find this question embedded in a control structure, like if(somevariable == 0).

There's another condition we can test for. Some things have a state of trueness or falseness associated with them. These special states, TRUE and FALSE, are called Boolean values. They can be tested for just by putting a variable or some statement in the if() statement. If it's TRUE, it will do the first set of commands. If it's FALSE, it'll do the second (else) set of commands.

So let's look at those last seven lines again:

if(reqpost())
{
    //  We'll be putting stuff here later
}

else

    return;

reqend();

The command reqpost()is one of those TRUE/FALSE statements. It returns one of two values. If the user clicks "OK," it shows up as "TRUE," and the program does whatever is between the brackets directly under the if.

Then it skips the command directly after the else and goes right to reqend(); which officially closes the requester

If the user clicks "Cancel," it comes back as "FALSE," and the program executes the command directly after "else," the return; command. In this case, return bounces us out of "main" and ends the program.

Believe it or not, that probably the hardest part of the program to understand. It's all downhill from here!

 
<<PREV 1 2 3 4 5 6 7 8 9 A B NEXT>>