Create Your Own Commands

Discussion in 'Archives' started by WhoCares357, Apr 4, 2007.

Create Your Own Commands
  1. Unread #1 - Apr 4, 2007 at 7:37 PM
  2. WhoCares357
    Joined:
    Jan 21, 2007
    Posts:
    608
    Referrals:
    1
    Sythe Gold:
    0

    WhoCares357 Forum Addict

    Create Your Own Commands

    How to Create Your Own Commands

    by WhoCares357


    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 create and 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:

    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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:

    Code:
    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.

    Code:
    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.

    Code:
    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. Please post any errors in my scripts, grammar, or information and I will change it.
     
< How to Auto Using Scar/SRL | Using SRL In Your Script >

Users viewing this thread
1 guest


 
 
Adblock breaks this site