Records tutorial, the power of object oriented programming.

In this tutorial we are going to go more in depth into records.

I made a small introduction on them in the syntax tutorial but that was just scratching the surface.

A cool thing about record for example is that they can inherit from each others.

I'm going to give you actual examples from my WaspLib. In the basescript.simba file of WaspLib you have TBaseScript, TBaseWalkerScript and TBaseBankScript:

  TBaseScript = record
    Name: String;
    Version: String;

    IsSetup: Boolean;

    TimeRunning: TStopwatch;
    StatsDebugTick: Int64;

    Action: String;
    ExtraInfo: String;

    ActionProfit: Int32;
    TotalActions: Int32;
    TotalProfit: Int32;

  TBaseWalkerScript = record(TBaseScript) //inherits all variables, functions and procedures of TBaseScript.
    RSW: TRSWalker;

  TBaseBankScript = record(TBaseWalkerScript) //inherits all variables, functions and procedures of TBaseWalkerScript.
    ScriptBank: PRSObject;    //Current bank for the script.
    BankTab: Int32;           //BankTab cache.

So you have TBaseScript which is the base record I use for all my scripts and it contains all the base variables my scripts use.

Then you haveTBaseWalkerScript which is used by all my scripts that use RSWalker. It inherits all the variables TBaseScript had.

Then there's TBaseBankScript which inherits from TBaseWalkerScript so it will have all of it's variables, procedures, functions and also all of TBaseScript.

So when I'm making a new script first I decide which kind of script it will be, if it's a script that uses the bank, lets say a woodcutting script I do:

  TWoodcutter = record(TBaseBankScript)
  //all variables of  TBaseBankScript are already part of this. now we add extra stuff we need.
  LogType: TRSItem;
  AxeType: TRSItem;

If the script just needs walker I use TBaseWalkerScript instead and if it doesn't need walking I use TBaseScript.

As I said previously, records that inherit from another also inherit their procedures and functions.

In TBaseScript I have this procedure:

procedure TBaseScript.Init; //override me to add more stuff.
  if IsSetup then Exit;
  IsSetup := True;

  if RemoteInputEnabled then RSClient.RemoteInput.Setup();

  //override me to add more stuff.

This is the base init function for all my scripts. But TBaseBankScript need some extra things set up on it's init function along with everything TBaseScript.Init already did.

So what do we do? We override it and inherit from the previous function like so:

procedure TBaseBankScript.Init; override;//override me to add more stuff.

  BankTab := -1;
  //override me to add more stuff.

And when I'm making a script, like the woodcutter example I gave above, if I need to setup extra things for that specific script I do the same:

procedure TWoodcutter.Init; override;

  LogType := 'Willow logs';
  AxeType := 'Rune axe'; 

And that's it for this tutorial 😄