SRL Archive - Help and Support

Function returning two values?


RISK

2011-12-11 16:35:51

I would like this function to return two values:
function InMine: Boolean;
var
I : Integer;
posMM : TPoint;
begin
if (not(LoggedIn)) then
Exit;
FindNormalRandoms;

for I := 1 to 5 do
begin
case (I) of
1 : posMM := Point(247, 241); // Center of essence mine
2 : posMM := Point(312, 176); // Northeast
3 : posMM := Point(320, 315); // Southeast
4 : posMM := Point(173, 319); // Southwest
5 : posMM := Point(173, 175); // Northwest
end;

Result := SPS_PosOnMM(posMM);
if (Result) then Break;
end;
end;

The values I would like to return are a boolean and a TPoint. But, as far as I can tell, there is no direct way to do this without using global variables. Is there another way to have it return two values?

Sabzi

2011-12-11 16:37:18

Make a record with a TPoint and a boolean in it then let the function return that type(the record you created).

weequ

2011-12-11 16:37:44

From: RISK
I would like this function to return two values:
function InMine: Boolean;
var
I : Integer;
posMM : TPoint;
begin
if (not(LoggedIn)) then
Exit;
FindNormalRandoms;

for I := 1 to 5 do
begin
case (I) of
1 : posMM := Point(247, 241); // Center of essence mine
2 : posMM := Point(312, 176); // Northeast
3 : posMM := Point(320, 315); // Southeast
4 : posMM := Point(173, 319); // Southwest
5 : posMM := Point(173, 175); // Northwest
end;

Result := SPS_PosOnMM(posMM);
if (Result) then Break;
end;
end;

The values I would like to return are a boolean and a TPoint. But, as far as I can tell, there is no direct way to do this without using global variables. Is there another way to have it return two values?
One option is to make a record that has a boolean and a TPoint. Other option is to add variables to the function parameters. Just like FindColor(X, Y…): Boolean; (X and Y are Integers and it returns a boolean)

BraK

2011-12-11 16:44:25

Check my tutorial on building functions :)

Making and understanding Functions with Multiple Parameters

RISK

2011-12-11 16:46:44

Ah! That worked beautifully, you two. Much appreciated.

Will do, BraK. :) I didn't know you had a tutorial like that up, haha. My bad.

BraK

2011-12-11 16:49:12

It goes into all the different types of parameters. Var, Const, out, and Unamed parameters and what they all do.

Bobarkinator

2011-12-13 21:26:36

Make it return a TPoint of the posMM (I'm assuming that's the TPoint you wanted to return) but if it doesn't find the posMM just make it return a TPoint with coordinates (0,0) so when you call that function in your script you just need to check the result of that function call and if it's (0,0) it's essentially a false, but if it's not then you know it's true and you have your TPoint.

Something like this:

function InMine: TPoint;
var
I : Integer;
posMM : TPoint;
begin
if (not(LoggedIn)) then
Exit;
FindNormalRandoms;

for I := 1 to 5 do
begin
case (I) of
1 : posMM := Point(247, 241); // Center of essence mine
2 : posMM := Point(312, 176); // Northeast
3 : posMM := Point(320, 315); // Southeast
4 : posMM := Point(173, 319); // Southwest
5 : posMM := Point(173, 175); // Northwest
end;

if (SPS_PosOnMM(posMM))
begin
Result := posMM;
exit; //Is this the right call to exit a function? I can't really remember
end;
end;
Result := Point(0,0); //If we reach the end of the function then it needs to return "false"
end;

So when you call the function just check the TPoint it returns and see if it's 0,0

euphemism

2011-12-13 23:26:54

The simplest way would be to have

function InMine(var Point: TPoint): Boolean;
You pass a TPoint into the function through "Point" and you modify "Point" within the function. Which in turn changes the variable you passed in. That way, you get your point, and the function has the ability to return true or false.'

E: You are probably better off using out:


function InMine(out Point: TPoint): Boolean;