Arrays

Simba Arrays, what are they and what can they be used for?

By Loading...

In this tutorial we are going to talk about arrays.

First of all, if you are new to programming in general you might not know what array are.

In simple terms, an array is a list of something. Can be a list of characters, numbers, strings, whatever.

In Simba you can make arrays of any type.

For a list of types you may check the FPC Docs.

But the most common ones you will be using in Simba are:

Integer
Int32
Int64
UInt32
UInt64
Double
Extended
String
Char
TPoint
TBox

Any record that is part of SRL or WaspLib or your script is also a type that can be used for an array.

To create an array you use the reserved words array of MyTypeHere. Like so:

var
  MyArray: array of Integer;

You can also create a type of that array:

type
  TIntegerArray: array of Integer;

var
  MyArray: TIntegerArray;

In this case, TIntegerArray is already part of SRL. So we can just include it and move on to the next part:

{$I SRL/osr.simba}

var
  MyArray: TIntegerArray;

Now let’s say you want to add the numbers from 0 to 5 to our array. There’s 2 ways we can do it.

The first one is:

{$I SRL/osr.simba}

var
  MyArray: TIntegerArray;

begin
  MyArray := [0, 1, 2, 3, 4, 5];
end.

And the second way is:

{$I SRL/osr.simba}

var
  MyArray: TIntegerArray;

begin
  MyArray += 0;
  MyArray += 1;
  MyArray += 2;
  MyArray += 3;
  MyArray += 4;
  MyArray += 5;
end.

Both of this methods will have the same result, keep in mind though, the second method you are appending numbers to the array, if the array wasn’t empty when you start you will have a different result.

To empty and array you can set it to be empty like so:

{$I SRL/osr.simba}

var
  MyArray: TIntegerArray;

begin
  MyArray := [0, 1, 2, 3, 4, 5];
  MyArray := [];
end.

Now… how do you use arrays for anything useful you ask? Well, you can element in an array has an index which you can use to retrieve the information stored. The syntax to get the element is MyArray[index]. Keep in mind indexes start at 0.

Let’s change to a different type of an array for this part to hopefully make more sense, TStringArray which is also a type included in SRL which is basically an array of String. We are going to setup and array and print the 3rd index of it.

{$I SRL/osr.simba}

var
  MyArray: TStringArray;

begin
  MyArray := ['first', 'second', 'third', 'fourth', 'fifth'];
  Writeln MyArray[3]; //this will print 'fourth'!
end.

You can also specify an index with a variable if you want and there are times you might not know how many elements an array has but want to use the last one and for that you can use High(MyArray):

{$I SRL/osr.simba}

var
  MyArray: TStringArray;
  i: Int32;

begin
  MyArray := ['first', 'second', 'third', 'fourth', 'fifth'];
  i := 1;

  Writeln MyArray[i]; //will print second
Writeln MyArray[High(MyArray)]; //will print fifth.
end.

And now with this information we can do things in loops! Let’s say you want to print every string in your array, you can easily do it like this:

{$I SRL/osr.simba}

var
  Arr: TStringArray;
  i: Int32;

begin
  Arr := ['first', 'second', 'third', 'fourth', 'fifth'];

  for  i := 0 to High(Arr) do
    Writeln Arr[i];
end.

Another useful thing you can do is to modify the elements of an array:

{$I SRL/osr.simba}

var
  Arr: TStringArray;

begin
  Arr := ['first', 'second', 'third', 'fourth', 'fifth'];
  Arr[3] := 'I was renamed';

  Writeln Arr[3];
end.

And of course you can do operations with array elements:

{$I SRL/osr.simba}

var
  StrArr: TStringArray;
  IntArr: TIntegerArray;

begin
  StrArr := ['first', 'second', 'third', 'fourth', 'fifth'];
  IntArr := [0, 1, 2, 3, 4];

  Writeln StrArr[2] + ' comes before ' + StrArr[3];

  IntArr[3] := IntArr[1] + IntArr[4] + IntArr[2];
  
  WriteLn IntArr[3];
end.

A common error I get pmed about is index out of range errors. This is exactly what it means, you are trying to access an index in an array that does not exist. This could be because the array is empty:

{$I SRL/osr.simba}

var
  Arr: TStringArray;

begin
  Arr := [];
  Writeln Arr[0];
end.

Or you are trying to access an index that is higher than the number of existing indexes:

{$I SRL/osr.simba}

var
  Arr: TStringArray;

begin
  Arr := ['first', 'second', 'third', 'fourth', 'fifth'];
  Writeln Arr[5];
end.

A common example that happens from time to time is when I upload a script that I was using one of my alts and I forget to reset Login.PlayerIndex to 0.

SRL uses an array of RSPlayers to handle logins. And what decides which RSPlayer we are going to use is Login.PlayerIndex. (If you don’t understand what I mean by RSPlayer an RSPlayer is a record with the login information, username, password, pin, etc.) When you have multiple accounts saved, it’s normal to have multiple RSPlayers and sometimes I accidentally upload a script with:

begin
  Login.PlayerIndex     := 5;
  AntibanEnabled        := True; //Enables Most Antiban
  BreaksEnabled         := True; //Enables Short Breaks
  SleepEnabled          := True; //Enables Sleep Breaks
  RemoteInputEnabled    := True; //Enables Remote Input
  ProgressReportEnabled := True; //Enables Graphical Progress Report
  ScriptDebugEnabled    := False; //Enables Script Debugging
end;

If you try to run this and don’t have 6 accounts saved, it’s going to give you an index out of range error.

To fix it simply change it to 0 or whatever else you want that applies to your case:

begin
  Login.PlayerIndex     := 0;
  AntibanEnabled        := True; //Enables Most Antiban
  BreaksEnabled         := True; //Enables Short Breaks
  SleepEnabled          := True; //Enables Sleep Breaks
  RemoteInputEnabled    := True; //Enables Remote Input
  ProgressReportEnabled := True; //Enables Graphical Progress Report
  ScriptDebugEnabled    := False; //Enables Script Debugging
end;

Lastly, some other common type of arrays you will probably be using while using Simba are:

TPointArray
TBoxArray

And they follow all the same rules as before.

Another thing that you should also be aware is that you can also have an array of an array, also known as a 2D Array.

An example of a 2D Array you will probably see often in Simba is what we call ATPAs which means Array of TPointArray.

This will be something covered in another tutorial, but they also follow the same rules as regular array and I thought you should be made aware of their existence.

I hope you find this useful, have fun with arrays!