SRL Archive - OSR Help

Need help with Exception: Access violation


bud_wis_er_420

2013-02-18 18:52:29

I'm just trying to make a double check function that if found on 2nd check will output 'true'. I keep getting an error - "Exception: Access violation" - when I change the variable c to 2, imitating the objective wasn't met. Can't figure out why. Please someone fill me in. This should return true if c is one or false if c is two.

program new;

var
c: integer;


function DoubleCheckFunction: Boolean;
begin
result := false;
if (c = 1) then //objective met would go here
result := true
else
begin //If I change c to 2 = error
c := 1; //Changing this signifies whether the objective was met or not
if DoubleCheckFunction then
result := true;
end;
end;


begin
c := 0;
if DoubleCheckFunction then
writeln('true')
else writeln('false');

end.

Le Jingle

2013-02-18 18:58:31

Do you mean something like this? Where you can have multiple else if's? (usually good to have a default else, to make sure what ever you are evaluating gets evaluated - isn't always done, but is good practice)


var
c: integer;

function DoubleCheckFunction: Boolean;
begin
result := false;
if (c = 1) then //objective met would go here
result := true
else if (c = 2) then
begin //If I change c to 2 = error
c := 1; //objective met would go here
if DoubleCheckFunction then
result := true;
end else
Writeln('c is not equal to 1 or 2, but is = ' + IntToStr(c));
end;

begin
c := 0;
if DoubleCheckFunction then
writeln('true')
else
writeln('false');
end.

bud_wis_er_420

2013-02-18 20:10:50

No, maybe this will explain better. Sry I'm sometimes terrible at explaining things from my point of view.

function DoubleCheckFunction: Boolean;
begin
result := false;

//Code: Do something here

// If Do Something/c = This happend/1
if (c = 1) then //objective met would go here
result := true
else
begin
//Do something failed

//Code: Now Do this: fail safe

//Below now checks one more time if objective is
//complete w/o having to reenter all the above code

//If I hange c to 2 = error
c := 1; //objective met would go here
if DoubleCheckFunction then
result := true;
end;
end;

here is my actual funtion I'm working on. I made the above function to test that it will output true if the objective wan't met the first time.
FindUrnInInv returns true if 2 are found in inv. So to save coding lines, this should repeat it's self until 2 urns are found in inv. I want it set up this way is because I'm using the same func on other areas.



//this function is just added so you can understand the bottom func
function FindUrnInInv(move: Boolean): Boolean;
var
ItemSlot: integer;
begin
if CheckLogin then Exit;
result := false;

color := GetACAUrnColor;
count := CountItems('color', color, []);
if (count = 2) then
begin
WriteDebug('Urns found, Checking if in correct spot');
MoveUrns;
result := true;
end else
begin
WriteDebug('At least 2 Urns not found');
Exit;
end;
box := InvBox(1);
if not FindColor(x,y,color,box.x1,box.y1,box.x2,box.y2) then
begin
WriteDebug('Didn''t find urn in inv slot one');
result := true;
end;
end;

// This function shoule repeat twice if inv is empty, once if already have one urn in inv.
function GetBankUrns: Boolean;
var
BankSlot: integer;
Pt: TPoint;
begin
if CheckLogin then Exit;
result := false;
WriteDebug('Searching for urn in bank');
SearchBank(UrnType);
MouseBankSlot(1, 3);
if WaitFunc(@IsUrnUpText,RandomRange(200,220), RandomRange(900,1000)) then
Begin
ClickMouse2(0);
if WaitOptionMulti('thdraw-2', 1000) then
begin
if FindUrnInInv then
result := true;
else GetBankUrns;
end else
begin
WriteDebug('Did not find option to withdraw 2');
end;
end;
end;


This:
function GetBankUrns: Boolean;
var
BankSlot: integer;
Pt: TPoint;
begin
if CheckLogin then Exit;
result := false;
WriteDebug('Searching for urn in bank');
SearchBank(UrnType);
MouseBankSlot(1, 3);
if WaitFunc(@IsUrnUpText,RandomRange(200,220), RandomRange(900,1000)) then
Begin
ClickMouse2(0);
if WaitOptionMulti('thdraw-2', 1000) then
begin
if FindUrnInInv then
result := true;
else GetBankUrns;
end else
begin
WriteDebug('Did not find option to withdraw 2');
end;
end;
end;

Instead of this:

function GetBankUrns: Boolean;
var
BankSlot: integer;
Pt: TPoint;
begin
if CheckLogin then Exit;
result := false;
WriteDebug('Searching for pouch in bank');
SearchBank(UrnType);
MouseBankSlot(1, 3);
if WaitFunc(@IsUrnUpText,RandomRange(200,220), RandomRange(900,1000)) then
Begin
ClickMouse2(0);
if WaitOptionMulti('thdraw-2', 1000) then
begin
if FindUrnInInv then
result := true;
else
begin
WriteDebug('Searching for pouch in bank');
SearchBank(UrnType);
MouseBankSlot(1, 3);
if WaitFunc(@IsUrnUpText,RandomRange(200,220), RandomRange(900,1000)) then
Begin
ClickMouse2(0);
if WaitOptionMulti('thdraw-2', 1000) then
begin
if FindUrnInInv then
result := true;
end;
end;
end
end else
begin
WriteDebug('Did not find option to withdraw 2');
end;
end;
end;

Le Jingle

2013-02-18 20:48:09

I wouldn't recommend recursively calling your methods for a small context function as illustrated by your snippets. Especially since you have no base case for the recursive calls, so it's likely that you'll end up in an infinite loop.

if you need to wait for a urn or check for a urn, etc. while (condition(s)) do can create a more safe fail checking system. Saving coding lines isn't always grand, especially since recursive methods will take more time to execute.

bud_wis_er_420

2013-02-18 21:05:16

The funcs are not finished. I know, about the inf loop and will have fail safes in there to kick it out. I'm just trying to get why I'm getting the error . I have repeated code, in my script over and over again. I'm trying ideas on how to save lines of code without have to make numerous small snip-it functions to recall things again.

The best explanation I could think of was this tiny function below. It should return true if c = 1 and if you manually change c to 2 or anything else but 1, this would simulate the script doing it in run time giving an out put of false.

This is saying:

Do This

If it happend then
func = true - end of func
If not happen then
DO This
If not do this then Exit - end func
else continue

restart func

Do This

If it happend then
func = true - end of func

function DoubleCheckFunction: Boolean;
begin
result := false;

//Code goes here: Do something here = 10 lines of code

// If Do Something/c = This happend/1
if (c = 1) then //objective met would go here
result := true
else
begin
//Do something failed

//Code goes here: Now Do this to correct things

//Below now checks one more time if we can complete objective
// w/o having to reenter all the above code

//1 = objective met(func will = true)
//2 = objective not met(func will = false)
c := 1; //If I change c to 2 I get the error
// saves me from haveing to repeat 'Do something here' = 10 lines of code
if DoubleCheckFunction then
result := true;

//if above is false end func
end;
end;

Le Jingle

2013-02-18 21:28:26

From: bud_wis_er_420
The funcs are not finished. I know, about the inf loop and will have fail safes in there to kick it out. I'm just trying to get why I'm getting the error . I have repeated code, in my script over and over again. I'm trying ideas on how to save lines of code without have to make numerous small snip-it functions to recall things again.

The best explanation I could think of was this tiny function below. It should return true if c = 1 and if you manually change c to 2 or anything else but 1, this would simulate the script doing it in run time giving an out put of false.

program new;

var
c: integer;


function DoubleCheckFunction: Boolean;
begin
result := false;
if (c = 1) then //objective met would go here
result := true
else
begin //If I change c to 2 = error
c := 1; //Changing this signifies whether the objective was met or not
if DoubleCheckFunction then
result := true;
end;
end;


begin
c := 0;
if DoubleCheckFunction then
writeln('true')
else writeln('false');

end.


The example above, is not an effective recursive function. You would need a variable to check/use as a base case for the function to stop. As stands, the example will only continue to loop on end; think about expanding the function to something you know how to use before trying to simplify code to a recursive function (even though it's not always the best choice, aside from potential cleaner code)
Edit: I see your pseudo code now, and if you repeat a piece of code, you could make it into a small function, and call it in later functions that make use of it.

bud_wis_er_420

2013-02-18 21:39:53

ohhhh I see what your saying, it IS in an inf loop, lol. I get it now. Fixed

function DoubleCheckFunction: Boolean;
begin
result := false;
if (c = 1) then
result := true
else
begin
c := 2;
if (c = 1) then // <<<-- failsafe, lol
if DoubleCheckFunction then
result := true;
end;
end;

I'd +1 rep but your to only one that seems to help me and can't give you anymore lol

although I did already have it right. Just messed up my example, lol

function GetBankUrns: Boolean;
var
BankSlot: integer;
Pt: TPoint;
begin
if CheckLogin then Exit;
result := false;
WriteDebug('Searching for urn in bank');
SearchBank(UrnType);
MouseBankSlot(1, 3);
if WaitFunc(@IsUrnUpText,RandomRange(200,220), RandomRange(900,1000)) then
Begin
ClickMouse2(0);
if WaitOptionMulti('thdraw-2', 1000) then
begin
if FindUrnInInv then // <<<--- FailSafe
result := true;
else GetBankUrns; /// <<<--- Repeat Func
end else
begin
WriteDebug('Did not find option to withdraw 2');
end;
end;
end;