PDA

View Full Version : Learn to program with SCAR



addy771
05-13-2007, 07:47 PM
Learn how to program with SCAR

Syntax
If you don't know/understand what a syntax is, it is the format that the code is in.
Still lost? Think of it like one of those big wedding cakes. The big piece goes on the bottom, and the smaller pieces are stacked in order on top of each other. You wont see a cake with a small layer at the bottom, and a big layer at the top. They always have to be the same. You can have different icings and decorations on the cake, but the layers will still be the same. So, now that you know what a syntax is, lets show you what the SCAR syntax is.


Program New;
begin
end.


Simple enough, just like the cake. You can't put the 'layers' in different orders. It will always have to be the same.

Now if you haven't guessed already, the SCAR syntax is the same syntax as you would see in Pascal. This is because SCAR uses a custom version of Pascal. If you already know Pascal, SCAR will come easy to you.


Variables
What are variables? Variables are slots in memory, which you can place a number, string or a boolean value in. I will get into variable types later. Think of a variable as a cookie tin. You can place cookies in it, and the amount of cookies can change at any time, when you eat some of them. Now, with SCAR, you wont be able to store cookies on your computer (too bad eh? :P), but you will be able to find many uses for them.

Variable Types
This is where some people get confused, where they have to choose which type of variable to use for a job. Lets go back to the cookies. Lets say you also have a piggy bank for storing your coins. You don't use your piggy bank to hold cookie, and nor do you use your cookie tin to hold coins. That's why you have to have variable types, or in other words, "Different containers". There are three main types of variables in SCAR, Integers,Strings and Booleans. Integers hold numbers, which can be negative or positive. Integers are supposed to be whole numbers, without decimals, however if a variable that was declared as an integer is forced to get a decimal value, the variable is then converted to what is know as an extended integer (also known as a 'real' integer). Don't worry about this, if you don't understand that then it's ok, it isn't important. The string type of variable holds letters and numbers, like a sentence. An example of a string would be 'Jimmy is 14 years of age'. Strings can hold up to 255 characters. The final variable, the boolean, only has two values. A boolean can either be 'True' or it can be 'False'.


Declaring Variables
Now that you've learned what variables are and what kinds there are, you can learn how to declare them. This is also part of the syntax. The way you declare a variable must always be the same.
To declare an Integer variable, we use
Name:=Value;
The 'Name' part can be any word of your choice, such as 'var1'.
The ':=' must be there, this tells SCAR that you are declaring a variable.
The 'Value' can be any number, see the Variable Types section above to see the range for integers.
The ';' must be there, this tells SCAR that we are finished declaring the variable.

To declare a string variable, we use
Name:='Value';
Like above, the 'Name', ':=', 'Value' and ';' are all the same, except that the value will be a string instead of an integer. The difference with a string variable is that we must add the "'" before and after the string. This simply tells SCAR where the string begins and ends.

To declare a Boolean variable, we use
Name:=Value;
This is the same as an integer, except the value can only be 'True' or 'False'.

Now, you know how to declare a variable, but where do you put it? Here is an example:


program Variables;

var
Integer1: Integer;
String1: string;
Boolean1: Boolean;

begin
Integer1:= 44;
String1:= 'I can see my house!';
Boolean1:= False;
end.

Notice in the example that I also had to tell SCAR what types of variables I am using, above the 'begin', and below the 'Program Variables;'.

Then, after the begin I declared the variables correctly. The above script will do nothing but assign a few slots in memory and then exit.

First Script, using variables
Now that you have all of that down, let's begin by making a very basic script.
start off by naming your program anything you want, and creating a variable called 'strx',as a string.
Now, under begin, declare the value of your string integer as 'Hello, World!'.
So far you should have something like this:


program First;

var
strX: string;

begin
strX:= 'Hello World!';
end.

Press the green play button to see if you have done it correctly,if you have, no error message will pop up.

Now let's use that same program with the writeln procedure, to output that message.
add " writeln(strx);" below where you defined 'strx'.
Press play and you will see "Hello, World!" in the debug box below. If not, you have done something wrong.

Looping Code
If you're ever going to make a useful script, you're going to have to learn how to master the loop. There are two main ways to loop a portion of code, and each can be configured to run forever, to a certain amount or until a certain condition is met.
First, lets start off with the most common loop, "Repeat...Until". This is used most often because it can loop any amount of coding, and that amount can be defined very easily. here is a demo of the repeat until loop:


program Loop;

var
x: Integer;

begin
repeat
x:= x + 1;
until(x > 100)
end.

As you can see, you define the start of the loop with the 'repeat'. Next, we make the integer 'x' add one to itself, each time the script loops. Then we define the end of the loop with the 'until'. in brackets we have put 'x>100', which instructs SCAR to repeat the code until the variable 'x' is greater than 100. If you wish to make the loop run forever, you would simple put in 'until(false)'.

Now, for the next kind of loop. This loop is called the 'while...do' loop. this, unlike the 'repeat...until' loop, is harder to specify more then one line of code to loop, and is harder to make it loop forever. Here is an example of this type of loop:


program New;

var
x: Integer;

begin
while x < 100 do
begin
x:= x + 1;
end;
end.

This is just like the 'repeat...until' loop, except that this has the extra 'begin' and 'end;'. Take note that this extra end does not have a period at the end, but a semi-colon. Keep in mind that for every begin you have, you must have an end with it. Now, we use this begin and end here to signal SCAR that we want to loop a sequence of code, rather then just one line. This will loop everything within the 'begin' and the 'end'. You will find uses for both of these types of loops when you start to build more advanced programs with SCAR. There is also the 'for...to...do' loop, but you will only need that for more advanced uses.


If...Then
Now for the next part. Lets say you wanted to build a script to click on a monster or something, but only if the monster was found? Then you would use the 'If...then'. What I'm referring to as 'If...Then' is a technique that checks if a condition is met before starting something. Here is an example of how you would use the 'If...Then' together with constants to create a configuration section for your program:


program Setup;

const
Autokill = True;

begin
if(Autokill = True)then
begin
//Autokilling code in here
WriteLn('Done!');
end;
end.

As you can see, I set up a constant called 'Autokill' which is a boolean. This constant can be defined as either true or false by the user, allowing them to choose whether to autokill or not, in this example. You can see the 'If...then' has the autokill constant in brackets as true. What this does is ask, "If autokill is set to true, then..." and then if the condition is met, the next line in the script will be run. If not, it will be ignored. This is similar to the 'While...Do' loop, because if you want it to work on more then one line you must use the 'begin' and 'end;' to define the area to be used. There are also other things you can add to the 'If...Then' to make it more convenient. Take a look at this example:

if(Autokill = True)and(not(Autokill = False))then

This makes it so that there are two conditions that must be checked before running the next line of code. In this case, I have also used a 'not' with extra brackets. A translation to English for that would be "Is autokill is true and not false, then". There is also 'or' which can be used like so:

if(Autokill = True)or(Autokill = False)then

This makes it so that if either one of the two conditions stated is true, the next line will be run. In this example, since a boolean variable/constant can only have one of two values (true/false), this code would run the next line all the time.

That's it! You've learned the basics of SCAR. Using these techniques you can do many things! For now, take a look at all the procedures/functions of SCAR (accessed by pressing F1 while SCAR is open) so you can start building your own scripts.


This tutorial was written by Addy771(Addyrulez)

Copyright 2007 Addy Studios (http://addy.awardspace.com)

3Garrett3
05-13-2007, 08:24 PM
so, why is this tut in the general board? was it supposed to be tut island? if so, get an admin/mod to move it.

addy771
05-13-2007, 08:30 PM
Sorry, I didn't know there was a tut island XD

Boreas
05-14-2007, 02:35 AM
moved

danrox2004
05-14-2007, 01:23 PM
Looks like a good tutorial!

Freddy1990
05-14-2007, 02:02 PM
Tutorial looks good, script examples look horrible ;)
Work a bit on ur scripting standards :)

codx1
05-14-2007, 02:10 PM
Nice tutorial, thx

addy771
05-14-2007, 10:22 PM
Tutorial looks good, script examples look horrible ;)
Work a bit on ur scripting standards :)
XD what's wrong with them?

Janilabo
05-14-2007, 10:43 PM
Very nice tutorial Addy! You explained things VERY well, so, the tutorial seems really "noob-friendly" to me. :) ..but I agree with Freddy about the script examples, they DO look horrible... :D

Here is all of your script examples with standards:

1.

program New;
begin
end.

2.

program Variables;

var
Integer1: Integer;
String1: string;
Boolean1: Boolean;

begin
Integer1:= 44;
String1:= 'I can see my house!';
Boolean1:= False;
end.

3.

program First;

var
strX: string;

begin
strX:= 'Hello World!';
end.

4.

program Loop;

var
x: Integer;

begin
repeat
x:= x + 1;
until(x > 100)
end.

5.

program New;

var
x: Integer;

begin
while x < 100 do
begin
x:= x + 1;
end;
end.

6.

program Setup;

const
Autokill = True;

begin
if(Autokill = True)then
begin
//Autokilling code in here
WriteLn('Done!');
end;
end.

7.

if(Autokill = True)and(not(Autokill = False))then

8.

if(Autokill = True)or(Autokill = False)then

See how much easier it makes to read the script examples, when you use scripting standards? It also makes scripting alot easier, if you DO use em. In my opinion scripts, that have been scripted using (correct) standards, look ALOT better than the ones that haven't been scripted using standards or those scripts that only have some parts with standards.

Check out Official SCAR Scripting standards (http://kaitnieks.com/scar/scriptingsta/).

addy771
05-14-2007, 10:48 PM
Oh, that's what you meant.

I've never spent much time making my scripts readable, and when it came to the examples I didn't consider that some people may find it hard to read. I'll fix that right now.

Freddy1990
05-15-2007, 02:13 PM
Wait a tick, ur addy! ^^ Lol haden't noticed :) Heya just cuz I came across the tut on ur boards I remembered it from ehre etc... heh
The tutorial itself is very good, but for tutorials you should try to use good scripting standards so that it's easier for people to understand and so that they will start using them as wlel if they don't already do do :)

addy771
05-15-2007, 02:33 PM
Wait a tick, ur addy! ^^ Lol haden't noticed :) Heya just cuz I came across the tut on ur boards I remembered it from ehre etc... heh
The tutorial itself is very good, but for tutorials you should try to use good scripting standards so that it's easier for people to understand and so that they will start using them as wlel if they don't already do do :)

Rofl yea, I'm Addy the admin from Eohax.

Hugolord
05-15-2007, 02:55 PM
addy i must say very nice Tut altho i already know all of the thins covered in it this is a very good Tut for beginners!