SRL Archive - OSR Help

type mismatch


ActualNoob

2013-05-31 10:43:22

Hey all iam a newbie to scripting and simba, hopefully someone can help me.
Iam getting this error when trying to compile. "[Error] Type mismatch at line 89" this is highlighted at setTiles(map);

my question is why is this happening? arent both the procedure(setTiles) parameter list and the variable "map" of the same type(Array of Array of Node)?

What this script is supposed to do:
  • Reads x,y coordinates from a file.
  • Extracts the coordinates into pieces(TStringList).
  • sets map[x][y].status to 1 if that coordinate exist in the file. (status is a integer variable of the Node record.)

the text.txt is in the format of:

129, 51
512, 61
0, 51
52, 52


map is defined as a variable
var
map : Array[0..512] of Array[0..512] of Node;

my main method contains
 setTiles(map);    

and the procedure contains
 procedure setTiles(var grid : array of array of Node);
var
a : String;
i,j,k,index: Integer;
file : Integer;
pieces : TStringList;
begin
pieces := TStringList.Create;



file := OpenFile('C:\Users\pokemon\Desktop\bot\text.txt',true);
ReadFileString(file,a,FileSize(file));

SplitRegExpr('[^[0-9]*\s', a,pieces);
j := 1;
k := 0;
index :=0;

//sets nodes for coordinates (x,y) to walkable
for i:=0 to (pieces.Count/2)-1 do
begin
grid[StrToInt(pieces.Strings[k])][StrToInt(pieces.Strings[j])].status := 1 ;
index := index +1;
k := k +2;
j := j +2;
end;


pieces.Free;
end;

gerauchert

2013-05-31 11:08:39

Not sure about this but perhaps try it without the map var predetermined length? And set length upon function call. Don't have access to simba atm so I would suggest playing around with it or try a new method until u get it doing what u need it to do

Le Jingle

2013-05-31 11:14:12

First off, file is a reserved keyword, so avoid using that as a variable name. Also, I see you are using a TStringList type, and would like to note that you should make sure to cast it back to an actual string type (granted that's what you want to do with it) before trying to manipulate it to an integer. Alsooo, make sure you make a sturdy regex pattern that doesn't miss/mess up/mis-use the string(s).

This seems relative, another user, Brandon, made a String to TPA function that seems like the same idea you have here. So a quick search might help too for your project.

Cheers,
Lj

ActualNoob

2013-05-31 11:46:01

@gerauchert : It seems you were right… the dimensions were different and so it flagged it as a mismatch in type. Thanks for helping.

@le Jingle : Thank you for the tips. to be honest i dont know much about these scripting concepts. I should take the time to go and learn them. Off to read some tutorials.:spot:

I tried to do make it as simple as possible. It seems to work.

program new;
var
grid : Integer;
intarray : Array of Integer;
intarrayarray : Array of Array of Integer;

procedure Test(var poo : Integer);

begin
poo :=5;
end;

procedure Foo(var x: Array of Integer);
var
i : Integer;
begin
for i:=0 to sizeOf(x) do
begin
WriteLn(x[i]);
end;
end;

procedure Bar(var y: Array of Array of Integer);
var
i,j : Integer;

begin

for i := 0 to High(y) do
begin
for j := 0 to High(y[i]) do
begin
y[i][j] := j;
WriteLn('y['+IntToStr(i)+','+IntToStr(j)+'] = '+
IntToStr(y[i][j]));
end;
end;
end;


begin

Test(grid);
WriteLn(grid);

setLength(intarray,5);
intarray := [4,1,2,6,2];
Foo(intarray);

SetArrayLength(intarrayarray,3);
SetLength(intarrayarray, 3);
SetLength(intarrayarray[0], 1);
SetLength(intarrayarray[1], 2);
SetLength(intarrayarray[2], 3);
Bar(intarrayarray);


end.

Janilabo

2013-05-31 12:01:31

From: ActualNoob
@gerauchert : It seems you were right… the dimensions were different and so it flagged it as a mismatch in type. Thanks for helping.

@le Jingle : Thank you for the tips. to be honest i dont know much about these scripting concepts. I should take the time to go and learn them. Off to read some tutorials.:spot:

I tried to do make it as simple as possible. It seems to work.

program new;
var
grid : Integer;
intarray : Array of Integer;
intarrayarray : Array of Array of Integer;

procedure Test(var poo : Integer);

begin
poo :=5;
end;

procedure Foo(var x: Array of Integer);
var
i : Integer;
begin
for i:=0 to sizeOf(x) do
begin
WriteLn(x[i]);
end;
end;

procedure Bar(var y: Array of Array of Integer);
var
i,j : Integer;

begin

for i := 0 to High(y) do
begin
for j := 0 to High(y[i]) do
begin
y[i][j] := j;
WriteLn('y['+IntToStr(i)+','+IntToStr(j)+'] = '+
IntToStr(y[i][j]));
end;
end;
end;


begin

Test(grid);
WriteLn(grid);

setLength(intarray,5);
intarray := [4,1,2,6,2];
Foo(intarray);

SetArrayLength(intarrayarray,3);
SetLength(intarrayarray, 3);
SetLength(intarrayarray[0], 1);
SetLength(intarrayarray[1], 2);
SetLength(intarrayarray[2], 3);
Bar(intarrayarray);


end.
Hey,

If you PM me your script, I can help you get it load the grid correctly for you. :)

-Jani

ActualNoob

2013-05-31 12:07:10

From: Janilabo
Hey,

If you PM me your script, I can help you get it load the grid correctly for you. :)

-Jani



Thanks for the offer, but i have managed to get it working.

I did not define an array type.


TNode = Array of Node;
TNodeArray = Array of TNode;

thanks for the quick and helpful responses.

Janilabo

2013-05-31 12:21:59

From: ActualNoob
Thanks for the offer, but i have managed to get it working.

I did not define an array type.

TNode = Array of Node;
TNodeArray = Array of TNode;

thanks for the quick and helpful responses.
No problem, just glad to hear you got it working!

ActualNoob

2013-05-31 15:12:32

I was being stupid for not accepting your help Janilabo … i cannot seem to pm you as i dont yet have 10 posts on this forum. Is there another way to contact you? :D

Janilabo

2013-05-31 16:04:48

From: ActualNoob
I was being stupid for not accepting your help Janilabo … i cannot seem to pm you as i dont yet have 10 posts on this forum. Is there another way to contact you? :D
You can contact me either via Skype: "Janilabo" (without "s) or using email: janilabo[at]gmail[dot]com. :)

-Jani

Enslaved

2013-05-31 16:53:10

nice to see someone using multi-dimensional arrays :)

ActualNoob

2013-06-01 18:42:36

I tried making three helper functions that will operate on a type/record ive created. Iam currently using them on integers to test right now. Please help me to see if theres a problem with the code or if theres a better way of doing things.

  • Delete an element of an array
  • Sort an array
  • Insert an element into a sorted array


program new;
var
a: array of Integer;
x : integer;

procedure Sort( var node: Array of integer);
var
i,j,k : Integer;
currentele : Integer;
begin
for i:=1 to High(node) do
begin

currentele := node[i];

for k:= i-1 downto 0 do
begin
if((k>=0) and (node[k] > currentele)) then
begin
node[k+1] := node[k];

end else break;
node[k] := currentele;
end;
end;
end;

procedure DeleteElement(var node: Array of integer ; index : Integer);
var
i,j : Integer;
begin
if (index = High(node)) then
begin
setLength(node,High(node));
end;

if((index>=0) and (index< High(node))) then
begin
for i:= index to High(node)-1 do
begin

node[i] := node[i+1];
end;
SetLength(node,i);
end;
end;

procedure AddElement(var node: Array of integer; element : Integer);
var
i,j, index: Integer;
begin
for i:= 0 to High(node) do
begin
if( element >= node[i]) then
begin
index:=i;
end;
end;
setLength(node, High(node)+2);
//shift right
for j:=High(node) downto index do
begin
if(j>=0) then
node[j] := node[j-1] ;
end;

node[index] := element;
end;

begin
SetLength(a,5);
a[0]:=6;
a[1]:=3;
a[2]:=9;
a[3]:=2;
a[4]:=1;

DeleteElement(a,0);
Sort(a);
AddElement(a,2);
DeleteElement(a,4); //delete last element in array

for x:=0 to High(a) do
begin
WriteLn(a[x]);
end;


end.


Le Jingle

2013-06-01 21:43:57

Do you have a compareTo function for your custom record? If so, then what you're trying to accomplish becomes easier to understand and accomplish (in my opinion).

Do note, there is already a sort for Pascalscript within the simba build (sorts an integer array). I'd suggest looking at a thread @Janilabo made rather recently , targeted towards sorting different types. (follow username + view recently started threads on profile page)

Cheers,
Lj

ActualNoob

2013-06-02 06:12:27

From: Le Jingle
Do you have a compareTo function for your custom record? If so, then what you're trying to accomplish becomes easier to understand and accomplish (in my opinion).

Do note, there is already a sort for Pascalscript within the simba build (sorts an integer array). I'd suggest looking at a thread @Janilabo made rather recently , targeted towards sorting different types. (follow username + view recently started threads on profile page)

Cheers,
Lj


Thanks for replying Lj. I dont have a compareTo function what should it do?
Iam trying to implement a a* pathfinding algorithm and iam using this and this tutorial.

So far i have been able to:
Obtain all walkable nodes of the map(my first post).
Obtain all walkable neighbors of a node that are within the map boundaries.

The part iam stuck on now is creating the open and closed list which stores nodes which must be sorted in order of lowest F Cost to highest. I must be able to add a node to the open list and delete them too. I believe that the open list must be sorted at all times and any element added must be added in the correct place(not appended to the end)

my node class looks like

Type Node = record
coordinate : TPoint;
gScore : Integer;
hScore : Integer;
ShortestPath : TPointArray;
fScore : Integer;
status : Integer; // 0(unwalkable,default),1(walkable) defaults to 0, 2(starting node), 3(destination)
parent_x,parent_y : Integer; //save index of parent node
neighbors : TPointArray;
inClosedList : boolean;
inOpenList : boolean;

end;

TNode = Array of Node;
TNodeArray = Array of TNode;

and my list class looks like

Type List = record
TAnode: TNode;
index_x,index_y : Integer;


end;

ActualNoob

2013-06-02 08:51:42

I managed to get the sorting procedure working in the actual program.

output:

Nodes in order of fScore:123,175: fscore =5
Nodes in order of fScore:134,154: fscore =2
after list
Nodes in order of fScore:134,154: fscore =2
Nodes in order of fScore:123,175: fscore =5

procedure

procedure Sort( var node: Array of Node);
var
i,j,k : Integer;
currentele : Node;
begin
for i:=1 to High(node) do
begin

currentele := node[i];

for k:= i-1 downto 0 do
begin
if((k>=0) and (node[k].fScore > currentele.fScore)) then
begin
node[k+1] := node[k];

end else break;
node[k] := currentele;
end;
end;
end;

I also fixed up a case where there was only 1 element in the array it gave a out of range error when trying to add an element. Can anyone else find any holes or fix things up so its more tight?
please try run the code below in simba and test some cases.

program new;
var
a: array of Integer;
x : integer;

procedure Sort( var node: Array of integer);
var
i,j,k : Integer;
currentele : Integer;
begin
for i:=1 to High(node) do
begin

currentele := node[i];

for k:= i-1 downto 0 do
begin
if((k>=0) and (node[k] > currentele)) then
begin
node[k+1] := node[k];

end else break;
node[k] := currentele;
end;
end;
end;

procedure DeleteElement(var node: Array of integer ; index : Integer);
var
i,j : Integer;
begin
if (index = High(node)) then
begin
setLength(node,High(node));
end;

if((index>=0) and (index< High(node))) then
begin
for i:= index to High(node)-1 do
begin

node[i] := node[i+1];
end;
SetLength(node,i);
end;
end;

procedure AddElement(var node: Array of integer; element : Integer);
var
i,j, index: Integer;
begin
for i:= 0 to High(node) do
begin
if( element >= node[i]) then
begin
index:=i;
end;
end;
setLength(node, High(node)+2);
//shift right
for j:=High(node) downto index do
begin
if(j>0) then
node[j] := node[j-1] ;
end;

node[index] := element;
end;

begin
SetLength(a,1);
a[0]:=6;



AddElement(a,1);


for x:=0 to High(a) do
begin
WriteLn(a[x]);
end;


end.

@Le Jingle: I looked at Janilabo's sorting functions but i couldnt figure out how to sort a array depending on the value of its variables.
Also i have two questiosn for you. How do you delete a element of an array? is there a function that does this in simba?

ActualNoob

2013-06-02 20:57:03

thanks for helping guys. I have managed to implement the path finding algorithm. Its a little slow (took 1-1.5 minutes to find it. lol), but it works. Now my question is, is it possible to make a form which has a inputbox and i can enter coordinates of where i want the start position and destination position to be? or better still, to able to click on a picture of the map and it will get the location from it.

In the end i used a compareTo function i defined. I didnt use the sort function and left the list unsorted, also i made the addElement procedure just append to the end.

Also what ways do runescape scripters use to find out if a another player exist or if he is attacking a monster?


Enslaved

2013-06-05 09:20:12

with simba it is kind of harder to do this as it does not tell you the current status of the monsters like in most java bots. but one effective method is trying to locate/ find if the health bar is within a certain number of pixels above the monster