Results 1 to 17 of 17

Thread: [Reflection][Lape]Basic Reflection Include Tutorial

  1. #1
    Join Date
    Oct 2006
    Posts
    6,752
    Mentioned
    95 Post(s)
    Quoted
    532 Post(s)

    Default [Reflection][Lape]Basic Reflection Include Tutorial

    Basic Reflection Include Tutorial



    So now that the Lape version of reflection is finished and released, I decided to make a two part tutorial covering the setup of the include, and how to go about scripting with it. The first part is the "basic" tutorial and will cover all that you need to know in order to start using the include in your scripts. The second part will be the "advanced" tutorial and will cover the more complex part of the include, such as using widgets to create your own functions, and use different functions within to make other functions. I am writing this in a way aimed at people that already have grasped the basics of scripting in simba, and as such will not overly comment on examples not regarding reflection.


    Table of Contents:
    • Overview
    • Static functions
      • Compass
      • Map
      • Tiles
      • Text
      • Bank
      • Chat
      • Internals
    • Non Static functions
      • LocalPlayer
      • Npc
      • Players
      • Objects
      • Items
    • Closing


    Overview

    The include is broken up into basically two different "categories." For the sake of making this tutorial clear, I will refer to them as "Static" and "Non Static" functions/procedures. For those of you who are not familiar with a java based language, if something is static, it can be accessed throughout the program, without creating an instance of the object type. Now in Lape, we can't directly make a function static or not, but we can use types, and declare as a variable.
    Don't worry if that doesn't make sense, it should become quite clear while going through the rest of the tutorial. Another point I would like to make. While talking about the functions in this tutorial, I will at times say that one section only has x number of functions, when it may actually have more in the ".simba." The other functions will be accessed by Reflect.Internals, and only used internally.

    The include currently relies of AeroLib for mouse functions, login functions and locations. I will soon add locations to widgets and the other necessary features and this include will be able to be used with or without AeroLib.




    Static Functions

    Like I mentioned in the Overview, the functions in the include that are considered static can be called and used without creating a variable. This part of the tutorial will be most familiar to people who have scripted with SRL-5 and/or SRL-OSR. Since they used Pascal script, this was the only option when scripting. In order to access any of the static functions, we use the variable named "Reflect." This contains all of the categories for the functions in the include.
    For example:





    Compass

    This section is a pretty self explanatory one, as there are only 3 functions.
    Simba Code:
    function AngleDeg: Extended;//Returns the Degree of compass in Degrees
    function AngleRad: Entended;//Returns the Degree of compass in Radians
    procedure Make(Direction: Variant);// Will change compass to specified direction

    Map

    Another Short section, as the only function is:
    Simba Code:
    function TileOnMM(Tile: TTile): Boolean;//Returns true if the tile is on the Minimap

    Tiles

    Tiles.simba contains several very useful functions, which are used in almost every reflection script.

    Simba Code:
    function DistanceFromTile(Tile: TTile): Integer;//Returns distance(In tiles) from given TTile.
    function GetPlane: Integer;//Returns current plane(Floor level)
    function GetTileHeight(Tile: TTile);//Returns Height of given TTile.
    function InPolygon(Area: TTileArray): Boolean//Returns if we are inside polygon of tiles.
    function NearTile(Tile: TTile; Dist: Integer): Boolean;//Returns if we are within Dist from Tile
    function TileToMM(Tile:TTile): TPoint//Will return a TPoint Mainscreen coord on the minimap
    //given a TTile.

    Simba Code:
    function TileToMS(Tile: TTile; XOffset: Integer = 0; YOffset:
      Integer = 0; ZOffset: Integer = 0): TPoint;

    TileToMs is one of the most important functions in the include when it comes to interacting with anything in game. It returns mainscreen coords when given a TTile. This is used whenever we want to interact with npcs, players, objects, ground items, or just Tiles. We are able to add pixel offsets to the function, which are independent of the compass rotation.

    Text

    The functions involving text, Reflect.Text, Are all the exact same as they are in other SRL color includes, so they should need no explaining.
    Simba Code:
    function ChooseOption(Option: String): Boolean;
    function GetUpText: String;
    function IsUpText(UpText: String): Boolean;
    function IsUpTextMulti(UpText: TStringArray): Boolean;
    function WaitUpText(S: String; Time: Integer): Boolean;
    function WaitUpTextMulti(S: TStringArray; Time: Integer): Boolean

    Bank

    Bank.simba contains both Static and Non Static functions. Opening and closing the bank are done using static functions, as expected. The core functions dealing with the items within the bank are non static functions, but are also referenced via static functions. The list of all functions are:
    Simba Code:
    procedure TReflectionBank.IsOpen;
    procedure TReflectionBank.Close;
    function TReflectionBank.ItemCount: Integer;
    procedure TReflectBankItemArray.GetAll;
    function TReflectBankItem.Find(TheItem: Variant): Boolean;
    function TReflectBankItem.Withdraw(Amount: Integer): Boolean;
    function TReflectBankItemArray.GetAll: TReflectBankItemArray;
    function TReflectBankItemArray.Find(var BankItem:TReflectBankItem, TheItem: Variant): Boolean;
    function TReflectBankItemArray.Withdraw(TheItem: Variant; Amount: Integer): Boolean;

    Declaring a variable as a TReflectBankItem/TReflectBankItemArray will typically be the best option, as you can gather much more data, but there are times where using a static function will be much cleaner and easier to use.

    Example of how to use both:
    Simba Code:
    var Arrow: TReflectBankItem;

    begin
      if not Reflect.Bank.IsOpen then
        Exit;
      if Arrow.Find('Iron arrow') then //Case sensitive
      begin
        if Arrow.Quantity < 100 then
        begin
          WriteLn('Low on arrows, Quiting!');
          TerminateScript;
        end;
        Arrow.Withdraw(100);
        Reflect.Bank.Close;
      end;
    end.
    Simba Code:
    begin
      if not Reflect.Bank.IsOpen then
        Exit;
      Reflect.Bank.Withdraw('Iron arrow', 100);//Case sensitive
      Reflect.Bank.Close;
    end.

    So, using the static bank functions, it is much quicker/shorter code in order to withdraw an item, the downside is of course the fact that you can't access any data from the item. Also note that you can get any data from TReflectBankItem that you can from the Item Record.

    Chat

    The last main section of Static functions for scripter's is Chat.simba. This is another section where functions will be added in at a later date. It will contain all functions regarding game chat within Rs. Currently, the functions are:
    Simba Code:
    function GetLastMessage: String;//Returns the text of last public chat message
    function TReflectionChat.NpcChooseOption(Options: TStringArray): Boolean;//Selects an option from
    //an npc chat given an array of string
    function TReflectionChat.NpcChooseOption(Option: string): Boolean; overload;//Same as above,
    //except it takes a single string
    function TReflectionChat.NpcClickToContinue: Boolean;//Clicks continue through all chat

    Internals

    I will only breifly talk about this section, saving it for the next part of the tutorial, as most scripter's will not need any of the functions within this section. Like I mentioned in the Overview, many functions throughout the include, are labeled as a "Internals" function. While it may be related to the category of the ".simba" it is in, It shouldn't be used by scripter's regularly. While there are many functions within "Internals" I will only mention two in this tutorial.
    Simba Code:
    function GetSetting(Setting: Integer): Integer;
    function GetSettingArray: TIntegerArray;

    These two functions could have been in another category, as they can be very useful as a scripting tool, but since they don't do anything useful directly, I decided to keep them in Internals. The best way I can think of to describe the Settings array, are as a integer array of game client variables. A lot of useful values are stored in this array, that change when the in game value change. For example:
    Simba Code:
    Result := Reflect.Internals.GetSetting(102) > 0;

    That is in the function TReflectLocalPlayer.IsPoisoned; If the integer value of the setting array at a index of 102 is any positive value, then we are poisoned. This can get even more precise, as the higher the value, the more poisoned we are (I believe the highest is 25). I won't go into any more examples as I would like for you to go find out neat uses for this set of functions your self! Think: Farming, combat options, ect..




    Non Static Functions

    This section is where the bulk of the tutorial will be, as the majority of the include is made up of these Non Static functions. Like previously stated, The term "Non Static", in the sense I am referring to, is any function that can only be accessed through declaring variables. This is done in order to make scripts be able to be written much neater, and in a more object orientated manner. Also, when finished reading this section, it should also make sense why it is done this way.

    LocalPlayer

    I wasn't planning on talking at all about what classes are inherited from what in the RS client or the include, but I feel now as though it will make it easier to understand. Simply put, in java, if a class inherits from another class, it will then contain all methods/fields from the inherited class. So, in the rs client, LocalPlayer, Npcs, and other players all inherit from a class called Actor. So, as you will soon see, LocalPlayer, Npcs, and other players share many of the exact same functions.
    With that out of the way, on to LocalPlayer!

    In the include, the variables we define are prefixed with: TReflect, so LocalPlayer is: TReflectLocalPlayer; In almost every script you will write, you will want to define a global variable as a TReflectLocalPlayer.

    Simba Code:
    var
      MyPlayer: TReflectLocalPlayer;

    So, using MyPlayer, we are able to access all sorts of data and functions regarding our player that are quite useful.
    Important Note: In order to handle memory usage smarter, whenever we log into rs, we must call:
    Simba Code:
    MyPlayer.Create;
    This loads the object reference to our player, enabling us to access the data. So, after our player is created, the rest of the functions regarding our player are quite easy to use without much confusion. Since I do not feel like typing all of the functions out, here is a screenie of them:



    Also, for what ever reason, simba's code completion doesn't show inherrited functions all the time, so TReflectLocalPlayer, Npcs, and other players ALL have the following functions:
    Simba Code:
    function GetSpokenText: String;//Returns current overhead text, if any.
    function GetTile: TTile;//Returns current tile/
    function GetQueue(Index: Integer = 0): TTile;//Returns tile in walking path
    function GetAnimation: Integer;//Returns Animation
    function GetHealth: Integer;//Returns top number health (Must be in combat to access)
    function GetMaxHealth: Integer;//Returns Bottom number Health(Must also be in combat)
    function GetInteractingIndex: Integer;//Returns index of npc/player interacting with
    function GetCombatCycle: Integer;//Returns cycle while in combat, == ClientLoop if not

    Npcs

    So, like just mentioned, since Npcs and LocalPlayer inherit from Actor, npcs will have the same data that I typed out above. NOTE: Npcs will not have any of the other data that LocalPlayer has, just what was typed out.
    So, as expected, we must declare variables:
    Simba Code:
    var
      AllNpcs: TReflectNpcArray;
      OurNpc: TReflectNpc;

    AllNpcs.GetAll;//Loads all Npcs into 'AllNpcs'
    AllNpcs.Get('Guard');// loads all Npcs with name 'Guard into 'AllNpcs'

    OurNpc.Find('Guard');//Loads the closest Npc with Name 'Guard' and returns true if found

    You must always remember to call whichever one of these functions you are using, before each and every time you grab data from them, or it will be outdated. For example, here is a simple combat Script.
    Simba Code:
    program FightTest;
      {$DEFINE SMART}
      {$i AeroLib/AeroLib.Simba}
      {$i AeroLib/reflection/Reflection.simba}

    var
      MyPlayer: TReflectLocalPlayer;

    procedure FightGuard;
    var
      Guard: TReflectNpc;
      Point: TPoint;
    begin
      while not MyPlayer.InFight do
      begin
        if Guard.Find('Guard') then
        begin
          Point := Reflect.Tiles.TileToMs(Guard.GetTile);
          HumanMMouse(Point, 2, 2);
          FastClick(MOUSE_LEFT);
          While MyPlayer.IsMoving do
            Wait(50);
        end;
      end;
    end;  

    begin
      InitAL;
      SetupReflection;
      LogInPlayer(False);
      MyPlayer.Create;
      repeat
      if MyPlayer.GetHealth < 20 then
        //Eat
      else
        FightGuard;
      until(False);
    end;

    As you can see, we load the 'Guard' data immediately before we doing anything with it, making sure that the data is always as current as we can possibly do! Mess around with the other fields that TReflectNpc has, there is some quite useful stuff in it.

    Players

    Players refer to any non Npc's including our local player. Currently I only added one function that returns an array of all the players around us. As I haven't scripted anything where I would need this, maybe some other people could chime in and tell me what type of functions in player.simba you would like to see!
    Simba Code:
    var
      Players: TReflectPlayerArray;

    begin
      Players.GetAll;
    end;

    Objects

    In Rs there are 4 types of in game objects, Interactable(This Include calls Game), Wall Decoration, Floor Decoration, and Boundary.
    Game Objects are any mainscreen object that has the option to examine. These are by far the most important objects from a scripter's point of view. Wall Decoration are like pictures, wreaths, candles, ect that are on walls. Floor Decoration are the same as Wall Decoration, just on the floor. Boundary is exactly what it sounds like, anything that forms a boundary in RS, Walls, fences, ect..
    Game Objects can be searched by name or ID's, the others must only be searched by ID.

    The functions for Objects are set up in the same basic form as Npcs, such that:
    Simba Code:
    var
      AllObjects: TReflectObjectArray;
      OurObject: TReflectObject;

    begin
      AllObjects.GetAll(objGame, 20); //Loads all game objects within 20 tiles
      AllObjects.Get(objGame, 'Tree', 20);// Loads all 'Tree's within 20 tiles
      OurObject.Find(objGame, 'Tree', 20);//Loads nearest 'Tree' and returns true if found within 20 tiles
    end.

    Another point I would like to clarify is that the Object Types are no longer constants, but an Enum, so you must use one of the following in your functions:
    Simba Code:
    type
      TObjType = (objNull, objGame, objWallDecoration, objFloorDecoration, objBoundary);
    objNull is used internally and will not return any object at all.

    Items

    This is the final topic to be covered in the 'Non Static functions and it shouldn't be any easier/harder than Npcs/Objects!
    There are 3 different types of objects in the include, Ground, Inventory, and bank. All of those extend from the main Item.Simba and contain the same Fields. I will add some of these items and other stuff to my fighting exampleand that should help show more of the includes features as well!
    Simba Code:
    program FightTest;
      {$DEFINE SMART}
      {$i AeroLib/AeroLib.Simba}
      {$i AeroLib/reflection/Reflection.simba}

    var
      MyPlayer: TReflectLocalPlayer;

    procedure FightGuard;
    var
      Guard: TReflectNpc;
      Point: TPoint;
    begin
      while not MyPlayer.InFight do
      begin
        if Guard.Find('Guard') then
        begin
          Point := Reflect.Tiles.TileToMs(Guard.GetTile);
          HumanMMouse(Point, 2, 2);
          FastClick(MOUSE_LEFT);
          while MyPlayer.IsMoving do
            Wait(50);
        end;
      end;
    end;

    procedure EatFood;
    var
     Lobster: TReflectInvItem;
    begin
      Lobster.Find('Lobster');
      Lobster.Click(MOUSE_LEFT);
    end;

    procedure LootItems;
    var
      TheItems: TReflectGroundItemArray;
      I: Integer;
      Point: TPoint;
    begin
      TheItems.GetAll(20);
      for I := 0 to High(TheItems) do
      begin
        if TheItems[I].HighAlchValue > 10000 then
        begin
          Point := Reflect.Tiles.TileToMS(TheItems[I].GetTile);
          HumanMMouse(Point, 2, 2);
          FastClick(MOUSE_LEFT);
          while MyPlayer.IsMoving do
            Wait(50);
        end;
      end;
    end;

    begin
      InitAL;
      SetupReflection;
      LoginPlayer(False);
      MyPlayer.Create;
      repeat
      if MyPlayer.GetHealth < 20 then
        EatFood;
      else
      begin
        FightGuard;
        LootItems;
      end;
      until(false);
    end.

    Like I said, since I haven't added locations to widgets yet, I didn't include an example of any of the bank items as it is quite limited atm.




    Closing

    If you have made it this far, you should have a good understanding of how to script using the include, and also may have hopefully learned a little bit about the RS client and it's hierarchy. I will be constantly changing this tutorial as more features are added to the include, namely, locations to widgets. Also note, while the example scripts I posted do compile(At least they should!) I didn't put much thought into how they actually function, for example, the script will constantly be trying to loot. I just wanted to write it in the way that made the most sense!

    I hope you guys learned some stuff from this tutorial, and if you see any typo's or anything that isn't clear, or just need help on something, please don't hesitate to post!
    Last edited by Kyle; 12-01-2015 at 03:20 AM.
    “The long-lived and those who will die soonest lose the same thing. The present is all that they can give up, since that is all you have, and what you do not have, you cannot lose.” - Marcus Aurelius

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    gj, been waiting for reflection lape

  3. #3
    Join Date
    Feb 2013
    Location
    Rimmington
    Posts
    319
    Mentioned
    33 Post(s)
    Quoted
    183 Post(s)

    Default






  4. #4
    Join Date
    Aug 2007
    Posts
    539
    Mentioned
    20 Post(s)
    Quoted
    266 Post(s)

    Default

    Nice! Time to go update my scripts/threads!

  5. #5
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

  6. #6
    Join Date
    Oct 2006
    Posts
    6,752
    Mentioned
    95 Post(s)
    Quoted
    532 Post(s)

    Default

    Quote Originally Posted by The Mayor View Post
    Very nice tutorial elfyyy. I learnt a thing or two about reflection
    Thanks! Just wait for the next tutorial, that's where the real reflection fun will come in!
    “The long-lived and those who will die soonest lose the same thing. The present is all that they can give up, since that is all you have, and what you do not have, you cannot lose.” - Marcus Aurelius

  7. #7
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by The Mayor View Post
    Very nice tutorial elfyyy. I learnt a thing or two about reflection
    There is no such thing as the word 'learnt' mr English teacher haha unless austrelian English is different like that

  8. #8
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    Quote Originally Posted by Robert View Post
    There is no such thing as the word 'learnt' mr English teacher haha unless austrelian English is different like that
    I guess you haven't learnt that learnt is a word

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

    Default

    Quote Originally Posted by Robert View Post
    There is no such thing as the word 'learnt' mr English teacher haha unless austrelian English is different like that
    Sure there is http://www.oxforddictionaries.com/wo...rnt-vs-learned
    Last edited by slacky; 01-10-2015 at 02:32 AM.
    !No priv. messages please

  10. #10
    Join Date
    Oct 2006
    Posts
    6,752
    Mentioned
    95 Post(s)
    Quoted
    532 Post(s)

    Default

    Updated to include the new bank and chat functions.
    “The long-lived and those who will die soonest lose the same thing. The present is all that they can give up, since that is all you have, and what you do not have, you cannot lose.” - Marcus Aurelius

  11. #11
    Join Date
    Mar 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    The sample code in the thread is incorrect. It seems that some of the functions have been renamed. Also the code is using the official reflection branch now I presume?

  12. #12
    Join Date
    Feb 2015
    Posts
    48
    Mentioned
    0 Post(s)
    Quoted
    12 Post(s)

    Default

    Hey guys, I'm trying to program a simple reflection script with the AeroLib include. I've just grabbed your Guard Fighter sample.
    Now every time i try to run the script, it will tell me it couldn't find the following include:
    {$i AeroLib/reflection/Reflection.simba}

    I've downloaded the most recent AeroLib from here: villavu.com/forum/showthread.php?t=108953

    The Debugging Console also tells me there's an unknown declaration of "SetupReflection".

    I've tried to change the include to {$i Reflection/Reflection.simba} but without any luck.

    Am i doing something wrong? Is there a more recent thread? Is there another way?

    Thank you for your help in advance

  13. #13
    Join Date
    Jan 2013
    Posts
    68
    Mentioned
    0 Post(s)
    Quoted
    25 Post(s)

    Default

    Quote Originally Posted by symbiosis View Post
    Hey guys, I'm trying to program a simple reflection script with the AeroLib include. I've just grabbed your Guard Fighter sample.
    Now every time i try to run the script, it will tell me it couldn't find the following include:
    {$i AeroLib/reflection/Reflection.simba}

    I've downloaded the most recent AeroLib from here: villavu.com/forum/showthread.php?t=108953

    The Debugging Console also tells me there's an unknown declaration of "SetupReflection".

    I've tried to change the include to {$i Reflection/Reflection.simba} but without any luck.

    Am i doing something wrong? Is there a more recent thread? Is there another way?

    Thank you for your help in advance
    Simba Code:
    Reflect.Setup;
    declare that at the bottom and should get you back on track. Here is the lape thread https://villavu.com/forum/showthread.php?t=111662
    Last edited by Zace; 04-08-2015 at 01:22 AM. Reason: Link

  14. #14
    Join Date
    Feb 2013
    Posts
    342
    Mentioned
    8 Post(s)
    Quoted
    110 Post(s)

    Default

    I have recently moved from RS3 to OS and was considering learning to script for this also, seems reflection is the way forward so I shall be reading this as well as anything else I can find to help me along.
    Thanks!

  15. #15
    Join Date
    Nov 2015
    Posts
    43
    Mentioned
    1 Post(s)
    Quoted
    21 Post(s)

    Default

    Hello everyone, Thanks for the great addition to oldschool section, I was just wondering how I would go about using reflection to drop a specific item in the inventory?

    How would I go about collecting the item number? to enter into item.Find(XXX)
    Last edited by Tog; 11-27-2015 at 07:49 AM.

  16. #16
    Join Date
    Sep 2015
    Posts
    32
    Mentioned
    1 Post(s)
    Quoted
    4 Post(s)

    Default

    can i use "function GetTileHeight(Tile: TTile);" to make TileToMS more accurate and if so how?

  17. #17
    Join Date
    Sep 2014
    Location
    C:\Simba\
    Posts
    565
    Mentioned
    9 Post(s)
    Quoted
    71 Post(s)

    Default

    It already calls that inside TileToMS (or atleast some function inside that does that). If you are running to issues with it being inaccurate make sure that your zoom is set to default (not fully zoomed out) and that you are passing the correct tile.
    Feel free to ask me any questions, I will do my best to answer them!

    Previously known as YouPee.

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
  •