Log in

View Full Version : Function not returning a value



HT BaaFly
11-20-2011, 09:45 AM
Sorry for being bothersome, but I'm stuck again :duh:

I've got a function in my script which has to return a string. The string is just text copied from runescape's screen. Now, the function on its own works flawlessly but when I introduce it to the rest of the script it works once in ten tries.

The function(s):

function GetBuyPrice: string;
begin
result := GetNumerals(GetTextAtExWrap(117, 287, 327, 300, 0, 10, 1, 39372, 5, 'StatChars'));
end;

function GetSellPrice: string;
begin
result := GetNumerals(GetTextAtExWrap(117, 287, 327, 300, 0, 10, 1, 39372, 5, 'StatChars'));
end;

The entire script so far (I know the quality of it is bad, it's my first attempt at scripting something major):

program New;
{$i SRL/SRL.scar}
{$i srl/srl/misc/grandexchange.scar}
var
BuyIMG, SellIMG, Item1X, Item1Y: integer;

function isBuyOpen: boolean;
var
x, y: integer;

begin
BuyIMG := BitmapFromString(11, 14, 'meJxjYEAC706AEB5AMwXCUvJQKW' +
'SEJKsV3QFEyLJALkgXTIFB3QZh4wQggsgCGZJO1WoJvRA1QJJ Hydg' +
'gczZQF6+SIwRB1OulTQfKAhGXij5QUNImSTW4Scm3CoiADCDS TpgE' +
'VwA0BKhGxqNazrcRqBGCJCL74Aoghkg4FgPVAK2DIKHwXjQTg AoUg' +
'zqAhgARUBaiAOIXuAkQJOBRAERwWYhfMBFECgD511mR');
Result := (FindBitmapIn(BuyIMG, x, y, MSX1, MSY1, MSX2, MSY2));
FreeBitmap(BuyIMG);
end;

function isSellOpen: boolean;
var
x, y: integer;

begin
SellIMG := BitmapFromString(11, 14, 'meJxjYEAC6/6BEC4AkcWlBlkWjz' +
'k4pISl5CEIogDKRpLViu6AIIgCCBuuBsgwyJwtbJwARFATjBM knar' +
'VEnohaoAkj5IxUA1QF6+SIwRB1OulTYdYx6WiDxSUtElSDW5S 8q0C' +
'IiBDNapfKmYiXAHQEKAaGY9qOd9GoEYg0s+cIxTeC1cAMUTCs RioB' +
'mgdBCErgJgAVKAY1AE0BIiAshAFEHfCTYAgAY8CIEILDUwEkQ IAEO' +
'dcMw==');
Result := (FindBitmapIn(SellIMG, x, y, MSX1, MSY1, MSX2, MSY2));
FreeBitmap(SellIMG);
end;

Procedure WalktoSymbol;
var
x, y: integer;

begin
FindColorTolerance(x, y, 2216175, MMX1, MMY1, MMX2, MMY2, 20);
Mouse(x, y, 5, 5, True); //Clicks on the bank symbol
end;

procedure OpenGE;
var
x, y: integer;

begin
If (FindObj(x, y, 'Grand Exchange clerk', 6325165, 20)) then
begin
Mouse(x, y, 5, 5, False); //Right clicks the clerk
Mouse(x, y + 49, 2, 2, True); //Clicks on exchange
Wait(2000 + Random(500));
If InGE then
begin
exit;
end;
end else
MakeCompass('s');
Wait(1500 + Random(500));
FindObj(x, y, 'Grand Exchange clerk', 3679532, 20);
Mouse(x, y, 5, 5, False); //Right clicks the clerk
Wait(100);
Mouse(x, y + 49, 2, 2, True); //Clicks on exchange
end;

procedure ClickBack;
begin
While isBuyOpen or isSellOpen do
Mouse(45, 298, 5, 5, True); //Clicks the arrow to get back
end;

procedure GettingGEOpen;
begin
If isBuyOpen or isSellopen then
ClickBack;
If InGE then
exit;
If not InGE or not isBuyOpen or not isSellopen then
WalktoSymbol;
Wait(5000 + Random(1000));
OpenGE;
Wait(1000 + Random(500));
end;

function GetNumerals(const s: string): string; //By Echo_
var
i, j: Integer;
numArray: array of string;
begin
numArray := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
for i := 1 to High(s) do
for j := 0 to High(numArray) do
begin
if not (s[i] = numArray[j]) then Continue;
Result := Result + s[i];
end;
end;

function isSlotFree: boolean;
begin
While not (GEIsSpotFree(1)) or not (GEIsSpotFree(1)) do
begin
GEAbortAll;
GERecoverAll;
end;
result := True
end;

function GetMidPrice: integer;
begin
result := StrToInt(GetNumerals(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars')));
end;

function GetSellPrice: string;
begin
result := GetNumerals(GetTextAtExWrap(117, 287, 327, 300, 0, 10, 1, 39372, 5, 'StatChars'));
end;

function GetBuyPrice: string;
begin
result := GetNumerals(GetTextAtExWrap(117, 287, 327, 300, 0, 10, 1, 39372, 5, 'StatChars'));
end;

procedure SetPrice1;
begin
Mouse(385, 222, 5, 5, True); //Clicks on the '...' to enter a custom price
wait(1000 + Random(500));
SendKeys(IntToStr(GetMidPrice * 3), 200);
SendKeys(chr(13), 200)
Wait(500 + Random(500));
Mouse(263, 290, 5, 5, True); //Clicks confirm
end;

procedure SetPrice2;
begin
Mouse(385, 222, 5, 5, True); //Clicks on the '...' to enter a custom price
wait(1000 + Random(500));
SendKeys(IntToStr(GetMidPrice * 2), 200);
SendKeys(chr(13), 200)
Wait(500 + Random(500));
Mouse(263, 290, 5, 5, True); //Clicks confirm
end;

procedure SetPrice3;
var
decimal: Extended;

begin
Mouse(385, 222, 5, 5, True); //Clicks on the '...' to enter a custom price
wait(1000 + Random(500));
decimal := GetMidPrice * 1.3
SendKeys(FloatToStr(decimal), 200);
SendKeys(chr(13), 200)
Wait(500 + Random(500));
Mouse(263, 290, 5, 5, True); //Clicks confirm
end;

procedure SetPrice4;
var
decimal: Extended;

begin
Mouse(385, 222, 5, 5, True); //Clicks on the '...' to enter a custom price
wait(1000 + Random(500));
decimal := GetMidPrice * 1.15
SendKeys(FloatToStr(decimal), 200);
SendKeys(chr(13), 200)
Wait(500 + Random(500));
Mouse(263, 290, 5, 5, True);//Clicks confirm
end;

procedure FindHigh;
begin
If InGE and GEIsSpotFree(1) then
begin
GEBuyAt(1);
SendKeys('Feather', 150);
Wait(2000 + Random(500));;
If GetTextAtExWrap(64, 342, 115, 354, 0, 10, 1, 23200, 5, 'SmallChars') = 'Feather' then
begin
Mouse(87, 351, 5, 5, True); //Clicks on the feather in the list
Wait(1000 + Random(1000));
Mouse(72, 220, 5, 5, true); //Clicks on the quantity 1
Wait(1000 + Random(1000));
If GetMidPrice > 500000 then
begin
SetPrice4;
end else
If (GetMidPrice < 500000) and (GetMidPrice > 100000) then
begin
SetPrice3;
end else
If (GetMidPrice < 100000) and (GetMidPrice > 10000) then
begin
SetPrice2;
end else
If GetMidPrice < 10000 then
begin
SetPrice1;
end;
While not InGE do
begin
Sleep(100);
end;
Wait(1500 + Random(500));
Mouse(103, 139, 5, 5, True); // Click on the trade offer once complete
Wait(1500+ Random(500));
Writeln('Getting Sell Price');
GetSellPrice;
Writeln('Got Sell Price');
Mouse(415, 297, 5, 5, True); //Clicks on the first item to collect it
Wait(300 + Random(300));
Mouse(465, 297, 5, 5, True); //clicks on the second item to collect it
Wait(300 + Random(300));
end else
Writeln('Item not found.');
end else
GettingGEOpen;
end;

procedure FindLow;

begin
While not InGE do
begin
Wait(1000);
end;
GESellAt(1);
FindColorTolerance(Item1X, Item1Y, 10197922, MIX1, MIY1, MIX2, MIY2, 20);
Mouse(Item1X, Item1Y, 5, 5, True); //Clicks on the feather's coords
Wait(200 + Random(250));

Mouse(283, 222, 5, 5, True); // Decrease 5%
Wait(100 + Random(100));
Mouse(283, 222, 5, 5, True); // Decrease 5%
Wait(100 + Random(100));
Mouse(283, 222, 5, 5, True); // Decrease 5%
Wait(100 + Random(100));

Mouse(263, 290, 5, 5, True); // confirm
Wait(500+ Random(500));
Mouse(103, 139, 5, 5, True); // Click on offer
Wait(500+ Random(500));
repeat
Writeln('Getting Buy Price');
GetBuyPrice;
Writeln('Got Buy Price');
until((GetBuyPrice = '12') or (GetBuyPrice = '13') or (GetBuyPrice = '14'));
Wait(1000+ Random(500));
Mouse(415, 297, 5, 5, True); //click on collect money
Wait(300 + Random(300));
end;

procedure Loop;
begin
GettingGEOpen;
FindHigh;
FindLow;
end;
begin
ClearDebug;
SetupSRL;
MouseSpeed := 10;
Loop;
Writeln(GetSellPrice);
Writeln(GetBuyPrice);
end.

What is returned:

[QUOTE=HT BaaFly;847110]
SRL Compiled in 0 msec
coords: 41, 164
coords: 41, 164
Getting Sell Price
Got Sell Price
coords: 41, 164
Getting Buy Price
Got Buy Price


Successfully executed.

All help is greatly appreciated

YoHoJo
11-20-2011, 09:47 AM
Before you call GetBuyPirce/GetSellPrice add a writeln

One saying Wreiln('Getting Buy Price') and another for the sell.
That way you can see if the functions are even being called or not.

Run the script and tell me if you see those come up in the debug box.

HT BaaFly
11-20-2011, 09:52 AM
Sure, just a minute

HT BaaFly
11-20-2011, 09:56 AM
SRL Compiled in 0 msec
coords: 41, 164
coords: 41, 164
Getting Sell Price
Got Sell Price
coords: 41, 164
Getting Buy Price
Got Buy Price


Successfully executed.

*Edited OP for this new source code*

YoHoJo
11-20-2011, 09:58 AM
Hmm quite odd, and you say when you call any of them (GetBuyPrice or GetSellPrice) alone you are SURE they work? Odd

HT BaaFly
11-20-2011, 10:00 AM
Yup, I've just made sure again

YoHoJo
11-20-2011, 10:15 AM
Oh also between the getting and got, and after the function have it writeln the actual price durf!
Like

Writeln('Getting Sell Price');
GetSellPrice;
WriteLn(GetSellPrice);
Writeln('Got Sell Price');

And same with buy price.

YoHoJo
11-20-2011, 10:26 AM
Going to bed now, just letting you know so you won't be eagerly awaiting an answer from me anytime soon, good luck!

HT BaaFly
11-20-2011, 10:48 AM
It's working now but I don't understand this, what difference does it make if the Writeln is in the loop instead of in the same procedure as the GetSellPrice or GetBuyPrice. Anyway, thanks a lot for your help and have a goodnight :)

YoHoJo
11-20-2011, 11:06 AM
Are you talking about the debug writelns we put in (getting/ got price) or the written that tells you the actual amount? If you're talking about actual amount then you actually never had a written for it, your function simply grabbed the buy/sell price and that's it, you never told the script to actually do anything with it besides grab it, which is why we needed the writeln(buyprice) or sell price. Get it? Typing this from phone sorry for any typos! Anywyas glad you got it worning woo!!

HT BaaFly
11-20-2011, 07:19 PM
nah I was talking about the writelns in the main loop but anyway, it's all solved now, thankfully. the script is nearing completion so i'll release it in the first script section soon for people to look at (don't expect something great)