Simba Code:
Function GetClosestObject(ID : Integer, ObjType : Integer): TRSObject;
var
ObjArra, SecObjArray : TRSObjectArray;
Obj : TRSObject;
PlayerPos : Tpoint;
I, Dist, LowestDist, ClosestObj: Integer
begin
ObjArray := GetAllObjects(ObjType);
For I := 0 to high(ObjArray) do
If (ObjArray[i].ID = ID) Then
SecObjArray[high(SecObjArray)] := ObjArray[i];
PlayerPos := GetMyPos;
LowestDist := 1000; // Set it high, just because haha.
For I := 0 to high(SecObjArray) Do
Begin
Dist := Distance(PlayerPos.x, PlayerPos.y, SecObjArray[i].Tile.x, SecObjArray[i].Tile.y);
If (Dist < LowestDist) then
Begin
LowestDist := Dist;
ClosestObj := i;
End;
End;
Result := SecObjArray[i];
End;
Here's the backbone to the function, keep in mind I wrote it in about 15 minutes. Also it has never been tested so it may fail miserably.