KeepBotting
2014-07-13 22:42:50
procedure TRSChooseOption.delete(var arr:TOptionArray; idx:integer);
{
Deletes the specified TOption from the specified TOptionArray.
}
var
i, j:integer;
begin
if not self.isOpen() then
exit;
if (idx = high(arr)) then
begin
setLength(arr, high(arr));
end;
if (idx >= 0) and (idx < high(arr)) then
begin
for i := idx to high(arr) - 1 do
begin
arr[i] := arr[i + 1];
end;
setLength(arr, i);
end;
writeDebug('delete(): deleted option ' + toStr(idx));
end;function TRSChooseOption.filter(txt:TStringArray):TOptionArray;
{
Deletes TOptions from the TOptionArray that do not match the TStringArray.
}
var
theOptions:TOptionArray;
i, j:integer;
begin
if not self.isOpen() then
exit;
theOptions := chooseOption.getOptions();
if (length(theOptions) < 1) then
exit;
for i := 0 to high(theOptions) do
for j := 0 to high(txt) do
if not (pos(txt[j], theOptions[i].str) > 0) then //access violation
begin
self.delete(theOptions, i);
end;
result := theOptions;
writeDebug('filter(): ' + toStr(result));
end; These are two functions I've written for filtering options. They work fine except for a weird exception.
Debug output:
[DEBUG] : delete(): deleted option 1
[DEBUG] : delete(): deleted option 2
[DEBUG] : delete(): deleted option 3
[DEBUG] : filter(): [{BOUNDS = {X1 = 261, Y1 = 139, X2 = 439, Y2 = 152}, STR = Attack Ankou}, {BOUNDS = {X1 = 261, Y1 = 171, X2 = 439, Y2 = 184}, STR = Examine Ankou}]

As you can see, it claims to have deleted options 1, 2, and 3 from the above. Naturally, option 0 (Attack Ankou) should remain.
However when I debug the TOptionArray, it results a second option (Examine Ankou, element 2 in the TOptionArray) as well.
This always happens, even when filtering in such a way that every TOption should be deleted–one TOption remains.
In addition to this, the script will randomly throw access violations at the indicated line (as seen below). Not a clue why.
[DEBUG] : filter(): [{BOUNDS = {X1 = 232, Y1 = 231, X2 = 410, Y2 = 244}, STR = Walk here}, {BOUNDS = {X1 = 232, Y1 = 263, X2 = 410, Y2 = 276}, STR = Cancel}]
[DEBUG] : delete(): deleted option 0
[DEBUG] : delete(): deleted option 1
[DEBUG] : delete(): deleted option 2
Exception in Script: Runtime error: "Access violation" at line 776, column 16
What's going on?
Edit: I suspect it may have something to do with the way I reset the length of my array in .delete(), here's a more verbose debug output of the filter:
[DEBUG] : delete(): deleted option Walk here at index 0
[DEBUG] : delete(): deleted option Cancel at index 1
Exception in Script: Runtime error: "Access violation" at line 776, column 12
Obviously, Walk here is not at index 0, nor is Cancel at index 1–however I don't know what I've done wrong that would cause this.