SRL Archive - OSR Help

Index out of range


hackingislol133

2019-09-22 18:59:08

I'm trying to write a looting function that right clicks on the loot pile, checks if the items in the pile matches the items declared in a string array, and then increments the number of times it has to loot by 1.


program new;


{$define SMART}
{$i srl/osr.simba}
{$i srl/shared.simba}
{$i RSWalker/Walker.simba}

const
lootItems = ['Bones', 'of Wine', 'platebody', 'Arrows', 'rune'];

var
i, l, dropCount: integer;
opts: TOptionArray;

begin
if ChooseOption.Open() then
begin
opts := chooseOption.getOptions();
dropCount := 0
for l := 0 to high(length(lootItems)) do //detect the number of loot items
begin
[COLOR="#FF0000"] if opts[0].str.Endswith(lootItems[l]) then
[/COLOR] begin
dropCount := dropCount + 1;
writeln(dropCount);
end
end
end
end;

When I try running the code with 3 valid items on the ground, it returns an index range error at the red highlighted line above.

Runtime error: "Index out of range (index:5, low:0, high:4)" at line 23, column 43 in file

I can't figure out why high is still returning 4 if it is checking for the length of the lootItem string array. I figured a quick fix would be to do high(length(lootItems)+1) but the high length is still 4. Is there something I'm missing here?

Dan the man

2019-09-22 19:49:49

Your issue is that you are setting the high value based on the length of the array.

for l := 0 to high(length(lootItems)) do     //detect the number of loot items

It should be:
for l := 0 to high(lootItems) do     //detect the number of loot items

It there are 5 options on the ground, the method that you did would set the 0-5 (length is 6 items, meaning that it is out of range).

hackingislol133

2019-09-22 21:57:42

From: Dan the man
Your issue is that you are setting the high value based on the length of the array.

for l := 0 to high(length(lootItems)) do     //detect the number of loot items

It should be:
for l := 0 to high(lootItems) do     //detect the number of loot items

It there are 5 options on the ground, the method that you did would set the 0-5 (length is 6 items, meaning that it is out of range).


With high(lootItems), the for loop won't iterate through the remaining loot choices. It just returns dropCount = 1 and terminates when there are multiple matching items in the context menu.

Edit: maybe I have to declare lootItems as an array of String and then pass it the string?
Edit2: nope, still doesn't work.

Citrus

2019-09-22 23:58:06

From: hackingislol133
With high(lootItems), the for loop won't iterate through the remaining loot choices. It just returns dropCount = 1 and terminates when there are multiple matching items in the context menu.

Edit: maybe I have to declare lootItems as an array of String and then pass it the string?
Edit2: nope, still doesn't work.


You aren't iterating through all the options. You have opts[0], which is only the first option.

for i := 0 to High(opts) do
for j := 0 to High(LootItems) do
if opts[i].str.Endswith(lootItems[j]) then
Inc(DropCount);

hackingislol133

2019-09-23 01:00:47

From: Citrus
You aren't iterating through all the options. You have opts[0], which is only the first option.

for i := 0 to High(opts) do
for j := 0 to High(LootItems) do
if opts[i].str.Endswith(lootItems[j]) then
Inc(DropCount);


Ahhhhhhhhhh there we go. For some reason I thought that getOptions was returning only 1 string instead of an array of strings.

Cheers!