Results 1 to 5 of 5

Thread: Index out of range

  1. #1
    Join Date
    Jan 2012
    Posts
    85
    Mentioned
    0 Post(s)
    Quoted
    40 Post(s)

    Default Index out of range

    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.

    Code:
    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
            if opts[0].str.Endswith(lootItems[l]) then
            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.

    Code:
    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?

  2. #2
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

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

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

    It should be:
    Simba Code:
    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).
    Last edited by Dan the man; 09-22-2019 at 11:51 PM.

  3. #3
    Join Date
    Jan 2012
    Posts
    85
    Mentioned
    0 Post(s)
    Quoted
    40 Post(s)

    Default

    Quote Originally Posted by Dan the man View Post
    Your issue is that you are setting the high value based on the length of the array.

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

    It should be:
    Simba Code:
    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.

  4. #4
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    Quote Originally Posted by hackingislol133 View Post
    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.
    Simba Code:
    for i := 0 to High(opts) do
      for j := 0 to High(LootItems) do
        if opts[i].str.Endswith(lootItems[j]) then
          Inc(DropCount);

  5. #5
    Join Date
    Jan 2012
    Posts
    85
    Mentioned
    0 Post(s)
    Quoted
    40 Post(s)

    Default

    Quote Originally Posted by Citrus View Post
    You aren't iterating through all the options. You have opts[0], which is only the first option.
    Simba Code:
    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!

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
  •