[COLOR="White"]
--Procedures--
[/B][/COLOR]
Now that you know a little of the layout in Scar scripts let’s learn something new: Procedures
A procedure is just a piece of the script that does something. This is difficult to explain, so I will be using some examples here.
First let’s look at the layout of a procedure.
program NewExample;
procedure Stay;
begin
Wait(500);
end;
begin
Stay;
end.
This is the layout. As you can see the name of the procedure is in the same format as in the program name. One thing you will notice once again, is that all lines, except for begin and the last end, end with ;. Like I said, good habits will eliminate the small errors in your future scripts.
As you can see, a procedure has its own begin and end. It can have more than one begin and end as it becomes more complex.
Now from this example you get your first glimpse of a command! “Wait(500)” is a command that tells Scar to wait 500 milliseconds. (1000 Milliseconds=1 Second) We will go through this more thoroughly later.
I will copy the example here again so you don’t have to scroll too much.
program Procedures;
procedure Stay;
begin
Wait(500);
end;
begin
Stay;
end.
Alright, now as you can see, after the procedure I have a second set of begin end. This is where it actually does things. You see, procedures aren’t used unless you tell the program to use it. It is like a part of the script written so that later you don’t have to write out the whole thing again. As in this example, the procedure “Stay” will now replace the need for you to write out “Wait(500)” every time you need it.
Let’s look at an example with two procedures, now.
This is an example:
program Procedures2;
procedure Write;
begin
Writeln('Hi, you are doing very well with this learning!');
end;
procedure Stay;
begin
Write;
Wait(500);
end;
begin
Stay;
end.
Don’t get scared!! I will explain this step by step.
As you can see, I have just added on to the last example. I have added the procedure “Write”. This is another command. “Writeln('')” is a command that writes what you put between '' in the Debug Box. By the way, the “l” in “Writeln” is “L”. I got this confused with “I” the first time I learned the command.
Once again, I wrote the procedure “Write” so that I would not have to go keep writing out Writeln('Hi, you are doing very well with this learning!') every time.
Now, you might see that I included the procedure “Write” in the procedure “Stay”. You may include procedures in other procedures when you write your own scripts. However, there is a rule to this.
Scar will not allow you to include a procedure if the procedure you want to include is below that location.
This may sound confusing, but think of it like this. In my first example of two procedures, I wrote out procedure “Write” and placed it before procedure “Stay” so that I could use it in there. If I had written the procedure “Stay” with the procedure “Write” included, and then typed the instructions for procedure “Write”, it would show up as an error.
If you are still confused here are examples of a correct and wrong way of including procedures.
Correct way.
program Procedures2;
procedure Write;
begin
Writeln('Hi, you are doing very well with this learning!');
end;
procedure Stay;
begin
Write;
Wait(500);
end;
begin
Stay;
end.
Wrong way.
program ProceduresBad2;
procedure Stay;
begin
Write;
Wait(500);
end;
procedure Write;
begin
Writeln('Hi, you are doing very well with this learning!');
end;
begin
Stay;
end.
Including procedures will be very helpful when your scripts become more and more complex. Just remember the rule.
If you still do not understand it, try to look at the examples and notice the differences.
[COLOR="White"]
--Colors--
[/B][/COLOR]
From the title you may be saying, “Has this guy gone bunkers, what does this have anything to do with colors?” Well, no I’m not crazy, and Scar has everything to do with colors. Scar in itself even has a color picker! (“Ctrl+P”) If you have read my long explanation of coordinates, (Congratulations on finishing it by the way) you will notice that I included the fact that pixels store colors. Now every color has a number. Yup, that’s right. What the Color Picker does is it finds the colors number. Another surprise! Colors have numbers. (When you pick a color with scar the number will appear in the Debug Box) This number can then be used in functions/commands to search for the color and do something with it. (We will explore these functions/commands later)
[COLOR="White"]
--Comments--
[/B][/COLOR]
Sometimes, while you write a script you will want to make a note or two or maybe a few dozen. Comments are used for this very reason. Along with this you can also give users information on the constants that they fill in. (We will explore this in the next chapter)
There are two ways to write a comment.
The first way is by putting // in front of the comment.
Here is an example.
program ExampleComment1; //This is the program name
begin
end.
The second way is by using {}. During this sort of comment you must not forget to set }. If you forget that, your whole script from { will become a comment.
Here is an example.
program ExampleComment2; {This is the program name}
begin
end.
Try copying these two examples to Scar. As you will see, comments turn green.
[COLOR="White"]
[COLOR="White"]+--------------------- [Format] ---------------------+
+--------------------- [Variables and Constants] ---------------------+[/COLOR]
--Variables and Constants--
[/B][/COLOR]
Variables and Constants may be hard to understand until you use them.
First of all, both variables and constants come after the program name and before anything else.
This is an example:
program VarAndConst;
var
const
begin
end.
Let’s look up close at variables first. Just like constants, variables store information. However, variables differ from constants because variables can be changed throughout the whole script. (Hence the name variables [varied]) I will come back to this a little later.
There are three different types of variables: Integer, String, Boolean, Extended.
Integer-Stores a positive or negative whole number
Extended-Stores any numbers (Including decimals)
String-Stores any characters (Mostly used for Writeln or SendKeys)
Boolean-Stores the values “True” and “False”
First, let’s learn how to include them. To include a variable you must first write var. Afterwards you can either skip a line or just start on the line below this. Now first you have to decide which variables you will need. This could be one letter or a word, but try to make it something that makes sense. Don’t put anything random. If this is hard to understand just follow my example.
This is an example:
program VariablesExample1;
var
i: Integer;
Greeting: String;
Yes: Boolean;
//Rest of the script
As you can see, I included an integer, a string, and a boolean.
Let’s look at these one by one.
First there is the integer. Now, if you aren’t too shabby at math, you will remember that integers are whole numbers. They can never have a decimal or fraction. They can also be either positive or negative. In scripting, integers are just this.
The most common integers are x and y. What these two integers most likely store are coordinates. X is for the x coordinates (First part of coordinate) and Y is for the y coordinates. (Second part of coordinate) Of course you can use other letters for this, but x and y is the most common way to go.
To use integers, you must first declare them. What this means is you have to tell the program what the integer is. For example if I had the integer “i”, I would have to tell the program what i stands for.
Here is an example:
program VariablesExample2;
var
i: Integer;
begin
i:=0;
//Rest of the script
end.
In this example, I first inserted the integer in the var section. I then declared what the integer equals in the part I wanted to use it.
Now the great thing about variables, as I have said before, is that they can be changed for different parts of your script. For example, if in one of your procedures, you want the integer “i” to equal 0 and for another procedure you want it to equal 7, you can do that!
Here is an example:
program VariablesExample3;
var
i: Integer;
procedure IntegerIsZero;
begin
i:=0;
//Rest of the procedure
end;
procedure IntegerIsSeven;
begin
i:=7;
//Rest of the procedure
end;
begin
IntegerIsZero;
IntegerIsSeven;
end.
As you can see, you can use a variable in two different places and have it stand for two different things.
Now the variable extended is used the exact same way, except that it stores mixed numbers instead of whole ones. (Numbers with decimals or fractions) As a beginner you will most likely not use until a little while longer, so just don’t worry about it for now.
Alright, now let’s look into strings. Unlike, integers, strings can store any number, letter or symbol. They are mainly used to store characters for functions like Writeln or SendKeys. Once again, since this is a variable we can change it for different parts of the script. Let’s look at an example.
This is an example.
program StringExample1;
var
Greeting: String;
procedure Say1;
begin
Greeting:='Hello';
Writeln(Greeting);
end;
procedure Say2;
begin
Greeting:='Yo!';
Writeln(Greeting);
end;
begin
Say1;
Say2;
end.
In this example, I used the string “Greeting” in two procedures. I declared Greeting as two different things. Go ahead, try this in Scar and change it up a bit.
Now-the boolean. This was the hardest variable for me to understand, and I see now that it is pretty difficult to explain. I will try my best.
As I have defined boolean in the beginning, a boolean just means true or false. Now what confused me when I first started was how to use it. However later I realized that you will almost never use booleans as variables. Once you get more advanced, you will find a use for it, but for now let’s just learn how it works.
Pascal and Scar work in this format: if, then, else.
What this format does is ask tells scar: If this is this, then do this, if it is not then do this. Here is an example to explain this a little better.
This is an example:
program ExampleIfThenElse;
var
i: Integer;
begin
i:=0;
begin
if(i=0) then
begin
Writeln('I is zero');
end else
Writeln('I is not zero');
end;
end.
This is how to use if, then, and else.
I first declared that I is zero. then I began my if, then, else sequence. I told the program: If i is zero, then write i is zero in the debug box. However if i is not zero, then write i is not zero in the debug box.
Study the layout and try to change a few things up to learn it a little better.
Now, by now you should be wondering how this applies to boolean. Well, remember that boolean stands for true or false. As I have said before, boolean in variables is mostly used just by Scar. You cannot see the boolean, but it is there.
Here is how it works. Let’s look at my script again.
This is an example.
program ExampleIfThenElse;
var
i: Integer;
begin
i:=0;
begin
if(i=0) then
begin
Writeln('I is zero');
end else
Writeln('I is not zero');
end;
end.
As you can see, if i is zero then it does something, otherwise it does something else. What boolean does is this. Scar checks if i is zero. “If” i is zero, then Scar returns the value that this is true. “Then” if “If” is true (i really is zero), then it does what you tell it after then. However, if the value of “If” returns as false (I is not zero), you tell it what to do using end else.
Remember this little fact. You do not have to use if, then, else. However, it makes your script much neater and easier to look at. Also, if you use this layout, you don’t necessarily need “end else”. If you don’t tell it what to do if the value is returned false, it just won’t do anything.
This might be hard to understand, but just don’t worry about it yet if you don’t understand it. However, you should still learn the layout of if, then, and else. Once you are more advanced, come back here and see if you can understand it.