Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Using Records in your Scripts

  1. #1
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default Using Records in your Scripts

    What are Records?
    I like to say that records are a way of storing lots of information within a defined variable. For example, I can store every type of weapon in the game in a variable called 'w.WeaponDTM'. As far as I'm concerned, with records you can store just about any type of data(Integers, TBox etc...). Why use it? Well it just saves you having to write multiple procedures for different for different types of the same item and it removes the necessity of massive if..then structures.


    Using Records

    Declaring Variables
    The first thing you need to do, is declare your entire record as a type. Now you're probably wondering what on earth a type is, well, it is used just like var and const. What goes underneath it? Well first of all you need to write what your record is, so let's do that. I'm going to use a record I used in one of my cooking scripts:
    Simba Code:
    type
      TFoodInfo = record // because I want to load the information of my Food
    ^^ That's pretty simple right?
    Now, what you want to put after that is the actual type of TFoodInfo you want to use. For example, do you want to use the colour of the food? The coordinates of the Food? What is that you want to use exactly? In my script I wanted to load my food DTM's and my food colours. So on the next line, you would put the type of information you want to store and obviously what type of data it is. You could name it anything you want, but for easier readability, it is good to name them appropriately, like so:
    Simba Code:
    type TFoodInfo = record // On the same line for readability :)
      foodDTM, foodColour: Integer; // they are both integers
    end;

    Great, so far you have written the type of record you want to use and the declared what you want to load from it. Now what we want to do is declare a variable which represents our record. Don't understand? Just read the example:
    Simba Code:
    var
      food: TFoodInfo; ; // 'food' is representing our record

    Ok stay with me here. Just to recap, I have created a record of TFoodInfo and under that record we have decided to load the DTM's and colors of my food(s). I am using the variable 'foodDTM' to represent my DTM's and 'foodColour' to represent my colours. Underneath that, I have declared the variable 'food' to represent my record. Did you get that all in one go? Good. If not, be a darling and read over it will ya?

    Loading the information
    So we're on to the next part, actually loading the information we want to store in the record, in this case - DTM's and colours.
    Let's go ahead and start the procedure under the variables we just declared:
    Simba Code:
    type TFoodInfo = record
      foodDTM, foodColour: Integer;
    end;

    var
      food: TFoodInfo;

    procedure loadFoodInfo(); // See how it fits it's purpose?
    begin

    The next line is usually always a case ... of, a case of what you ask? Well a case of whatever the person who is using your script has decided to use! Don't get it? When you declare your players, you can create and extra line called Strings[0] or Integers[0]. Then you can make the player enter which type of what they want to use. Still don't get it? Oh my:
    Simba Code:
    with players[0] do
        begin
          loginName   := '';
          displayName := '';
          password    := '';
          isActive    := ;
          isMemeber   := ;
          bankPin     := '';
          strings[0]  := 'Shrimp'; // the Food you want to use
        end;
    Ok now do you get it? Finally! Now we can use this Strings[0] in our case ...of. Like this:
    Simba Code:
    type TFoodInfo = record
      foodDTM, foodColour: Integer;
    end;

    var
      food: TFoodInfo;

    procedure loadFoodInfo(); // See how it fits it's purpose?
    begin  
      case lowercase(players[currentPlayer].strings[0]) of // see, I'm just using strings[0]

    Hopefully you know how to use cases. What goes next is your options. In this example I'm going to create a case which supports five fish, because frankly I don't need any more than five examples to show you how it works . So it should now look like this:
    Simba Code:
    type TFoodInfo = record
      foodDTM, foodColour: Integer;
    end;

    var
      food: TFoodInfo;

    procedure loadFoodInfo();
    begin  
      case lowercase(players[currentPlayer].strings[0]) of
        'shrimp':

        'crayfish':  

        'sardine':    

        'anchovies':

        'trout':      
      end;
    end;

    Now here comes the important part. What goes after your possible option is the variable representing your record followed by a full stop '.' followed by the type of value you want to use(foodDTM, foodColour). So it would look like this:
    Simba Code:
    'shrimp':       begin // This is needed because you are doing more than one thing
                          food.foodColour := ;
                          food.foodDTM := ;
                        end;
       
        'crayfish':     begin
                          food.foodColour := ;
                          food.foodDTM := ;
                        end;

        'sardine':      begin
                          food.foodColour := ;
                          food.foodDTM := ;
                        end;

        'anchovies':    begin
                          food.foodColour := ;
                          food.foodDTM := ;
                        end;


        'trout':        begin
                          food.foodColour := ;
                          food.foodDTM := ;
                        end;

    Finally we can get on to loading the actual DTM and colours. After writing in 'food.foodDTM' and 'food.foodColours' we can now equal it to our actual DTMs and colours. The finished procedure should look like this:
    Simba Code:
    type TFoodInfo = record
      foodDTM, foodColour: Integer;
    end;

    var
      food: TFoodInfo;

    procedure loadFoodInfo();
    begin
      case lowercase(players[currentPlayer].strings[0]) of
        'shrimp':       begin
                          food.foodColour := 9147041;
                          food.foodDTM := DTMFromString('Make your own DTM!');
                        end;

        'crayfish':     begin
                          food.foodColour := 2046307;
                          food.foodDTM := DTMFromString('Make your own DTM!');
                        end;

        'sardine':      begin
                          food.foodColour := 5741142;
                          food.foodDTM := DTMFromString('Make your own DTM!');
                        end;

        'anchovies':    begin
                          food.foodColour := 6512478;
                          food.foodDTM := DTMFromString('Make your own DTM!');
                        end;

        'trout':        begin
                          food.foodColour := 10132129;
                          food.foodDTM := DTMFromString('Make your own DTM!');
                        end;
      end;
    end;

    Wohooo! We have made our record! Obviously I could have used way more fish than just these five, in fact I have in my script, but just for example purposes, I have only used a small amount


    Actually using the flipping record
    So now you've created you're record, you wanna bloody use it no? Well it's simple, instead of wrting out shrimpDTM, troutDTM, pikeDTM etc... you can just replace it with food.foodDTM and whichever food the person using your script has decided to use, will be used! So like this:
    Simba Code:
    function clickFood(): Boolean;
    var
      x, y: Integer;
    begin
      if findDTM(food.foodDTM, x, y, mainScreen.getBounds) then // See? Just food.foodDTM
      begin
        mouse(point(x, y));
        if isMouseOverText(['aw'], 2000) then
        begin
          fastClick(MOUSE_LEFT);
          wait(1200 + Random(200));
          result := True;  
          exit;
        end else
          result := False;
      end;
    end;

    The final thing you have to do in order to actually use your records is load it. Where do you load it? In your mainloop of course(not scripting mainloop). So here:
    Simba Code:
    begin
      setupSRL();
      activateClient();
      declarePlayers();

      loadFoodInfo(); // before starting the actual script :)

      PlayScript();

    end.

    And there you go! That's how to create, load and use records

    Note
    • It is Pascal convention to add a "T" before the name of a user-defined type, so in this example it's TFoodInfo instead of just FoodInfo. This is not necessary but desirable.
    • Standards and functions used in this tutorial are in accordance with srl 6. Records can still however be used with OSR following the same principles.
    Last edited by Abu; 12-11-2013 at 06:09 PM.

  2. #2
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    As to "Why use it?" I would've mentioned it removes the necessity of massive if..then structures. Anyway, nice tutorial.
    There used to be something meaningful here.

  3. #3
    Join Date
    May 2007
    Location
    Waterloo, Ontario, Canada
    Posts
    1,008
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Very very nice and detailed tutorial my friend, rep received most certainly. Thanks for the addition, even I learned something here today.



  4. #4
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Frement View Post
    As to "Why use it?" I would've mentioned it removes the necessity of massive if..then structures. Anyway, nice tutorial.
    Thanks and Added

    Quote Originally Posted by 'Toxin View Post
    Very very nice and detailed tutorial my friend, rep received most certainly. Thanks for the addition, even I learned something here today.
    Thanks, I'm glad I can teach some of the more advanced users something new

  5. #5
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

    Duuuude...... you're totally mishowing the use of a record, throw some extra attributes in there, it's pretty pointless to have a record for just one attribute.

    -RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  6. #6
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Sir R. M8gic1an View Post
    Duuuude...... you're totally mishowing the use of a record, throw some extra attributes in there, it's pretty pointless to have a record for just one attribute.

    -RM
    Fine fine, I'll add some longer examples in while... *sigh*


    EDIT: In the examples I loaded DTM's as well as colours and put five examples instead of two. Hopefully this better demonstrates the uses of records

    Last edited by Abu; 04-17-2012 at 05:52 PM.

  7. #7
    Join Date
    Apr 2007
    Posts
    581
    Mentioned
    1 Post(s)
    Quoted
    2 Post(s)

    Default

    Nice tutorial, its very detailed and looks like any beginner can learn about records. Rep++

  8. #8
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by ShawnjohnSJ View Post
    Nice tutorial, its very detailed and looks like any beginner can learn about records. Rep++
    Thanks.

    Also, it's in the advanced section because you vary rarely see non-advanced scripters use records(like me for example), but it is very easy to learn and should become a good habit for everyone.

    Maybe I should get this moved to intermediates?

  9. #9
    Join Date
    Jul 2008
    Location
    NSW, Australia
    Posts
    881
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

  10. #10
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Btw it's Pascal convention to add a "T" before the name of a user-defined type

  11. #11
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Echo_ View Post
    Btw it's Pascal convention to add a "T" before the name of a user-defined type
    So it would be TFoodInfo?

  12. #12
    Join Date
    Dec 2006
    Location
    Program TEXAS home of AUTOERS
    Posts
    7,934
    Mentioned
    26 Post(s)
    Quoted
    237 Post(s)

    Default

    rep+, thanks.

  13. #13
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by P1nky View Post
    rep+, thanks.
    Thank you! I know have two bars for rep

    EDIT: Your rep power =14? Holy mother of...

  14. #14
    Join Date
    Dec 2006
    Location
    Program TEXAS home of AUTOERS
    Posts
    7,934
    Mentioned
    26 Post(s)
    Quoted
    237 Post(s)

    Default

    Quote Originally Posted by abu_jwka View Post
    Thank you! I know have two bars for rep

    EDIT: Your rep power =14? Holy mother of...
    Haha, really? Well you do deserve it, seriously a easy simple tutorial with good examples Mate. Keep it up

  15. #15
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by P1nky View Post
    Haha, really? Well you do deserve it, seriously a easy simple tutorial with good examples Mate. Keep it up
    Thanks and check my graphics thread?

  16. #16
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Quote Originally Posted by abu_jwka View Post
    So it would be TFoodInfo?
    Yep

  17. #17
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Echo_ View Post
    Yep
    Ok Thanks added at the end of the tutorial

  18. #18
    Join Date
    Jan 2012
    Posts
    522
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    So your able to make it select multiple resources but can you make it do a different method for the task with the same?

  19. #19
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Phyaskou View Post
    So your able to make it select multiple resources but can you make it do a different method for the task with the same?
    You didn't finish off your sentence there, for the task with the same....

    But I think I know where you are getting at and yes.

  20. #20
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    Quote Originally Posted by abu_jwka View Post
    Maybe I should get this moved to intermediates?
    I would certainly hope this fits there; but yes, nice tutorial.

  21. #21
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Alright thanks, I've sent in a request

  22. #22
    Join Date
    Jan 2012
    Posts
    522
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Know how you have it as "food.FoodColor := Color;"

    I get an error with mine but I changed it from "food" to "ore"
    [Error] (33:23): Unknown identifier 'ore' at line 32
    Compiling failed.
    Line 32 is that line that I have said at the very top.

  23. #23
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Phyaskou View Post
    Know how you have it as "food.FoodColor := Color;"

    I get an error with mine but I changed it from "food" to "ore"
    [Error] (33:23): Unknown identifier 'ore' at line 32
    Compiling failed.
    Line 32 is that line that I have said at the very top.
    Read through the tutorial rather than just trying to copy and paste. You will find the solution yourself and this will help you in future when using records.

    *hint* Read over the first section; Declaring Variables *hint*

  24. #24
    Join Date
    Jan 2012
    Posts
    522
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Now I get it. I guess I just read it too quick

  25. #25
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    Moved on Request

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •