Results 1 to 6 of 6

Thread: Question about script

  1. #1
    Join Date
    Sep 2014
    Location
    Netherlands
    Posts
    264
    Mentioned
    11 Post(s)
    Quoted
    130 Post(s)

    Default Question about script

    Hi,

    Maybe this is a pretty dumb question, but I was reading some scripts today to learn from them. I saw a lot of times "self.xxx" in scripts. What does this mean?

    For example, in the bankscreen file: "self.setBounds(b);". I know it sets the bounds (duh), but what else is happening here? Where did they declare what setBounds means? (Sorry for my broken English, I tried to formulate it as good as possible)

    Thanks in advance,

    SlipperyPickle

  2. #2
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    'self' is a keyword referring to the parent type of a typed function.

    Consider the following function:
    Simba Code:
    function someFunction(someInteger:integer):boolean;

    It is a 'non-typed' function because it accepts no parent type. It is called like someFunction(100);

    Now consider this function:
    Simba Code:
    function TPointArray.someFunction(someInteger:integer):boolean;

    This is a 'typed' function because it is a function of type TPointArray. It is called like someTPA.someFunction(100); where someTPA is a pre-declared variable of type TPointArray.

    ***

    Now, let's continue with our 'typed function' example, so we can demonstrate how 'self' is used. For this example, I will simply wrap the TPA-debugging functions already present in SRL-6.

    Simba Code:
    function TPointArray.someFunction(someInteger:integer):boolean;
    var
      i:integer;
    begin
      for i:= 0 to someInteger do
       smartImage.debugTPA(self);
    end;

    Notice the usage of the 'self' keyword. This keyword simply tells the function to double back and perform operations on itself.

    ***

    If a user declared three variables of type TPointArray...

    Simba Code:
    var
      tpaa, tpab, tpac:TPointArray;

    And then called our TPointArray.someFunction() on them...

    Simba Code:
    tpaa.someFunction(50);
    tpab.someFunction(50);
    tpac.someFunction(50);

    The result would be all three TPAs getting debugged to the SMART canvas, 50 times each.

    This is because TPointArray.someFunction() is a function of type TPointArray, and can use the 'self' keyword to refer back to the variable that it is performing operations on (which is to the left of the dot when the call is made).

    ***

    If you'd like a different explanation, read my posts on this thread https://villavu.com/forum/showthread...99#post1305099 (starting from the post that link will drop you at)

    If you're unclear on anything, let me know and I'll explain it a different way

    As a bonus, here's some practical examples of 'self' usage which I put in all of my scripts:

    Function of type TPointArray, 'self' will refer to the TPA that the function is called on
    Simba Code:
    function TPointArray.centerPoint():TPoint;
    var
      tmp:TPointArray;
    begin
      if (length(self) < 1) then
       exit();

      tmp := self;
      tmp.sortFromPoint(self.getMiddle());
      result := tmp[0];
    end;

    Function of type TRSMinimap, 'self' will refer to the RS Minimap
    Simba Code:
    procedure TRSMinimap.waitFlagExists(timeout:integer); //waits up to timeout for the flag to be gone
    var
      t:TTimeMarker;
    begin
      if (not isLoggedIn()) then
       exit();

      if tabBackpack.isFull() then
       exit();

      if (not (self.isFlagPresent())) then
       begin
         writeWarn('Flag not present, unable to wait while flag exists');
         exit();
       end;

      t.start();

      while (t.getTime() < timeout) do
       begin
         writeDebug('Waiting while the flag exists');
         wait(randomRange(250, 500));

         if (not (self.isFlagPresent())) then
          exit();
       end;
    end;

    E:

    Oh and also, Lape is really cool so you can even leave the 'self' keyword out, and the interpreter will still read your code correctly. But that's bad practice.
    Last edited by KeepBotting; 10-13-2015 at 07:43 PM.
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  3. #3
    Join Date
    Sep 2014
    Location
    Netherlands
    Posts
    264
    Mentioned
    11 Post(s)
    Quoted
    130 Post(s)

    Default

    Thanks, this will give me enough to study for tonight!

  4. #4
    Join Date
    Jan 2013
    Posts
    146
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by KeepBotting View Post
    'self' is a keyword referring to the parent type of a typed function.

    Consider the following function:
    Simba Code:
    function someFunction(someInteger:integer):boolean;

    It is a 'non-typed' function because it accepts no parent type. It is called like someFunction(100);

    Now consider this function:
    Simba Code:
    function TPointArray.someFunction(someInteger:integer):boolean;

    This is a 'typed' function because it is a function of type TPointArray. It is called like someTPA.someFunction(100); where someTPA is a pre-declared variable of type TPointArray.

    ***

    Now, let's continue with our 'typed function' example, so we can demonstrate how 'self' is used. For this example, I will simply wrap the TPA-debugging functions already present in SRL-6.

    Simba Code:
    function TPointArray.someFunction(someInteger:integer):boolean;
    var
      i:integer;
    begin
      for i:= 0 to someInteger do
       smartImage.debugTPA(self);
    end;

    Notice the usage of the 'self' keyword. This keyword simply tells the function to double back and perform operations on itself.

    ***

    If a user declared three variables of type TPointArray...

    Simba Code:
    var
      tpaa, tpab, tpac:TPointArray;

    And then called our TPointArray.someFunction() on them...

    Simba Code:
    tpaa.someFunction(50);
    tpab.someFunction(50);
    tpac.someFunction(50);

    The result would be all three TPAs getting debugged to the SMART canvas, 50 times each.

    This is because TPointArray.someFunction() is a function of type TPointArray, and can use the 'self' keyword to refer back to the variable that it is performing operations on (which is to the left of the dot when the call is made).

    ***

    If you'd like a different explanation, read my posts on this thread https://villavu.com/forum/showthread...99#post1305099 (starting from the post that link will drop you at)

    If you're unclear on anything, let me know and I'll explain it a different way

    As a bonus, here's some practical examples of 'self' usage which I put in all of my scripts:

    Function of type TPointArray, 'self' will refer to the TPA that the function is called on
    Simba Code:
    function TPointArray.centerPoint():TPoint;
    var
      tmp:TPointArray;
    begin
      if (length(self) < 1) then
       exit();

      tmp := self;
      tmp.sortFromPoint(self.getMiddle());
      result := tmp[0];
    end;

    Function of type TRSMinimap, 'self' will refer to the RS Minimap
    Simba Code:
    procedure TRSMinimap.waitFlagExists(timeout:integer); //waits up to timeout for the flag to be gone
    var
      t:TTimeMarker;
    begin
      if (not isLoggedIn()) then
       exit();

      if tabBackpack.isFull() then
       exit();

      if (not (self.isFlagPresent())) then
       begin
         writeWarn('Flag not present, unable to wait while flag exists');
         exit();
       end;

      t.start();

      while (t.getTime() < timeout) do
       begin
         writeDebug('Waiting while the flag exists');
         wait(randomRange(250, 500));

         if (not (self.isFlagPresent())) then
          exit();
       end;
    end;

    E:

    Oh and also, Lape is really cool so you can even leave the 'self' keyword out, and the interpreter will still read your code correctly. But that's bad practice.
    Thorough explanation +Rep

    Im Back... Previously known as Megaleech
    [Herbalife]

  5. #5
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    I'd recommend learning about static methods in Java. The concept is relevant here.

  6. #6
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by KeepBotting View Post
    Oh and also, Lape is really cool so you can even leave the 'self' keyword out, and the interpreter will still read your code correctly. But that's bad practice.
    Not entirely accurate.
    Omitting `self` when calling any method will yield a search through every declaration that's already declared at this point in your code, none of the stuff which is declared later on will be searched through.

    Example - this will fail:
    Simba Code:
    function TPoint.Magnitude(): Double;
    const null:TPoint = [0,0];
    begin
      Result := DistanceTo(null); //ERR!!ASFASDAhGH NOOOO!!!!! plz! Where are you!!!???ASDAASDASDARRRRG
    end;

    function TPoint.DistanceTo(pt:TPoint): Double;
    begin
      Result := Sqrt(Sqr(pt.x-self.x)+Sqr(pt.y-self.y));
    end;

    This however will work just fine
    Simba Code:
    function TPoint.Magnitude(): Double;
    const null:TPoint = [0,0];
    begin
      Result := self.DistanceTo(null);
    end;

    function TPoint.DistanceTo(pt:TPoint): Double;
    begin
      Result := Sqrt(Sqr(pt.x-self.x)+Sqr(pt.y-self.y));
    end;




    So you wanna know a trick, you say? Say you want to forward a method, but you dislike creating stupid forward declarations, this can save your life
    Simba Code:
    function PointMagnitude(pt:TPoint): Double;
    const null:TPoint = [0,0];
    begin
      Result := system.DistanceBetween(pt,null); //WTF!!! DistanceBetween isn't yet declared but we can call it just fine :o MAGIC!
    end;

    function DistanceBetween(self,other: TPoint): Double;
    begin
      Result := Sqrt(Sqr(self.x-other.x)+Sqr(self.y-other.y));
    end;

    begin
      WriteLn(PointMagnitude([10,10]));
    end;



    Quote Originally Posted by Sin View Post
    I'd recommend learning about static methods in Java. The concept is relevant here.
    While we are mentioning java:




    Good talk.!
    Last edited by slacky; 10-20-2015 at 06:07 AM.
    !No priv. messages please

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
  •