Overriding

Short Simba tutorial about overloads and overrides.

In this tutorial I'm going to teach you guys how to override and overload functions.

There are times you might want to modify a Simba/SRL/WaspLib/Plugin function because it doesn't quite do what you want to. If it's an SRL or WaspLib function you could just open the file and modify it, however this approach has some downsides. If you modify it, any script that uses that function will be affected and that might not be what you want. But worst of all, whenever you update the libraries, unless you are using git all your work will be lost unless you properly back it up.

If you want to modify lot's of things and even develop new stuff in the libraries, you should use git. If you already know it, great, if not, you should learn otherwise you will have a bad time.

Anyway, let's say we would like to modify SRL's Inventory.ClickItem to have a wait after it clicks an item.

And we want it to be only in the script we made in the previous tutorial:

{$DEFINE SRL_USE_REMOTEINPUT}
{$I SRL/osr.simba}

var
  Item: TRSItem := 'Tuna';
  RSW: TRSWalker;
  GETile: TPoint := [4477, 2491];

procedure Init;
begin
  SRL.Setup;
  RSW.Setup('world');

  Login.AddPlayer('username', 'password', 'bankpin');
  if not RSClient.IsLoggedIn then
    Login.LoginPlayer;
end;

function AtTile(Tile: TPoint; Distance: Int32 = 15): Boolean;
begin
  Result := RSW.GetMyPos.DistanceTo(Tile)  0 then
  begin
    Slot := Slots[0];
    
    Result := True;
  end;
end;

Anyway you have the original function: TRSInventory.FindItem(Item: TRSItem; out Slots: TIntegerArray): Boolean and then a second one that is overloading the original one to use a different set of parameters. Instead of taking TIntegerArray, you can just pass it an Int32: TRSInventory.FindItem(Item: TRSItem; out Slot: Int32): Boolean

But I found it really annoying you had to constantly be passing it an extra parameter when sometimes I just wanted to find an item, get a True or False result and didn't care about which slot the item was in. So I overloaded the function:

function TRSInventory.FindItem(Item: TRSItem): Boolean; overload;
var
  Slots: TIntegerArray;
begin
  Result := Self.FindItems([Item], Slots); //we call the original function and pass it the TIntegerArray parameter it requires, but then don't use them. We just get the True or False result I wanted.
end;

And that's it, there's not much more to it, so I'm ending this tutorial here. I hope you understood it.

Maybe try it a bit in your scripts to get a feel of how it works.