Results 1 to 23 of 23

Thread: Types and Arrays

  1. #1
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default Types and Arrays

    Hello and welcome to the Tutorial:

    Types and Arrays



    Chapters

    • What's a type?
    • How do I make a type?
    • How do I set a type?
    • How do I use a type?
    • What's an array?
    • How do I make an array?
    • How do I use an array?
    • Combining types and arrays




    What is a type?

    A type is a list of custom variables. The name of the type is called record. For example DoorProfiles:

    SCAR Code:
    type DoorProfile = record // This is the name of your type
      Points: array of tpoint;  // These are the things you can call out of a DoorProfile type. (Custom variables).
      MidPoint: Tpoint;
      PixelCount: integer;
      Slope: extended;
      Color:integer;
    end;

    Another example, the player array:

    SCAR Code:
    type
      TUser = record
        Name: string; // * User Name
        Pass: string; // * User Pass
        Nick: string; // * Screen Name for random detection
        Active: Boolean; // * Set to True if Ok, False if Lost.
        Loc: string; // * User Location
        Rand: string;
        Skill: string; // * User Action to Perform
        Level: array[0..21] of word;
          // * Levels of all skills. SetIn GetPlayerLevels.
        Ore: string; // * test
        WaitTime: Integer; // * Wait Time for Each User, Good for Autofighters
        Fight: Boolean; // * If we want the Char to randomly fight
        Worked: Integer; // * Time User has worked
        Banked: Integer; // * Number of Banks User has done
        Killed: Integer; // * Number of Kills User has made
        Pin: Integer;    // * Current Users Pin Number
        Boolean1: Boolean; // * For reports, etc.
        Boolean2: Boolean; // * For reports, etc.
        Boolean3: Boolean; // * For reports, etc.
        Integer1: Integer; // * For reports, etc.
        Integer2: Integer; // * For reports, etc.
        Integer3: Integer; // * For reports, etc.
        Integer4: Integer; // * For reports, etc.
        String1: string; // * For reports, etc.
        String2: string; // * For reports, etc.
        String3: string; // * For reports, etc.
        Extended1: Extended; // * For reports, etc.
        Extended2: Extended; // * For reports, etc.
        Extended3: Extended; // * For reports, etc.
      end;

    Another example, TPoint:

    SCAR Code:
    type Tpoint = record
     x, y: integer;
    end;



    How do I make a type?

    If you start making a type, you need to have a name. The name is what you put between type and = record:

    SCAR Code:
    type RGB = record

    After the name you can just make a list of what you would like to integrate in you type:

    SCAR Code:
    type RGB = record
      green: integer;
      blue: integer;
      red: integer;

    {SHORTER:}

    type RGB = record
      green, blue, red: integer;

    And what you don't need to forget: an end at the end of your list!!:

    SCAR Code:
    type RGB = record
      green: integer;
      blue: integer;
      red: integer;
    end;

    {SHORTER:}

    type RGB = record
      green, blue, red: integer;
    end;



    How do I set a type?

    You can only set types at the begin of you script. That means you can't set a type in a procedure or function!
    If you set a type at the start of your script, it doesn't mean that it's a variable! If you take a look at the example of the player array above, you can see that the type is called TUser and not Players. That's why you need to set your type as a variable. Example:

    SCAR Code:
    var  Player: TUser; //Now you can use the TUser in your script.
      var  RGBColors: RGB //Now you can use RGB in your script.

    WRONG:
    SCAR Code:
    TUser.Name := ' '
    TUser.Pass := ' '
    TUser.Active := True;

    RGB.Blue :=
    RGB.Green :=
    RGB.Red

    GOOD:
    SCAR Code:
    var Player: TUser;
    var RGBColors: RGB;

    Player.Name := ' '
    Player.Pass := ' '
    Player.Active := True;

    RGBColors.Blue :=
    RGBColors.Greem :=
    RGBColors.Red :=




    How do I use a type?

    You use your types just as you use a normal variable. Example:

    SCAR Code:
    a := 8;
    b := 5;

    RGBColors.Blue := 23;
    RGBColorS.Green := 3;
    RGBColors.Red := 80;

    a := GetColor(123, 456);
    RGBColors.Blue := GetColor(456, 789);

    FindColor(Tpoint.x, Tpoint.y, 123, 456, 789, 765);




    What is an array?

    An array is a list of variables defined by numbers. Example:

    SCAR Code:
    var HeadColor: array[0..2] of Integer;

    { This will contain HeadColor[0], HeadColor[1], HeadColor[2] }

    var MonsterColors: array of Integer;

    { This doesn't have a length yet }




    How do I make an array?


    To make an array, you first need to set your array as an variable:

    SCAR Code:
    var MonsterColors: array of integer; //If you don't know the length of the array yet

    var MosterColors: Array[0..10] of integer; //If you know that the length will be 11 (you can also set something on 0).

    Now you can define everything in the array:
    SCAR Code:
    {If you know the length:}
    var MonsterColors: array[0..4] of integer;

    MonsterColors[0] := 1;
    MonsterColors[1] := 2;
    MonsterColors[2] := 3;
    MonsterColors[3] := 4;
    MonsterColors[4] := 5;

    {If you DONT know the length:}
    var MonsterColors: array of integer;

    MonsterColors := rs_ScanMiniMap(1234) //rs_ScanMiniMap will return all similar colors into an array of integer. It sets the length automaticly.




    How do I use an array?

    Now you have set your array, you can start using it:
    SCAR Code:
    If FindMSColor(x, y, MosterColors[0]) or FindMSColor(x, y, MosterColors[1]) or FindMSColor(x, y, MosterColors[2]) then WriteLn('One of the colors was found.);

    The way above is the long way and most of the only only works if you know the length of the array. A quicker and way more used way to do it is this:

    SCAR Code:
    var i: integer;

    {If you know the length:}
    for i := 0 to 4 do //This will perform FindMSColor 5 times with every time an other part of the array
    begin
      if FindMSColor(x, y, MonsterColors[i]) then WriteLn('Color found');
    end;

    {If you DONT know the length - This way can also be used for the array above}
    //With GetArrayLength you get the length of the array. You always have to do -1 after GetArrayLength if you use for..do!!!
    for i := 0 to GetArrayLength(MonsterColors) -1 do
    begin
      if FindMSColor(x, y, MonsterColors[i]) then WriteLn('Color found');
    end;



    Combinding types and arrays

    It may be very handy to combine types and arrays. For example the PlayerArray is built this way. First there was made a type of one user (Tuser) and after that they made an array of TUser (Named Players):

    SCAR Code:
    Players: array of TUser;

    This way you can easily set all your players:

    SCAR Code:
    Players[0].Name     :='';
        Players[0].Pass     :='';
        Players[0].Nick     :='';
        Players[0].String1  :='';
        Players[0].Integer1 := 1 ;
        Players[0].Active   := True;
        Players[0].Boolean1 := True;

        Players[1].Name     :='';
        Players[1].Pass     :='';
        Players[1].Nick     :='';
        Players[1].String1  :='';
        Players[1].Integer1 := 1 ;
        Players[1].Active   := True;
        Players[1].Boolean1 := True;

    There are various other this where it may be handy to combine types with arrays. But I hope that after reading this tutorial you have the knowledge you make them yourself!

    Thanks for reading,
    Nielsie95
    Hup Holland Hup!

  2. #2
    Join Date
    Apr 2007
    Posts
    220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Very nice tut man! I first didn't even knew what types were.
    (W00T first post)

  3. #3
    Join Date
    Oct 2006
    Location
    Ireland
    Posts
    855
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This is just what I needed.... The srl player array doesnt include enough integers strings and bools for my liking.

  4. #4
    Join Date
    Jan 2007
    Location
    Not here
    Posts
    1,604
    Mentioned
    2 Post(s)
    Quoted
    19 Post(s)

    Default

    yay I understand arrays yes I know shocking.....I was an SRL member and didn't understand arrays *hangs head*

    Anyway thx Niel as always!
    Sleeping...

  5. #5
    Join Date
    Sep 2006
    Location
    New Jersey, USA
    Posts
    5,347
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Nice tutorial

    Was a great refresher for types; I'm always forgetting how to use them correctly
    Interested in C# and Electrical Engineering? This might interest you.

  6. #6
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Great tutorial... even though I knew this stuff . I'm just posting a suggestion. You can also use type like this
    SCAR Code:
    type
      Number = Integer;
    Which would let you make variables like this
    SCAR Code:
    var
      MyNumber: Number;
    Which would be the same as saying
    SCAR Code:
    var
      MyNumber: Integer;

    Good tutorial though!

  7. #7
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Yes, thanks for the suggestion . But I think people would rather use normal variables for that.
    Hup Holland Hup!

  8. #8
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

    Default

    nice tut A+

  9. #9
    Join Date
    Dec 2006
    Location
    Third rock from the sun.
    Posts
    2,510
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Oooh, thank you

    I think I'm gonna have to use Types for my project in Delphi. Thank for for making this

  10. #10
    Join Date
    Jun 2007
    Location
    Tampa, FL
    Posts
    39
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    is it possible to use an integer as an array length like array of [0..I]
    Can't sleep Clowns will eat me
    Can't eat Clowns will sleep with me

  11. #11
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Yes:

    Array [0..1] of Integer;
    Hup Holland Hup!

  12. #12
    Join Date
    Oct 2006
    Posts
    1,071
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Wow thank you for this, have been trying to figure out arrays for awhile now

  13. #13
    Join Date
    Jun 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Great tut!

  14. #14
    Join Date
    Mar 2007
    Posts
    674
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default

    i have been looking for this tut, just found it. Thanks

  15. #15
    Join Date
    Apr 2007
    Location
    new zealand
    Posts
    87
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    looks good im goin 2 read now been looking for array tut 4 a while lol ty

  16. #16
    Join Date
    Mar 2007
    Posts
    1,700
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    This is exactly what I needed! These had been confusing for me and the already existing tuts didn't make much sense...Thanks!

  17. #17
    Join Date
    Aug 2007
    Location
    Texas
    Posts
    16
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Awesome Tutorial, now maybe I can use arrays in my first sript.

  18. #18
    Join Date
    Dec 2006
    Location
    SC
    Posts
    692
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Very helpful. Thank you.

  19. #19
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Gah, I haven't slept for like 20 hours and I got it in first time...Congratz for me
    heh my brilliant art piece...
    SCAR Code:
    program New;

    type
    EPoint = record
    x : integer;
    y : integer;
    z : integer;
    end;

    procedure lol;
    var ThePoint : Epoint;
    begin
      thepoint.x := random(100);
      thepoint.y := random(100);
      thepoint.z := random(100);
      writeln(inttostr(thepoint.x)+' ,'+inttostr(thepoint.y)+' ,'+inttostr(thepoint.x));
    end;

    begin
      lol
    end.

    Edit:
    Thank you for good tutorial
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

  20. #20
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    It's a good start
    Now get some sleep
    Hup Holland Hup!

  21. #21
    Join Date
    Sep 2007
    Location
    Inside Your Mother
    Posts
    109
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Hey i understood what you were talking about but i still dont understand where all of those things would go in a script. Like im confused... can ne1 help me out?

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

    Default

    nice tut

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

    Default

    You should probably look in the not outdated tutorial for more relevant information. You should also take care to not post spam on topics.

    "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."

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Types, Arrays, and Classes
    By Dan Cardin in forum OSR Intermediate Scripting Tutorials
    Replies: 10
    Last Post: 07-14-2008, 08:25 PM
  2. Arrays, stuck on arrays
    By Camaro' in forum OSR Help
    Replies: 1
    Last Post: 03-08-2008, 02:02 AM
  3. Types!
    By SKy Scripter in forum Outdated Tutorials
    Replies: 6
    Last Post: 10-15-2007, 03:39 AM

Posting Permissions

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