SRL Archive - Help and Support

I'm confused on ranges again


Le Jingle

2012-10-22 20:03:11

I'm confused why I can't get this snippet to work?

I'm probably overlooking something very simple :/ Help appreciated;


program new;
{$i srl/srl.simba}

function ReturnInvExemptSlots: TIntegerArray;
var i: integer;
var RuneDTM: integer;
var TPA1: TPointArray;
var ResArr: TIntegerArray;
begin
RuneDTM := DTMFromString('m1gAAAHic42JgYHBmgmB/IDYFYksg9gZiOyB2B2I/IDYHYnFGBgYVRggtAcTyUKwExTJALA3ET27dYnBzdmZYu2wpg6ebG4OVuTlDY3U1g5O9PUNibCzDzEmTGLw9PBj+A+0mBjMSiREAAC3wGqc=');
TPA1 := ItemCoordinates('inv', 'dtm', RuneDTM, []);
writeln('Our TPA Length is: ' + tostr(length(TPA1)));
writeln('Our TPA Points are: ' + tostr(tpa1));

for i := 1 to Length(TPA1) do
begin
ResArr[i] := CoordsToItem(TPA1[i].x, TPA1[i].y);
CombineIntArrayWrap(result, ResArr, result);
end;

writeln('Our result is: ' + tostr(result));
freedtm(RuneDTM);
end;

begin
SetupSRL;
Writeln('the length of function is: ' + tostr(length(ReturnInvExemptSlots)));
end.

Cheers,
Lj

Nebula

2012-10-22 20:20:42

Dude… You do know you don't have to have var in there this many times right?


var i: integer;
var RuneDTM: integer;
var TPA1: TPointArray;
var ResArr: TIntegerArray;


Maybe instead of using ItemCoordinates, you can use FindDTMs?

Sin

2012-10-22 20:30:30

What does it return? 0?


From: Nebula
Dude… You do know you don't have to have var in there this many times right?


var i: integer;
var RuneDTM: integer;
var TPA1: TPointArray;
var ResArr: TIntegerArray;


Maybe instead of using ItemCoordinates, you can use FindDTMs?



'tis preference.

Brandon

2012-10-22 20:30:52

Also just want to point this out.


for i := 1 to Length(TPA1) do
begin
ResArr[i] := CoordsToItem(TPA1[i].x, TPA1[i].y);
CombineIntArrayWrap(result, ResArr, result);
end;


You don't want to do that for sure. You want to set the length of the result first then just index each point.. Why? Because if you don't know the way combine works, then you cannot be sure that it doesn't resize the array every single time. Ex:



L := Length(TPA1);
SetLength(Result, L);
for i := 1 to L do
Result[i] := CoordsToItem(TPA1[i].x, TPA1[i].y);

Then again, the suggestion of FindDTMs is better.

riwu

2012-10-22 20:42:41

You should start your for loop with 0 since the TPA1 array will begin with 0.
Also if you prefer to do slot by slot search just:
for i:=1 to 28 do
begin
iBox:=InvBox(i);
if FindDTM(blabla, iBox.X1, iBox.Y1, ...) then
begin
SetLength(Result, Length(Result) + 1);
Result(High(Result)):= i;
end;
end;
I think there may already be some SRL function that does exactly this though.

Le Jingle

2012-10-22 20:47:39

What I'm trying to do (which I forgot to point out before I ran to do an errand), is return slots in an Int Arr variable (ex. result of the snippet function); then these numbers will be passed on as a var in params to another function that will remove them from the DepositAllExecept Func (#Flight)..

So back to my problem, I seem to always go out of range, on the line right after my for loop. I think I might have made myself stuck, because even though I return several points from the TPA, I might be dealing with 0 index counting (needed for the TPA) and need 1 index counting for a proper inventory array?

Arg sorry, when it comes to length's/high, I get lost fast. :x


for i := 1 to L do
Result[i] := CoordsToItem(TPA1[i].x, TPA1[i].y); // out of range

Sin

2012-10-22 20:51:08

From: riwu
You should start your for loop with 0 since the TPA1 array will begin with 0.
Also if you prefer to do slot by slot search just:
for i:=1 to 28 do
begin
iBox:=InvBox(i);
if FindDTM(blabla, iBox.X1, iBox.Y1, ...) then
begin
SetLength(Result, Length(Result) + 1);
Result(High(Result)):= i;
end;
end;
I think there may already be some SRL function that does exactly this though.


This seems like the best way to go.

riwu

2012-10-22 20:58:01

From: Le Jingle
What I'm trying to do (which I forgot to point out before I ran to do an errand), is return slots in an Int Arr variable (ex. result of the snippet function); then these numbers will be passed on as a var in params to another function that will remove them from the DepositAllExecept Func (#Flight)..

So back to my problem, I seem to always go out of range, on the line right after my for loop. I think I might have made myself stuck, because even though I return several points from the TPA, I might be dealing with 0 index counting (needed for the TPA) and need 1 index counting for a proper inventory array?

Arg sorry, when it comes to length's/high, I get lost fast. :x


for i := 1 to L do
Result[i] := CoordsToItem(TPA1[i].x, TPA1[i].y); // out of range

Dont use Length for your i loop because TPA[Length(TPA)] is actually out of range as that's 1 index higher than its highest: TPA[High(TPA)].
Use
i:=0 to High(anyArray)
Similar problem here: http://villavu.com/forum/showthread.php?p=1115180

Le Jingle

2012-10-22 21:06:50

From: riwu
Dont use Length for your i loop because TPA[Length(TPA)] is actually out of range as that's 1 index higher than its highest: TPA[High(TPA)].
Use
i:=0 to High(anyArray)
Similar problem here: http://villavu.com/forum/showthread.php?p=1115180


Yes, that's what I've tried, however I think it's a matter of using the CoordsToItem, because in that function, you have to have started from 1-28 in order for invbox(result) to be called correctly? That's more of where I meant I get confused, in the process of somehow bypassing the start from 0 in the loop, while going through the loop only as many times as in the TPA's length, but accounting for the 0 index array start and the 1-28 needed for invbox

riwu

2012-10-22 21:19:04

From: Le Jingle
Yes, that's what I've tried, however I think it's a matter of using the CoordsToItem, because in that function, you have to have started from 1-28 in order for invbox(result) to be called correctly? That's more of where I meant I get confused, in the process of somehow bypassing the start from 0 in the loop, while going through the loop only as many times as in the TPA's length, but accounting for the 0 index array start and the 1-28 needed for invbox

You are now using an element in an array, so let's say the array looks like this:
[1,2,3,4…28].
What's in Array[1]?
and what's in Array[28]? (hint: this can't be answered)
Hope you get it ;)

Le Jingle

2012-10-22 21:28:48

From: riwu
You are now using an element in an array, so let's say the array looks like this:
[1,2,3,4…28].
What's in Array[1]?
and what's in Array[28]? (hint: this can't be answered)
Hope you get it ;)


Ah so that's why my TPA1.x's and .y's didn't auto appear after I typed the period after the end bracket? Is there any way I can utilize the element index?

Brandon

2012-10-22 21:35:41

From: Le Jingle
Ah so that's why my TPA1.x's and .y's didn't auto appear after I typed the period after the end bracket? Is there any way I can utilize the element index?



{*
Returns an array of inventory slots that contain the specified DTM.
*}
Function ItemSlots(DTM: Integer): TIntegerArray;
var
I, H: Integer;
TPA: TPointArray;
Begin
If FindDTMs(DTM, TPA, MIX1, MIY1, MIX2, MIY2) Then
Begin
H := High(TPA);
SetLength(Result, H + 1);
For I := 0 To H Do
Result[I] := CoordsToItem(TPA[I].X, TPA[I].Y);
End;
End;


Then you can invbox them as much as you want.. simply do:

TIntVar = ItemSlots(......);

MouseTBox(InvBox(TIntVar[0]), MOUSE_MOVE); //Index it as much as you want. Up to the bounds of the array.


//Ex:

For I := 0 To High(TIntVar) Do
MouseTBox(InvBox(TIntVar[I]), MOUSE_MOVE);

riwu

2012-10-22 21:41:17

From: Le Jingle
Ah so that's why my TPA1.x's and .y's didn't auto appear after I typed the period after the end bracket? Is there any way I can utilize the element index?

You lost me :S
Like this?

Maybe you don't understand my previous post:
The index of an array starts from 0, and the end of it is given by High(theArray).
The relationship between High and Length:
High(Array) = Length(Array) - 1

Hence when accessing an array with a for loop you should use
for i:=0 to High(Array) or more oddly, for i:=0 to (Length(array) - 1).
Using for i:=1 to Length(array) results in it skipping the Array[0], and going into Array[High(Array) + 1] which doesn't exist.
Other than that nothing wrong with your code at a glance. Thankfully Combine procedures in Simba does re-size the array for you. Though there's much cleaner codes suggested above.

Le Jingle

2012-10-22 22:13:58

Could be my simba, as I don't get this any which way I make a basic function:

But I do understand the high's and length's somewhat from Runaway's explanations on a previous thread quite some time ago. I do see how an array start's from 0 too, however I do appreciate the re-clarification, as I do feel weak on the subject itself (It's yet to 'click' and make it into 'I know it like the back of my hand' part of my brain :p ).

Thanks too Brandon for the snippet, that's what I was trying to achieve, as well as works. I also forgot/slipped my mind that FindDTMs returned a TPA and ya'll were suggesting to use that instead of item coordinates -.- My fault.

Thanks for the help though everyone,
much appreciated!

Cheers,
Lj