PDA

View Full Version : Create Your Own Commands



WhoCares357
02-28-2007, 04:17 AM
How to Create Your Own Commands



by WhoCares357

If this belongs in the Intermediate Tutorials, please move it there.

If you’re reading this tutorial, I’m expecting you to know the basic structure of Scar: syntax, variables, constants, and some simple commands like MoveMouse. You should also know what includes are (Like SRL). It is also a requirement that you know how to use simple procedures. If you don’t know these things, dig around at http://www.srl-forums.com/forum/tutorial-beginners-f176.html a bit.

You have probably always wondered how SRL and other includes make all those new commands like MMouse, Mouse, etc. I’m going to teach you how to create your own commands. If you get confused, look back at the examples.

A normal script with a procedure would look like this:


program New;

procedure Name;
begin
WriteLn('My name is WhoCares357');
end;

begin
Name;
end.


All you basically do is tell Scar what the procedure does and then tell it to use the procedure in the main loop. This eliminates the discomfort of constantly having to write out WriteLn('My name is WhoCares357');.

A command is basically the same thing, except it has perimeters. These perimeters are actually just options the user sets for the command.

There are two types of commands. One is called a procedure. The other is called a function. The difference between the two is that a function always returns a value at the end (true or false).

Let’s modify our earlier script to include a procedure with perimeters.


program New;

procedure Name(YourName: string);
begin
WriteLn(YourName);
end;

begin
Name('WhoCares357');
end.

Study this a little. Remember how we declared variables for the script. We’re doing the same thing for the procedure. For procedures and functions, however, we declare the variables inside the parentheses to indicate that the variable type is the option for the user.

Don’t worry about that. Just know that you have to set options inside the parentheses and declare what the option is. In this case the option is a string and we called it YourName.

Remember how we used to use variables and constants to replace the x,y coordinates in MoveMouse or the color numbers in FindColor? We do the same thing with options. (Stuff inside parentheses)

Let’s review. We called the procedure Name and declared YourName as a string. Then we replaced the text for WriteLn with YourName.

Now look in the main loop. To use the procedure Name we must now enter the option data. In this case, it is a string. So I entered the string 'WhoCares357'. Try this out. You will see that whatever you put between '' will be written in the Debug Box.


Let’s add a little to our last script.


program New;

procedure Name(YourName: string; WaitTime: Integer);
begin
Wait(WaitTime);
WriteLn(YourName);
end;

begin
Name('WhoCares357', 100);
end.


Examine this and study it a little. You will see that I added a new option to the procedure Name. I added a wait time. To declare more than one option, you have to separate them with ;.

You will see that I added a new command to my procedure. I added Wait. I used the option WaitTime to fill the place of how long to wait. In the main loop I used the procedure and filled in the string option 'WhoCares357', separated the options with a comma, and then filled in the Integer option with 100. Now, the procedure Name will Wait the amount of time I set with the WaitTime option and will write whatever I put as the YourName option.

Let’s look at how we use booleans with procedures.


program New;

procedure Name(YourName: string; WaitTime: Integer; Clear: boolean);
begin
if Clear = true then
begin
ClearDebug;
end
Wait(WaitTime);
WriteLn(YourName);
end;

begin
Name('WhoCares357', 100, false);
end.

I have added an option of a boolean here. The boolean name is Clear. Remember that booleans can either be true or false.

I have put the option to use by using an if then statement. If the user says true for Clear, the debug box will clear before the procedure will wait the amount of time designated by the user and then type what was designated by the user.

You should understand the basics of creating procedures now. If you don’t, study the examples and experiment a bit.

Functions are similar to procedures. As a beginner, I would not recommend setting options for functions. However, I will still show you how later.

We make functions return a value like this:


program New;

function Pumpkins: Boolean;
begin
Wait(100);
WriteLn('Pumpkins');
Result := True;
end;

begin
if Pumpkins = true then
WriteLn('Result was true!');
end.


First I made the function Pumpkins. I told Scar to return the value as a boolean (true or false). I then told what the function does (Waits 100 ms then writes Pumpkins). You see that Result := True? That is how we set what Pumpkins equals at the end. We could’ve done Result := false; and the function Pumpkin would return the value of false. How do we use this? Look in the main loop. We use an if then statement. If the function Pumpkins acts and returns a value of true, then do this.

Now, let’s set options for the function.


program New;

function Pumpkins(Say: String): Boolean;
begin
Wait(100);
WriteLn(Say);
Result := True;
end;

begin
if Pumpkins('Hi') = true then
WriteLn('Result was true!');
end.


All I added was an option to the function Pumpkins. In the parentheses I declared the option Say as a string. If you remember from making procedures, this is exactly the same. Say just acts as a replacer for a command in the function (In this case it replaces what WriteLn does).

Then, in the main loop we told Scar if it goes through the process of Pumpkins with the option set to 'Hi' then write Result was true!.

Functions can also return integers. Let’s look at an example.


program New;

var Sum: Integer;

function Add(a, b: Integer): Integer;
begin
Result := a + b;
end;

begin
Sum := Add(2, 3);
WriteLn('The sum is ' + IntToStr(Sum));
end.


My function in this example is called Add. This is significant to the fact that it adds the two integers the user sets in the parameters (between parentheses). I first set two options for the user (a and b). I named these as integers so they can only hold whole numbers. You will also see that I made the return value an integer (: Integer; at the end). By doing this, the result will now have to be an integer. I told Scar that the Result := a + b. This just tells Scar to add a and b and make the outcome the result integer.

I also declared a variable called Sum to receive the functions result.

I did this by making Sum := the function Add with the two integers 2 and 3. Then I inserted the integer Sum into the procedure IntToStr to show it in the Debug Box.

Procedures and functions can save you loads of scripting, so learn to use them. If any of this is confusing, try study the examples and make your own similar functions or procedures.

What we learned is that functions and procedures build on each other. Katniek used Pascal functions that someone created earlier to create his own functions and procedures. SRL then took Katniek’s functions and procedures and created more elite ones. You can place the functions and procedures in includes to use them in later scripts without writing out so many procedures and functions.

I hope this helped you. If you see any errors in my scripts, grammar, spelling, or information please say so so I can fix it.

Boreas
02-28-2007, 04:44 AM
It is also a requirement that you know how to create and use procedures.

I think you mean just use, because this tut is about the create part.

Also explain

program New;

var Sum: Integer;

procedure Add(var WhereToPutAnswer:integer; a, b: Integer);
begin
WhereToPutAnswer := a + b;
end;

begin
Add(Sum,2, 3);
WriteLn('The sum is ' + IntToStr(Sum));
end.

Good job, well explained.

WhoCares357
02-28-2007, 10:07 PM
I think you mean just use, because this tut is about the create part.

Also explain

program New;

var Sum: Integer;

procedure Add(var WhereToPutAnswer:integer; a, b: Integer);
begin
WhereToPutAnswer := a + b;
end;

begin
Add(Sum,2, 3);
WriteLn('The sum is ' + IntToStr(Sum));
end.

Good job, well explained.

I'll fix it right away Sir Boreas.

JAD
02-28-2007, 11:02 PM
its a good tutorial, but for the Wait(WaitTime) and things like that, don't you need something like this?

program Whatever;
const
WaitTime =1000 //set how many seconds to wait
procedure DoIt;
begin
Wait(WaitTime)
end;
begin
DoIt;
end.

i thought you had to use constants because it wouln't know what Wait time is without you setting a WaitTime no?

WhoCares357
03-01-2007, 12:41 AM
The options are more for the programmar rather than the user of the script.

And Wait(WaitTime). The WaitTime is transfered from the options in the procedure name. Those options act like variables. When you use the procedure you would put an integer instead of WaitTime in the option.

Like this.


procedure NowWait(WaitTime: Integer);
begin
Wait(WaitTime);
end;

begin
NowWait(100);
end.


I used the variable WaitTime as a integer in the options of the procedure and then transfered WaitTime value to the Wait function. Then in when I wanted to use NowWait, I just added 100 to it in the paraphrases to represent the integer WaitTime.

JAD
03-01-2007, 03:15 AM
so the value of (WaitTime) is (100)? sorry, thought this would be easier for me to understand :p its a good tut, I just don't get it lol. I think i might if WaitTime=100 though.. I'll ask you on MSN later though.

WhiteShadow
03-01-2007, 04:15 AM
Good job, maybe a better name for this tutorial could of been "Parameters" ?

>:}

WhoCares357
03-01-2007, 11:54 AM
Good job, maybe a better name for this tutorial could of been "Parameters" ?

>:}

Cange it then :). Makes it more relelvent. Thanks.

Caesar
03-05-2007, 12:37 AM
Very helpful tutorial. Thanks a lot :-)

Fearlesssss
05-28-2007, 10:05 PM
Nice tut it really explains it well, now I finally understand the parameters stuff(Boolean) in the functions..