PDA

View Full Version : Variable help



Too Soon
11-21-2013, 10:14 PM
Alright so I'm trying to re-mimic the procedure procedure GetMousePos(var x,y: integer);

An example of GetMousePos is

program getMousePos;

var x,y: integer;
begin
GetMousePos(x,y);
ClickMouse(x,y,true);
end.

It stores the mouse position in variable X and Y.
What do you do to store output on the input variable? When I try I get an Access Violation error.

riwu
11-21-2013, 10:42 PM
What do you mean by mimicking the procedure? overriding/overloading it?

Third argument of ClickMouse() should be an Integer (use MOUSE_LEFT/MOUSE_RIGHT constants if you are using ogl/srl include)

Too Soon
11-21-2013, 10:58 PM
I'm trying to make a procedure that will store output on the input variables like GetMousePos does.
ClickMouse(x,y,0); oops sorry.

bonsai
11-22-2013, 12:07 AM
That's what the 'var' is in the procedure/function line. var means changes will get changed in the caller variable.

No var means any changes inside the function do not get changed in the caller.

Neither one should cause an access violation. You would have to post some code for an explanation of the issue.

Too Soon
11-22-2013, 01:03 PM
That's what the 'var' is in the procedure/function line. var means changes will get changed in the caller variable.

No var means any changes inside the function do not get changed in the caller.

Neither one should cause an access violation. You would have to post some code for an explanation of the issue.


Thanks for this. That really helped.
I guess the violation error is something irrelevant to my question, but here's my code. Thank you very much in advance.

program keyListener;
var x,y:integer;
a,b,c: TPoint;
ab: TPointArray;

function keyListener(key,time: integer): TPoint;
var loopBack,tx,ty: integer;
begin
loopBack := GetTimeRunning + time;
writeLn('Loop time: ' + intToStr(time));
while(loopBack > GetTimeRunning) do
begin
if (isKeyDown(key)) then
begin
GetMousePos(tx,ty);
writeLn('Mouse Position: {X:' + intToStr(tx)
+ ' Y:' + intToStr(ty) + '}');
result := Point(tx,ty);
sleep(time div 2);
end;
end;
end;

procedure setValue(var keyL: TPoint; tpa: TPointArray; number: integer);
begin
tpa[number] := keyL;
end;

begin
setValue(keyListener(112,10000),ab,nextAvaliableLi ne);
end.

What I'm trying to do is get the mouse position (TPoint keyListener) and store it on an array (TPointArray ab) on the next available line of that array. (line 0 of array ab. (ab[0]))

riwu
11-22-2013, 01:18 PM
You need to increase the length of your tpa before you can add more elements to it.

SetLength(tpa, Length(tpa) + 1);
tpa[High(tpa)]:= keyL;

bonsai
11-22-2013, 01:37 PM
Also, you have the var on the wrong variable.

procedure setValue(var keyL: TPoint; tpa: TPointArray; number: integer);
begin
tpa[number] := keyL;
end;

Since you're changing tpa, it should be more like:

procedure setValue(keyL: TPoint; var tpa: TPointArray; number: integer);
begin
tpa[number] := keyL;
end;

barty180
12-04-2013, 04:04 AM
you guys are so smart =]