Results 1 to 5 of 5

Thread: Records made easy!

  1. #1
    Join Date
    Jan 2012
    Location
    Long Island, NY
    Posts
    413
    Mentioned
    5 Post(s)
    Quoted
    95 Post(s)

    Default Records made easy!

    Hi, please enjoy the tutorial! Leave feedback/questions/etc in the comments below.

    Table Of Contents
    I. Intro to Records
    II. Accessing Records
    III. Modifying Records
    IV. Example
    V. Applying It
    VI. Closing



    I. Intro to Records:
    A record is a data structure that is useful for holding other datatypes that define some structure. Think of a human as a record holding height, weight, and age as it's data. It's simply a list or, rather, a record of information that defines what is being recorded. So let's create our first record.

    First, we must use a type keyword, this can be put at the top of your script, alongside var and const. Then we assign our name TFoo to the keyword record, and close the record with an end keyword. Every record keyword needs to closed with an end statement. Then we need any variable inside of our record, if there is an empty record then your script will not compile. So we will make a variable named a of type integer.
    Simba Code:
    type
      TFoo = record
        a : integer;
      end;

    We can of course hold multiple variables in our record, so in this example let's add another one named b of type string.
    Simba Code:
    type
      TFoo = record
        a : integer;
        b : string;
      end;

    Great! Now let's create and initialize a TFoo record in our script. This can be done by first declaring a record as a variable. Using the var keyword you would declare a record just like any other data type. We will create a TFoo record.
    Simba Code:
    var
      f : TFoo;

    We will assign the values 5 to a and 'John' to b. You can initialize it anywhere in the script by using the following.
    Simba Code:
    with t do
      begin
        a := 5;
        b := 'John';
      end;

    Back to Table of Contents



    II. Accessing Records:
    To display the contents of our record we need to create a procedure that will write the information to the console. This can be done by treating our record as an object so we just call the procedures from the records themselves. We will name our procedure print() and affix to the TFoo object type. By using self we can just apply the procedure to our record rather than calling the procedure with a record as an input parameter.
    Simba Code:
    procedure TFoo.print();
    begin
      writeln('a: ' + intToStr(self.a));
      writeln('b: ' + self.b);
    end;
    Alternatively, you could have written procedure print(t : TFoo); and replace self with t.

    By using this method, we can call print() from our TFoo object t.
    Simba Code:
    t.print();

    So if you have been following you should end up with this as your program...
    Simba Code:
    program new;
    type
      TFoo = record
        a : integer;
        b : string;
      end;
    var
      t : TFoo;

    procedure TFoo.print();
    begin
      writeln('a: ' + intToStr(self.a));
      writeln('b: ' + self.b);
    end;

    begin
      with t do
        begin
          a := 5;
          b := 'John';
        end
      t.print();
    end.

    After running the program you should get...
    Code:
    a: 5
    b: John
    Successfully executed.
    Back to Table of Contents



    III. Modifying Records:
    Suppose we want to update or change the data in our record. This is accomplished easily by writing procedures similar to print(), except we want parameters in the procedure field to accept input. Let's write two procedures to modify the a and b variables in our record.
    Simba Code:
    procedure TFoo.setA(i : integer);
    begin
      self.a := i;
    end;

    procedure TFoo.setB(s : string);
    begin
      self.b := s;
    end;

    To test it out we will call our procedures to set the values of the record t to 10 and 'Jane' for variables a and b respectively.
    Simba Code:
    t.setA(10);
    t.setB('Jane');
    t.print();

    So the finished program is as follows...
    Simba Code:
    program new;
    type
      TFoo = record
        a : integer;
        b : string;
      end;
    var
      t : TFoo;

    procedure TFoo.print();
    begin
      writeln('a: ' + intToStr(self.a));
      writeln('b: ' + self.b);
    end;

    procedure TFoo.setA(i : integer);
    begin
      self.a := i;
    end;

    procedure TFoo.setB(s : string);
    begin
      self.b := s;
    end;

    begin
      with t do
        begin
          a := 5;
          b := 'John';
        end
      t.print();
      t.setA(10);
      t.setB('Jane');
      t.print();
    end.

    Which should produce the following output...
    Code:
    a: 5
    b: John
    a: 10
    b: Jane
    Successfully executed.
    Back to Table of Contents



    IV. Example:
    For this example let's first state the problem. I work at a doctor's office that is incredibly cheap and only keeps records of your name, your DOB, your height, your weight, and your heart rate. Yup... they only keep track of the basic stuff. I have a patient who is named Paul, was born 05/04/1950, weighs 180 pounds, is 72 inches tall, and has a heart rate of 170bpm. He came in and shrunk 4 inches, but gained 300 pounds, and changed his name to Cooper(and he still doesn't have a last name.) I need a print out of the original report and the new patient report. Let's get this in code!

    What record do we need?:
    Health information is being stored regarding the patient, thus we will name our record type TPatient.

    What information goes in the record?:
    All the patient's health information goes in the record. So, name, DOB, height, weight, heart rate.
    Now we see the name of our record along with all the variables, so let's declare that.
    Simba Code:
    type
      TPatient = record
        name, DOB : string;
        height, weight, heart : integer;
      end;

    Then we want to declare a TPatient record named p.
    Simba Code:
    var
      p : TPatient;

    Now we are asked to be able to print the report, so let's write the print() procedure.
    Simba Code:
    procedure TPatient.print();
    begin
      writeln('-=-=-=-=-=-=-=-=-=-=Patient Report=-=-=-=-=-=-=-=-=-=-');
      writeln('Name: ' + self.name);
      writeln('Date of Birth(DD/MM/YYYY): ' + self.dob);
      writeln('Weight(pounds): ' + intToStr(self.weight) + 'lbs');
      writeln('Height(inches): ' + intToStr(self.height) + '"');
      writeln('Heart Rate(BPM): ' + intToStr(self.heart));
    end;

    For this problem, we need to modify the patient's height, weight, and name so let's write procedures setHeight(), setWeight(), and setName().
    Simba Code:
    procedure TPatient.setName(s : string);
    begin
      self.name := s;
    end;

    procedure TPatient.setHeight(i : integer);
    begin
      self.height := i;
    end;

    procedure TPatient.setWeight(i : integer);
    begin
      self.weight := i;
    end;

    Next is to initialize our record and print the record. We will do in the main body of the script.
    Simba Code:
    with p do
        begin
          name := 'Paul';
          dob := '05/04/1950';
          weight := 180;
          height := 72;
          heart := 170;
        end;
        p.print();

    Finally we will modify the record to match our information needed in the problem and then print it again.
    Simba Code:
    p.setName('Cooper');
    p.setHeight(p.height - 4);
    p.setWeight(p.weight + 300);
    p.print();

    Tie it all together and you should get....
    Simba Code:
    program new;
    type
      TPatient = record
        name, dob : string;
        height, weight, heart : integer;
      end;
    var
      p : TPatient;

    procedure TPatient.print();
    begin
      writeln('-=-=-=-=-=-=-=-=-=-=Patient Report=-=-=-=-=-=-=-=-=-=-');
      writeln('Name: ' + self.name);
      writeln('Date of Birth(DD/MM/YYYY): ' + self.dob);
      writeln('Weight(pounds): ' + intToStr(self.weight) + 'lbs');
      writeln('Height(inches): ' + intToStr(self.height) + '"');
      writeln('Heart Rate(BPM): ' + intToStr(self.heart));
    end;

    procedure TPatient.setName(s : string);
    begin
      self.name := s;
    end;

    procedure TPatient.setHeight(i : integer);
    begin
      self.height := i;
    end;

    procedure TPatient.setWeight(i : integer);
    begin
      self.weight := i;
    end;

    begin
      with p do
        begin
          name := 'Paul';
          dob := '05/04/1950';
          weight := 180;
          height := 72;
          heart := 170;
        end;
        p.print();
        p.setName('Cooper');
        p.setHeight(p.height - 4);
        p.setWeight(p.weight + 300);
        p.print();
    end.

    This should output....
    Code:
    -=-=-=-=-=-=-=-=-=-=Patient Report=-=-=-=-=-=-=-=-=-=-
    Name: Paul
    Date of Birth(DD/MM/YYYY): 05/04/1950
    Weight(pounds): 180lbs
    Height(inches): 72"
    Heart Rate(BPM): 170
    -=-=-=-=-=-=-=-=-=-=Patient Report=-=-=-=-=-=-=-=-=-=-
    Name: Cooper
    Date of Birth(DD/MM/YYYY): 05/04/1950
    Weight(pounds): 480lbs
    Height(inches): 68"
    Heart Rate(BPM): 170
    NICE!

    Back to Table of Contents



    V. Applying it:
    Consider the following example. I want to make an AIO fishing script using the openGL library(ogLib). The script will fish, bank, and drop any fish at a set of of large amount of locations.

    Simple problem, let's think about the example a little more. To make the script easy to configure, I will use a form that allows you to select a location and displays the level needed and the fish available at the location. Can you guess what kind of record we will make? Yup, a TLocation. So let's list out our data that will go in the script.
    TLocation:
    -Name of location
    -Are we banking?
    -Path to bank(TPoint coordinate array for walking)
    -Path to fish
    -Boundary for bank(TBox to check if current position as a TPoint is inside box)
    -Boundary for fish
    -Models for bankers(array of uInt32 to hold IDs for model search)
    -Models for fish pools
    -Textures for fish inventory(same as models, but for textures)
    -Textures for fish action bar
    Simba Code:
    TLocation = record
          name : string;
          canBank : boolean;
          pathToBank : array of TPoint;
          pathToFish : array of TPoint;
          boundaryBank : TBox;
          boundaryFish : TBox;
          bankers : array of uInt32;
          fishPools : array of uInt32;
          fishInventory : array of uInt32;
          fishActionBar : array of uInt32;
      end;

    This record will allow me to save myself a nightmare when coding out this script by keeping my data organized and also just generally easier to type in. I could try and write a list of HUNDREDS of variables/constants and try to remember their names or come up with an easy name like findFish([FISH_POOL_MODEL_ID_DRAYNOR_SHRIMP, FISH_POOL_MODEL_ID_DRAYNOR_ANCHOIVE]), ouch! By putting my TLocation inside an enumerated type and initializing that someplace I can easily call something like findFish(Draynor.fishPools).

    Back to Table of Contents



    VI. Closing:
    For more examples, and more information on records, please read the following material:
    1. Using Records in your Scripts: By Abu
    2. Record Data Structure: On Pascal-Programming.info
    3. Record: On Free Pascal Wiki



    Back to Table of Contents

  2. #2
    Join Date
    Aug 2012
    Posts
    188
    Mentioned
    9 Post(s)
    Quoted
    71 Post(s)

    Default

    Great guide! I've been implementing records in my scripts recently and it makes it so much easier!

    Note that another very useful thing with records are arrays of record. You can store all your fish spots in one Record Array!
    Also, you have the ability to pass records into function and procedures like any other variable.

  3. #3
    Join Date
    Jun 2014
    Posts
    463
    Mentioned
    27 Post(s)
    Quoted
    229 Post(s)

    Default

    I like the format of your tutorial. The layout is clean and easy on your eyes. The examples you used were perfect for this as well, good job. Repped
    Tsunami

  4. #4
    Join Date
    Sep 2012
    Location
    Australia.
    Posts
    839
    Mentioned
    16 Post(s)
    Quoted
    225 Post(s)

    Default

    Great guide here Brotein, you've put lots of effort into this and it shows. The layout is easy on the eyes and the examples make it easy for users to add records to their scripts. I look forward to more guides from you! Enjoy the +rep .

  5. #5
    Join Date
    Apr 2015
    Location
    FireFox
    Posts
    528
    Mentioned
    10 Post(s)
    Quoted
    227 Post(s)

    Default

    Fantastic guide man, may you be drowned by rep!
    Scripting with ogLib

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
  •