gerauchert
09-23-2007, 11:55 PM
TUTORIAL FOR NEW SCRIPTERS!
By: Gerauchert
Assumed knowledge:
Can read
Understand scar basics (start button, crosshair, color picker ect...)
MAKING YOUR FIRST SCRIPT
COMMENTS
Anything written with a //"TEXT" or a {"TEXT"} in scar will not be recognized when running your program. This allows you to leave comments next to stuff in your script that you would like to explain. Comments are always green. Heres what it would look like:
//THIS IS A COMMENT
this is not a comment lol
{another form of commentthat allows me
to write onmultiple lines wooooooo
make sure you close the bracket though...}
MAIN LOOPS
First, open up scar. This is what you should see:
program New; // Name of your program
begin // Main loop
end.
The first line of coding is where the name of your script goes. Change 'New' to whatever you like. The name of your program has to be all one word. You will also see a 'begin' and an 'end.' <-- notice the period. The begin and end mark the beggining and ending of your main loop. You might be asking, what the hell is a main loop? The main loop is the part of the script that actually executes the actions that you give it. Anything that you put in the main loop, the script will do when you run your program.
Notice how the 'end' has a period after it. This means that this is the 'end' that is part of the main loop. Your script will have many 'end' identifiers but the only 'end' that has a period after it is the one in the main loop. All the other 'end' identifiers will have a semi colon at the end of it. Like this: 'end;'.
PROCEDURES
Okay so now you know what a main loop is, but you have no clue what "stuff" you put in the main loop to make your script run. You do this by making procedures and functions. To do this you must create a few spaces between your program name and your main loop. Like this:
program FirstScript; // New name :)
begin //Main loop
end.
Now you have some space so in one of those empty lines type: procedure;
Like this:
program FirstScript;
procedure;
begin //Main loop
end.
You need a name for your procedure so you can declare it in your main loop. You also need a begin and an end for your procedure (remember its with no period). So you should have something like this:
program FirstScript;
procedure DoSomething; // Procedure and name
begin
end;
begin // Main loop
end.
Alright now we have this procedure, but like our main loop, it still doesnt do anything. So lets make it do something now. Type this in and ill explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!'); //Types 'I did it!' in the debug box
end;
begin // Main loop
end.
The Writeln procedure will type a message in the debug box (the area below where your script is). This will still do nothing until we declare the procedure in our main loop. So add the procedure DoSomething into your main loop. Your script should look like this:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
DoSomething; // Calls out the Procedure DoSomething
end.
Now press start and check the debug box for your message. There you go! You have made your first script.
IDENTIFIERS, VARIABLES, & CONSTANTS
Now that you understand the concept of making procedures and using your main loop it is time for you to learn a little more. Keep your first script open as we will be using it to learn new concepts.
Now we are going to learn how to repeat something. Type this in and I will explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
DoSomething;
until(False); // It repeats forever with the 'False'
end.
Notice how i have added two things, a 'repeat' and an 'until'. Think of these as like a 'begin' and an 'end'. For every begin there must be an end and for every 'repeat' there must be an 'until'. So we want it to repeat DoSomething until(False). You may ask, what does it mean by 'until(False);'? It simply means that it will keep repeating until you manually stop the script (ctrl + alt + s). Try it out by hitting the start button.
Look at how many times it wrote that in the debug box! But what if you didnt want it to repeat something at that speed? This is how you do it. Type this in and I will explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000); // Waits 1000 ms
DoSomething;
until(False);
end.
Notice how I added Wait(1000); This means that the script will DoSomething every 1 second. The number is always in milliseconds.
Now what if you dont want the script to go forever? Lets say you only wanted it to repeat for a select number of times. To do this you need to declare a variable.
Lets go over the types of variables:
-- Integer -- this variable can have a value of all integers 0,1,2,3...ect.
-- String -- this variable has a value of text like: Hello, goodbye...ect.
-- Boolean -- this variable has a value of either True or False
-- Extended -- this variable can hold all real numbers like: .021,-13,...ect
Now that you know the types of variable I will show you how to declare them. You can either declare them as global or local variables. Global variables are variables that you can use throughout your whole script in any procedure. Local variables are declared only in individual procedures. Lets make a global variable. Type this in and I will explain afterwards:
program FirstScript;
var i: Integer; // Global Variable
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000);
DoSomething;
until(False);
end.
Notice how I added 'var'. That is the first thing that you must do in order to declare a variable. I added 'var' below the program name to make it a global variable. If we wanted to make it a local variable then we would have put the 'var' like this:
program FirstScript;
procedure DoSomething;
var i: Integer; // Local Variable
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000);
DoSomething;
until(False);
end.
See the difference? Good. Moving on...Okay now notice the part next to 'var' where it says 'i: Integer;'. 'i' is the actual variable that we are using. You can rename it to whatever you like as long as it is text with no spaces. And next to the 'i' there is a colon and the word 'Integer'. This means that the variable 'i' has the value of an Integer (one of the types of variables shown above).
Okay now we need to use this variable to make it stop repeating after 'i' amount of times. Type this in and ill explain it afterwards:
program FirstScript;
var i: Integer;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
i:= 0; // Start value of i
repeat
Wait(1000);
DoSomething;
i:= i + 1;
until(i = 4);
end.
I start the main loop out by declaring i as 0. A common error in this would be to forget the colon after the variable, but you MUST have this in order for it to work. So after it does the procedure DoSomething it adds 1 to the variable of i. Then I changed it from until(False) to until(i = 4). No colon is need here. So this will DoSomething 4 times and stop. Try it out and see. Feel free to play around with the numbers until you get the hang of it.
CONSTANTS
Okay its time to learn about constants. Think of constants as variables that cannot change. They can have the same values of variables, but just remember that they dont change in value. They remain constant throughout the script. Type this in and I will explain afterwards:
program FirstScript;
var i: Integer;
const Say= 'I did it!'; // what it will type in the debug box
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
Wait(1000);
DoSomething;
i:= i + 1;
until(i = 4);
end.
The first thing you should notice is the word 'const' this is how you will declare constants. The word 'say' is my actual constant. I have it use the string value by typing apostrophies around the text. You will use the apostrophies around any type of string whether it is a constant or a variable. Notice how the text turns pink when you do that. That is another good indicator of whether it is a string or not. The next thing you should notice is in the DoSomthing procedure. Instead of actually having the string written out in the Writeln procedure I use the constant 'Say'. Do not use the apostrophies when using a variable or a constant in the parenthesis.
This method yields the same result as before, but it will save you time if you have to use the same text or number over and over. Just shorten it up into a constant to make life easier on yourself.
IF THEN, FOR TO DO, & CASE STATEMENTS
Its time to learn some more cooool stufff!!!
IF THEN STATEMENTS
Scripts are built around if then statements. They are extremely useful. Think of if then statements like the identifiers begin & end and repeat & until. Every time you put an 'if' you need a 'then'. Type this in and I will explain it afterwards:
program FirstScript;
var i: Integer;
const Say= 'I did it!';
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
Wait(1000);
DoSomething;
i:= i + 1;
if(i = 2)then // if then statment
begin
Writeln('halfway there!');
end;
until(i = 4);
end.
Ok so I am making the script Writeln('halfway there') when i=2. So it will only type that after it has typed "i did it!" two times. After I have the if then statement i have a 'begin'. This is not needed but it is a good habit to form in order to make your script look neat by having good standards (I will go over standards later). So this is usefull for when you want the script to perform a certain action only under a specific set of conditions (like having i=2).
FOR TO DO STATEMENTS
These statements are usefull for having your script perform an action of the same type multiple times with a variable increasing or decreasing along with it. That probably makes no sense but it will once i type it out for you...ill explain this afterwards:
program FirstScript;
var i, z: Integer; // <--- added z as a variable
const Say= 'I did it!';
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
for z:= 1 to 20 do // for to do statement
begin
Wait(1000 + random(z)); // <--- the variable z
DoSomething;
i:= i + 1;
if(i = 10)then
begin
Writeln('Halfway there!');
end;
end;
until(i = 20);
end.
Alright lets start with the obvious. I added the variable 'z' to start with as an integer. At the begining of my main loop is my for to do statement. So the statement will start with z=1 and keep increasing it until z=20. So this will increase the randomness of my wait time before it preforms DoSomething. This probably wont be noticeable when you run it since there is such a small wait time.
CASE STATEMENTS
Case statements are very useful for cleaning up long code that doesnt need to be there. They are also commonly used in making antiban routines which I will go over later in this tutorial. Type this in and I will explain it afterwards:
program FirstScript;
var i: Integer;
const
Say= 'I did it!';
procedure DoSomething;
begin
for i:=0 to 2 do // for to do statement
begin
case i of // case statement
0: Writeln(Say + 'Oh yeah!');
1: Writeln(Say + 'Look at me!');
2: Writeln(Say + 'I can make case statements!');
end; //case needs to have an 'end' as well
end;
end;
begin
DoSomething;
end.
Ok so I started out with a for to do statement in order for the script to preform each of my Writeln procedures. I then begin the case statement by typing 'case'. Next goes the variable 'i' as is used by the for to do statement. If we wanted only one of the Writelns to preform like for example number 1, we would take out the for to do statement and implace of the variable 'i' we would put a 1. After that we have to put an 'of'. Think of it like every other identifier for every 'begin' there needs to be an 'end' and for every 'case' there needs to be an 'of'. You also have to remember that every 'case' needs an 'end' as well.
There is a reverse way to do this if you wanted. If you wanted to have the numbers in a descending order you would use 'down to'. Like this:
program FirstScript;
var i: Integer;
const
Say= 'I did it!';
procedure DoSomething;
begin
for i:=2 downto 0 do // downto do statement
begin
case i of // case statement
0: Writeln(Say + 'Oh yeah!');
1: Writeln(Say + 'Look at me!');
2: Writeln(Say + 'I can make case statements!');
end; //case needs to have an 'end' as well
end;
end;
begin
DoSomething;
end.
Easy right? Instead of the numbers going from 0 to 2 it goes down from 2 to 0.
WHILE DO LOOPS
While do loops are very good failsafes and they are good for implementing antirandoms into your scripts. I will show you more about that in the SRL scripting section of the tutorial. While do loops basically mean that while a certain action is being preformed (like mining or woodcutting) it will do something during that action (like check for randoms or preform antiban). So i will give you an example:
program FirstScript;
var i: Integer;
const Say= 'w00t for me';
procedure DoSomething;
begin
repeat
Wait(1000);
i:= i + 1;
while (3 < i < 10) do
begin
Writeln(Say);
Wait(500 + random(100));
end;
until(i > 10);
Writeln('i is greater than 10');
end;
begin
DoSomething;
end.
So first we have the 'while' and then comes whatever you want the while to actually mean. In this case the 'while' is when i is between 3 and 10. Then comes the 'do' and the begin afterwards. So we have the script wait and add 1 to i until it is greater than 3 then it will begin sending messages, but after the value of i has reached more than 10 it will end the script.
FUNCTIONS
Functions are vital in creating a script. They are setup the same way as a procedure but the return a value. They can return any type of variable value such as an integer, boolean, string...ect. You can customize functions just for your script to use or you can make it usable to the general public by allowing it to have its own syntax. Ill give you an example of a function:
Function DidSomething: Boolean; // What you want the function to return (a boolean)
begin
repeat
DoSomething;
i:= i + 1;
if(i > 4) then
begin
Result:= True; // Declares under those conditions the function to be true
end else // else refers to any other value i will have thats less than or = to 4
Result:= False; // Anything else will be considered false
until(i > 5);
end;
STANDARDS
I know you are probably thinking, "why do i need standards? who cares what my code looks like as long as it works right?" WRONG! If anyone wants to become an SRL member you better read and apply this.
For those of you who are like...WTF are standards? Dont worry I will tell you exactly what they are. SRL standards are universal spacing techniques that allow you code to look neat, readable, and easy to edit.
SRL STANDARDS RULES:
- The two space rule!
- Two spaces after a begin or a repeat
- Two spaces after a if then (only if it doesnt have a begin after it)
- Two spaces backwards for end and until
- Two spaces after a case statement
- Two spaces after a 'var'
Every begin should have its end directly beneath it (space-wise). Along with every repeat should have it's until directly beneath it. They will always line up in this manner. If you have to ignore the two spaces backwards rule (and make more spaces) to get your ends and untils to line up then do it. Here is an example:
procedure DoSomething;
begin
| repeat
| | Wait(1000);
| | i:= i + 1;
| | while (3 < i < 10) do
| | begin
| | | Writeln(Say);
| | | Wait(500 + random(100));
| | end;
| until(i > 10);
| Writeln('i is greater than 10');
end;
Notice how the lines connect the first letter of begin and the first letter of end. The same thing happens with the repeat and until.
I will teach you some other SRL standards tricks now. Here are some nice ways to make your vars, const, and actual variable declaration look neat.
Program NewScript;
const
SomethingLong = 20; // Notice how i arranged biggest to smallest and lined up the = signs
Somethinglol = 'yes'; // Two space rule :)
Something = True;
Procedure DeclareVars;
var
i,b,c: Integer; //Two space rule applies to var as well :)
t: String;
z: Boolean;
begin
i := 20; //Notice there is one space after the variable and one after the =
b := 10;
c := 5;
t := 'woot';
z := False;
end;
These rules are somewhat more leaniant but make your script look nice none the less.
CONTINUED ON THE NEXT PAGE!
By: Gerauchert
Assumed knowledge:
Can read
Understand scar basics (start button, crosshair, color picker ect...)
MAKING YOUR FIRST SCRIPT
COMMENTS
Anything written with a //"TEXT" or a {"TEXT"} in scar will not be recognized when running your program. This allows you to leave comments next to stuff in your script that you would like to explain. Comments are always green. Heres what it would look like:
//THIS IS A COMMENT
this is not a comment lol
{another form of commentthat allows me
to write onmultiple lines wooooooo
make sure you close the bracket though...}
MAIN LOOPS
First, open up scar. This is what you should see:
program New; // Name of your program
begin // Main loop
end.
The first line of coding is where the name of your script goes. Change 'New' to whatever you like. The name of your program has to be all one word. You will also see a 'begin' and an 'end.' <-- notice the period. The begin and end mark the beggining and ending of your main loop. You might be asking, what the hell is a main loop? The main loop is the part of the script that actually executes the actions that you give it. Anything that you put in the main loop, the script will do when you run your program.
Notice how the 'end' has a period after it. This means that this is the 'end' that is part of the main loop. Your script will have many 'end' identifiers but the only 'end' that has a period after it is the one in the main loop. All the other 'end' identifiers will have a semi colon at the end of it. Like this: 'end;'.
PROCEDURES
Okay so now you know what a main loop is, but you have no clue what "stuff" you put in the main loop to make your script run. You do this by making procedures and functions. To do this you must create a few spaces between your program name and your main loop. Like this:
program FirstScript; // New name :)
begin //Main loop
end.
Now you have some space so in one of those empty lines type: procedure;
Like this:
program FirstScript;
procedure;
begin //Main loop
end.
You need a name for your procedure so you can declare it in your main loop. You also need a begin and an end for your procedure (remember its with no period). So you should have something like this:
program FirstScript;
procedure DoSomething; // Procedure and name
begin
end;
begin // Main loop
end.
Alright now we have this procedure, but like our main loop, it still doesnt do anything. So lets make it do something now. Type this in and ill explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!'); //Types 'I did it!' in the debug box
end;
begin // Main loop
end.
The Writeln procedure will type a message in the debug box (the area below where your script is). This will still do nothing until we declare the procedure in our main loop. So add the procedure DoSomething into your main loop. Your script should look like this:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
DoSomething; // Calls out the Procedure DoSomething
end.
Now press start and check the debug box for your message. There you go! You have made your first script.
IDENTIFIERS, VARIABLES, & CONSTANTS
Now that you understand the concept of making procedures and using your main loop it is time for you to learn a little more. Keep your first script open as we will be using it to learn new concepts.
Now we are going to learn how to repeat something. Type this in and I will explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
DoSomething;
until(False); // It repeats forever with the 'False'
end.
Notice how i have added two things, a 'repeat' and an 'until'. Think of these as like a 'begin' and an 'end'. For every begin there must be an end and for every 'repeat' there must be an 'until'. So we want it to repeat DoSomething until(False). You may ask, what does it mean by 'until(False);'? It simply means that it will keep repeating until you manually stop the script (ctrl + alt + s). Try it out by hitting the start button.
Look at how many times it wrote that in the debug box! But what if you didnt want it to repeat something at that speed? This is how you do it. Type this in and I will explain it afterwards:
program FirstScript;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000); // Waits 1000 ms
DoSomething;
until(False);
end.
Notice how I added Wait(1000); This means that the script will DoSomething every 1 second. The number is always in milliseconds.
Now what if you dont want the script to go forever? Lets say you only wanted it to repeat for a select number of times. To do this you need to declare a variable.
Lets go over the types of variables:
-- Integer -- this variable can have a value of all integers 0,1,2,3...ect.
-- String -- this variable has a value of text like: Hello, goodbye...ect.
-- Boolean -- this variable has a value of either True or False
-- Extended -- this variable can hold all real numbers like: .021,-13,...ect
Now that you know the types of variable I will show you how to declare them. You can either declare them as global or local variables. Global variables are variables that you can use throughout your whole script in any procedure. Local variables are declared only in individual procedures. Lets make a global variable. Type this in and I will explain afterwards:
program FirstScript;
var i: Integer; // Global Variable
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000);
DoSomething;
until(False);
end.
Notice how I added 'var'. That is the first thing that you must do in order to declare a variable. I added 'var' below the program name to make it a global variable. If we wanted to make it a local variable then we would have put the 'var' like this:
program FirstScript;
procedure DoSomething;
var i: Integer; // Local Variable
begin
Writeln('I did it!');
end;
begin
repeat
Wait(1000);
DoSomething;
until(False);
end.
See the difference? Good. Moving on...Okay now notice the part next to 'var' where it says 'i: Integer;'. 'i' is the actual variable that we are using. You can rename it to whatever you like as long as it is text with no spaces. And next to the 'i' there is a colon and the word 'Integer'. This means that the variable 'i' has the value of an Integer (one of the types of variables shown above).
Okay now we need to use this variable to make it stop repeating after 'i' amount of times. Type this in and ill explain it afterwards:
program FirstScript;
var i: Integer;
procedure DoSomething;
begin
Writeln('I did it!');
end;
begin
i:= 0; // Start value of i
repeat
Wait(1000);
DoSomething;
i:= i + 1;
until(i = 4);
end.
I start the main loop out by declaring i as 0. A common error in this would be to forget the colon after the variable, but you MUST have this in order for it to work. So after it does the procedure DoSomething it adds 1 to the variable of i. Then I changed it from until(False) to until(i = 4). No colon is need here. So this will DoSomething 4 times and stop. Try it out and see. Feel free to play around with the numbers until you get the hang of it.
CONSTANTS
Okay its time to learn about constants. Think of constants as variables that cannot change. They can have the same values of variables, but just remember that they dont change in value. They remain constant throughout the script. Type this in and I will explain afterwards:
program FirstScript;
var i: Integer;
const Say= 'I did it!'; // what it will type in the debug box
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
Wait(1000);
DoSomething;
i:= i + 1;
until(i = 4);
end.
The first thing you should notice is the word 'const' this is how you will declare constants. The word 'say' is my actual constant. I have it use the string value by typing apostrophies around the text. You will use the apostrophies around any type of string whether it is a constant or a variable. Notice how the text turns pink when you do that. That is another good indicator of whether it is a string or not. The next thing you should notice is in the DoSomthing procedure. Instead of actually having the string written out in the Writeln procedure I use the constant 'Say'. Do not use the apostrophies when using a variable or a constant in the parenthesis.
This method yields the same result as before, but it will save you time if you have to use the same text or number over and over. Just shorten it up into a constant to make life easier on yourself.
IF THEN, FOR TO DO, & CASE STATEMENTS
Its time to learn some more cooool stufff!!!
IF THEN STATEMENTS
Scripts are built around if then statements. They are extremely useful. Think of if then statements like the identifiers begin & end and repeat & until. Every time you put an 'if' you need a 'then'. Type this in and I will explain it afterwards:
program FirstScript;
var i: Integer;
const Say= 'I did it!';
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
Wait(1000);
DoSomething;
i:= i + 1;
if(i = 2)then // if then statment
begin
Writeln('halfway there!');
end;
until(i = 4);
end.
Ok so I am making the script Writeln('halfway there') when i=2. So it will only type that after it has typed "i did it!" two times. After I have the if then statement i have a 'begin'. This is not needed but it is a good habit to form in order to make your script look neat by having good standards (I will go over standards later). So this is usefull for when you want the script to perform a certain action only under a specific set of conditions (like having i=2).
FOR TO DO STATEMENTS
These statements are usefull for having your script perform an action of the same type multiple times with a variable increasing or decreasing along with it. That probably makes no sense but it will once i type it out for you...ill explain this afterwards:
program FirstScript;
var i, z: Integer; // <--- added z as a variable
const Say= 'I did it!';
procedure DoSomething;
begin
Writeln(Say);
end;
begin
i:= 0;
repeat
for z:= 1 to 20 do // for to do statement
begin
Wait(1000 + random(z)); // <--- the variable z
DoSomething;
i:= i + 1;
if(i = 10)then
begin
Writeln('Halfway there!');
end;
end;
until(i = 20);
end.
Alright lets start with the obvious. I added the variable 'z' to start with as an integer. At the begining of my main loop is my for to do statement. So the statement will start with z=1 and keep increasing it until z=20. So this will increase the randomness of my wait time before it preforms DoSomething. This probably wont be noticeable when you run it since there is such a small wait time.
CASE STATEMENTS
Case statements are very useful for cleaning up long code that doesnt need to be there. They are also commonly used in making antiban routines which I will go over later in this tutorial. Type this in and I will explain it afterwards:
program FirstScript;
var i: Integer;
const
Say= 'I did it!';
procedure DoSomething;
begin
for i:=0 to 2 do // for to do statement
begin
case i of // case statement
0: Writeln(Say + 'Oh yeah!');
1: Writeln(Say + 'Look at me!');
2: Writeln(Say + 'I can make case statements!');
end; //case needs to have an 'end' as well
end;
end;
begin
DoSomething;
end.
Ok so I started out with a for to do statement in order for the script to preform each of my Writeln procedures. I then begin the case statement by typing 'case'. Next goes the variable 'i' as is used by the for to do statement. If we wanted only one of the Writelns to preform like for example number 1, we would take out the for to do statement and implace of the variable 'i' we would put a 1. After that we have to put an 'of'. Think of it like every other identifier for every 'begin' there needs to be an 'end' and for every 'case' there needs to be an 'of'. You also have to remember that every 'case' needs an 'end' as well.
There is a reverse way to do this if you wanted. If you wanted to have the numbers in a descending order you would use 'down to'. Like this:
program FirstScript;
var i: Integer;
const
Say= 'I did it!';
procedure DoSomething;
begin
for i:=2 downto 0 do // downto do statement
begin
case i of // case statement
0: Writeln(Say + 'Oh yeah!');
1: Writeln(Say + 'Look at me!');
2: Writeln(Say + 'I can make case statements!');
end; //case needs to have an 'end' as well
end;
end;
begin
DoSomething;
end.
Easy right? Instead of the numbers going from 0 to 2 it goes down from 2 to 0.
WHILE DO LOOPS
While do loops are very good failsafes and they are good for implementing antirandoms into your scripts. I will show you more about that in the SRL scripting section of the tutorial. While do loops basically mean that while a certain action is being preformed (like mining or woodcutting) it will do something during that action (like check for randoms or preform antiban). So i will give you an example:
program FirstScript;
var i: Integer;
const Say= 'w00t for me';
procedure DoSomething;
begin
repeat
Wait(1000);
i:= i + 1;
while (3 < i < 10) do
begin
Writeln(Say);
Wait(500 + random(100));
end;
until(i > 10);
Writeln('i is greater than 10');
end;
begin
DoSomething;
end.
So first we have the 'while' and then comes whatever you want the while to actually mean. In this case the 'while' is when i is between 3 and 10. Then comes the 'do' and the begin afterwards. So we have the script wait and add 1 to i until it is greater than 3 then it will begin sending messages, but after the value of i has reached more than 10 it will end the script.
FUNCTIONS
Functions are vital in creating a script. They are setup the same way as a procedure but the return a value. They can return any type of variable value such as an integer, boolean, string...ect. You can customize functions just for your script to use or you can make it usable to the general public by allowing it to have its own syntax. Ill give you an example of a function:
Function DidSomething: Boolean; // What you want the function to return (a boolean)
begin
repeat
DoSomething;
i:= i + 1;
if(i > 4) then
begin
Result:= True; // Declares under those conditions the function to be true
end else // else refers to any other value i will have thats less than or = to 4
Result:= False; // Anything else will be considered false
until(i > 5);
end;
STANDARDS
I know you are probably thinking, "why do i need standards? who cares what my code looks like as long as it works right?" WRONG! If anyone wants to become an SRL member you better read and apply this.
For those of you who are like...WTF are standards? Dont worry I will tell you exactly what they are. SRL standards are universal spacing techniques that allow you code to look neat, readable, and easy to edit.
SRL STANDARDS RULES:
- The two space rule!
- Two spaces after a begin or a repeat
- Two spaces after a if then (only if it doesnt have a begin after it)
- Two spaces backwards for end and until
- Two spaces after a case statement
- Two spaces after a 'var'
Every begin should have its end directly beneath it (space-wise). Along with every repeat should have it's until directly beneath it. They will always line up in this manner. If you have to ignore the two spaces backwards rule (and make more spaces) to get your ends and untils to line up then do it. Here is an example:
procedure DoSomething;
begin
| repeat
| | Wait(1000);
| | i:= i + 1;
| | while (3 < i < 10) do
| | begin
| | | Writeln(Say);
| | | Wait(500 + random(100));
| | end;
| until(i > 10);
| Writeln('i is greater than 10');
end;
Notice how the lines connect the first letter of begin and the first letter of end. The same thing happens with the repeat and until.
I will teach you some other SRL standards tricks now. Here are some nice ways to make your vars, const, and actual variable declaration look neat.
Program NewScript;
const
SomethingLong = 20; // Notice how i arranged biggest to smallest and lined up the = signs
Somethinglol = 'yes'; // Two space rule :)
Something = True;
Procedure DeclareVars;
var
i,b,c: Integer; //Two space rule applies to var as well :)
t: String;
z: Boolean;
begin
i := 20; //Notice there is one space after the variable and one after the =
b := 10;
c := 5;
t := 'woot';
z := False;
end;
These rules are somewhat more leaniant but make your script look nice none the less.
CONTINUED ON THE NEXT PAGE!