Results 1 to 9 of 9

Thread: Records, Types, and Psuedo-OOP

  1. #1
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Records, Types, and Psuedo-OOP

    Records and Psuedo-OOP

    OMG... what do you mean by Psuedo-OOP?
    When I say Psuedo-OOP I mean "fake object oriented programming". Simba doesn't have a totally object oriented way of things to speak of. We do have records however, which is what this tutorial is all about.

    What exactly is a record?
    I like to think of a record as a collection of data. It, for example, can be a collection of an integer, a string, and another integer. When you are scripting, you've probably run into a thing called a TPoint. You've used these with TPAs or TPointArrays. If you don't know these, they are just a type of data that represents a point on the screen, or a coordinate plane. A TPoint is an example of a record. it holds two integers, x and y. Somewhere, there is code that defines what exactly a TPoint is. It would be coded somewhat like the following. We will break this down line by line in a second.
    SCAR Code:
    TPoint = record
      x: Integer;
      y: Integer;
    end;
    Now, let's look at the first line :
    SCAR Code:
    TPoint = record
    "TPoint" shows the name of the new record we are creating. By saying, " = record" we are saying that it is a record (there are a few other things not explained in the tutorial that could take the place of the word "record").

    Now let's look at the variable declaring lines :
    SCAR Code:
    x: Integer;
    y: Integer;
    These lines tell us what variables are going to be contained in our record and what their names will be. To give an example of how these could be used, consider the following code where point is a variable of type TPoint. This code would be found in the program long after the definition of the record.
    SCAR Code:
    point.x := 5;
    point.y := 10;
    This code would set the value of variable "x" in the record "point" to 5 and the value of the varible "y" in the record "point" to 10. You can later call these values as in the following code.
    SCAR Code:
    Writeln(IntToStr(point.x));
    Writeln(IntToStr(point.y));
    Now that you understand the basics of what a record is and how one works, let's look at how to make our own records!

    In Simba, you know that you can declare variables in the following way.
    SCAR Code:
    var
      i: Integer;
    You also know that you can declare constants in a similar way
    SCAR Code:
    const
      i = 5;
    You can declare types or records in a similar way! Just as with the const and var words. You can put the word type after the "program somename;" line to create records! Here is an example of how you might create the record TStringAndInteger in a program.
    SCAR Code:
    program New;
    type
      TStringAndInteger = record
        str: String;
        i: Integer;
      end;

    var
      foo: TStringAndInteger;
    begin
      foo.str := 'Some String Here';
      foo.i := 5;
    end.
    Beyond this point in the tutorial is just an example of how to use records while scripting.
    In case some of you curious learners were wondering, records can also hold arrays! I use this when I write reflection walking so that I can associate each path with a name. Here is the record definition that I would use.
    SCAR Code:
    type
      TRefPath = record
        path: TPointArray;
        name: String;
      end;
    Later on in my program, I would set a few TRefPath variables up!
    SCAR Code:
    var
      willows,oaks: TRefPath;

    procedure DefinePaths;
    begin
      willows.path = [Point(1,1)] // This line would be the TPoint array of all the tiles for the path.
      willows.name = 'Willows Path'; // This is the name of the path.

      oaks.path = [Point(1,2)] // This line would be the TPointArray for all the tiles in the path
      oaks.name = 'Oaks Path'; // This is the name of the path.
    end;
    Lastly, I would create a procedure to say which path the script was walking by using the name variable and walk the path using the path variable. However, I will not show you this code because I think you should write it yourself.

    A Last Blurb on Object Oriented Programming (OOP)
    In my opinion, OOP can make your code much easier to understand and quite honestly, easier to write. A record in Simba is an example of an object. If you get deeper and deeper in to object oriented programming, you will see that things called classes can be used to define procedure and functions from within an object. so, you could call something like this:
    SCAR Code:
    someObject.WalkThePath(someTPointArray);
    If you are interested in Object Oriented programming, specifically in the language of Java, I would check out Chapter 5 of this book : Javanotes.

    In object oriented programming, you can create a thing called a pointer a pointer stored the location of a function or a procedure so that you can call the function or procedure under a different name. Take this record as an example.
    SCAR Code:
    program New;
    type
      TNamedMethod = record
        p: procedure(s: String);
        name: String;
      end;
    var
      rec: TNamedMethod;
     
    procedure say(s: String);
    begin
      Writeln(s);
    end;
    begin
      rec.p := @say;
      rec.name := 'Say';
     
      Writeln('Running: ' + rec.name);
      rec.p('HAI');
    end.
    This shows you that you can run a procedure through a pointer. Now that you've seen that a record can contain a pointer, I'll tell you that you can have a pointer outside of a record as well. See this final example.
    SCAR Code:
    program New;
    var
      p: procedure(s: String);
     
    procedure say(s: String);
    begin
      Writeln(s);
    end;
    begin
      p := @say;
      p('HAI');
    end.

    Happy Programming! If you have any questions, post them here and I will add them to the tutorial!

    This tutorial may not be reproduced in any way, shape, or form without prior written permission from the owner, Tniffoc.
    Last edited by Tniffoc; 08-19-2010 at 04:11 AM. Reason: Updated for Simba

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

  2. #2
    Join Date
    Jul 2007
    Location
    Missouri
    Posts
    318
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice, I never really understood records, it cleared up quite a bit for me.

    Thanks. Rep++

  3. #3
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by Illkillutill View Post
    Nice, I never really understood records, it cleared up quite a bit for me.

    Thanks. Rep++
    Thanks for posting! I'm glad the tutorial is doing it's job!

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

  4. #4
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    w00t nice! rep+! Im going to be using this strategy in my scripting from now on xP

  5. #5
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Woot! Glad I could help y'all.

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

  6. #6
    Join Date
    Aug 2007
    Location
    Where do you live?
    Posts
    934
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    "fake object oriented programming"
    Hehe...

    Very nice, I already know bout records and such, but still a good tut

    SCAR Code:
    Inc(rep);

  7. #7
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Woot. I'm glad this is working out so well.

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

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

    Default

    First time I've seen the procedure pointers in SCAR. I wish you had gone a little more in depth with them.

    For example, do the arguments have to be the same identifier (like "s" in your example) or do they just need to be the same type? And what's the advantage of using that? It seems simpler and quicker to just use "say('blah');" instead of declaring a pointer, directing it to the procedure, and finally using it through that.

    Otherwise a fine tutorial. I actually learned something new .
    Last edited by WhoCares357; 04-14-2009 at 09:19 PM.

  9. #9
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by WhoCares357 View Post
    First time I've seen the procedure pointers in SCAR. I wish you had gone a little more in depth with them.

    For example, do the arguments have to be the same identifier (like "s" in your example) or do they just need to be the same type? And what's the advantage of using that? It seems simpler and quicker to just use "say('blah');" instead of declaring a pointer, directing it to the procedure, and finally using it through that.

    Otherwise a fine tutorial. I actually learned something new .
    Sounds like more on procedure pointers is in demand. I'll write up a section and send you a pm when it is finished.

    Quote Originally Posted by Aequix View Post
    Brilliant Tutorial, love it. Already read over a few OOP tutorials in my time studying programming - more specifically for C++ and a brief one on Java, but I love the brief blurb on it at the end of the tutorial. The Record section is just as great, simply explained and easy to grasp. Certainly gathered my interest and hope it will do the same for others.

    Just as a brief few questions - although I'll do some looking myself perhaps you know - is OOP used widely in SCAR at the current time? Or is it just a minor implementation that a few people use? Also, would you advise learning more OOP techniques in the aim of developing Scripts using them?

    Cheers.
    OOP is used in only the more complex scripts. I use it because...
    1. It looks cool.
    2. It's easier to write for multiple places.


    I would advise learning it, yes.

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

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
  •