SRL Archive - OSR Help

If... Then... Else..., What am I going wrong?


SomeGuyFromHere

2012-12-30 18:50:35

Hey,

so I always thought if…then…else works like this:
If the condition from if is met, then the code under "then" is executed and not what is included in else.
The "else"-code is only executed when the condition is not met.

:wacko:

Doesn't seem to work that way.
Here is a procedure I wrote; note the underlined part, this seems to be the one that is causing trouble:

Procedure OpenGate;

var
x, y: Integer;
begin

if FindObjCustom(x, y, ['Open Gate'], [3622483, 3094077], 3)

then
begin
Writeln('Gate found. Clicking.');
ClickMouse(x, y, mouse_Left);
Writeln('OK, clicked on the gate. Waiting for 12 seconds for the process to finish...');
Wait(12000);
end

else Writeln('Gate not found, is it already open? Checking...');

[U][B]if [/B]FindObjCustom(x, y, ['Close Gate'], [3622483, 3094077], 3)
[/U] [U][B]then [/B]begin Writeln('Gate is already open! Continuing with walking.')[/U]
[U]end

[B]else[/B]
Writeln('Something might have gone wrong, no gate in sight, neither open or closed. Teleporting back to Burthorpe.');
LodestoneTeleport('Burthorpe');
end;[/U]

Now what I want to happen is that after the gate gets opened or the gate is already open, the rest of the code is skipped with and the procedure ends.
What actually happens is that, no matter what the result of the door opening was the failsafe fires…

Using the "Exit;" command I was able to work around the problem for a while but since I reprogrammed the code this is not longer possible, except with labels which you shouldn't use unless it's really necessary, so I need to get this done properly.

Could someone point out my error? Thanks a lot Villavu!

Ian

2012-12-30 18:53:49

The then should be ath the end of the if line, and if you want something with multiple lines to be executed you need them to be between begins and ends.

Procedure OpenGate;
var
x, y: Integer;
begin
if FindObjCustom(x, y, ['Open Gate'], [3622483, 3094077], 3) then
begin
Writeln('Gate found. Clicking.');
ClickMouse(x, y, mouse_Left);
Writeln('OK, clicked on the gate. Waiting for 12 seconds for the process to finish...');
Wait(12000);
end else
Writeln('Gate not found, is it already open? Checking...');
if FindObjCustom(x, y, ['Close Gate'], [3622483, 3094077], 3) then
begin
Writeln('Gate is already open! Continuing with walking.')
end else
begin
Writeln('Something might have gone wrong, no gate in sight, neither open or closed. Teleporting back to Burthorpe.');
LodestoneTeleport('Burthorpe');
end;
end;

I suggest reading through this if you haven't already: http://villavu.com/forum/showthread.php?t=58935 :)

NCDS

2012-12-30 21:46:26

I'm assuming you want the break; command, which 'breaks' out of the current loop and continues with the rest of the code. The tut BMW posted is really nicely written as well if you're looking for some further explanation.

SomeGuyFromHere

2013-01-02 14:34:44

BMWxi, thanks a lot, stuff seems to work now :)