View Full Version : Labels
Can you call goto Label within a Case argument?
HarryJames
03-31-2011, 01:33 AM
You can't use goto (to my knowledge). You can call a procedure though;
Procedure say1;
Begin
Writeln('Looks like the number was 1!');
End;
Procedure say2;
Begin
Writeln('Looks like the number was 2!');
End;
Begin
Case random(2) of
1: say1;
2: say2;
End.
i luffs yeww
03-31-2011, 01:38 AM
I was wrong, you can't (deleted post because I'm butthurt).
But, I'll say this again because it's important; you shouldn't use goto.
ok so ill just change my
Case Blah of
yeah: goto Whatever
nah: goto IDK
to
if blah = yeah then
goto whatever
else if blah = nah then
goto IDK;
Edit: Yeah when i changes it to if thens it compiled
Sure. I wouldn't recommend using goto's though.
Reasons?
@Yago: you can't use them to break out of or into a loop. I'm thinking that applies to case statements as well, but I honestly don't remember right off the top of my head.
Edit:
ok so ill just change my
Case Blah of
yeah: goto Whatever
nah: goto IDK
to
if blah = yeah then
goto whatever
else if blah = nah then
goto IDK;
Edit: Yeah when i changes it to if thens it compiled
Would be "better" anyways as if..then is generally faster than using a Case.
Yeah i changed it to if thens and it worked thanx
i luffs yeww
03-31-2011, 01:43 AM
Reasons?
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF
^ PDF (obvi). Paper by Edsger Dijkstra called "Go-to statement considered harmful."
I personally like goto cause I save lines of code using it. If not then i would have to make a repeat until loop created a new Boolean set it false for so many things and true if blah... Ive don't it before it wasn't pretty though that's all... and it would be worse it i had a loop within a loop cause I cant break out of it.
That's why i go with goto's
i luffs yeww
03-31-2011, 02:36 AM
You can break out of loops with "Break;" ;)
I see no reason to use goto.
Yeah but as I said, If you have loops within loops within loops then its a lot more annoying.
i luffs yeww
03-31-2011, 10:10 PM
:x I don't see why using a goto and messing with the structure of your script is better than managing loops..
i luffs yeww
03-31-2011, 10:16 PM
Less code doesn't always mean anything is better. It just leads to more mistakes, less organized code, and more confusion.
Sometimes ... So far its working great for me thanks though
masterBB
03-31-2011, 10:19 PM
Less code ;)
I've been taught not to use them. It's for your overview and the layout of script, apart from the practical issues.
Can you give an example of a loop inside a loop inside a loop. I'm sure it can be done different, without extreme nesting nor using goto ;).
Yeah sure ... I'm pretty sure NCDS agrees(at least partially0.
Say I have
repeat
repeat
repeat
until(true1)
if (false1) then
break; //but i want to go to the start ...
// i would have to add the if false thing to everything else...
// say i only wanted to break out of one loop or go to the next part of the function
// its more annoying in my opinion
until(true2)
if (false1) or (false2) then
break
until(true3)
if (false1) or (false2) or false3 then
exit
else ... //go to the next thing
i luffs yeww
03-31-2011, 10:33 PM
Set a value and then break, then check for a certain value and break if it's true/whatever you do.
Point is, there is never a valid reason to use goto over anything else.
Or anything else over goto, but can you show me an example of what you are talking about?
Dude, you can almost always avoid using labels someway or another.
i luffs yeww
03-31-2011, 11:06 PM
Can you show me a code example of where you use goto? :)
I use it in 2 other places but heres one
Label Starting;
var
A, B, Tries: Integer;
RawInv: TInvItem;
begin
if not (LoggedIn) then
Exit;
begin
Tries := 0;
WriteLn('Running Procedure CookFood.');
Starting:
AntiRandoms;
if (GetPlaneIndex = 0) then
if R_ItemExists(Players[CurrentPlayer].Integers[5], RawInv)then
begin
if not ItemActivated(RawInv.Slot) then
begin
if R_ClickItem(RawInv.Slot,true,'') then
begin
if WaitUntil(5000, ItemActivated(RawInv.Slot)) then
Writeln('Item Is Activated.')
else
Inc(Tries);
if (Tries >= 5) then
begin
EndScript('Item Not Activated.');
Exit;
end;
Goto Starting;
end
else
Inc(Tries);
if (Tries >= 5) then
begin
EndScript('Trouble Clicking Item.');
Exit;
end;
Goto Starting;
Edit: Im looking for efficiency and ease
i luffs yeww
03-31-2011, 11:42 PM
Can you show the whole thing?
Yeah I'm not done fixing it and stuff though. Ill get up up in a bit.
Edit: Here:
Procedure CookFood;
Label Starting;
var
A, B, Tries: Integer;
RawInv: TInvItem;
begin
if not (LoggedIn) then
Exit;
begin
Tries := 0;
WriteLn('Running Procedure CookFood.');
Starting:
AntiRandoms;
if (GetPlaneIndex = 0) then
if R_ItemExists(Players[CurrentPlayer].Integers[5], RawInv)then
begin
if not ItemActivated(RawInv.Slot) then
begin
if R_ClickItem(RawInv.Slot,true,'') then
begin
if WaitUntil(5000, ItemActivated(RawInv.Slot)) then
Writeln('Item Is Activated.')
else
Inc(Tries);
if (Tries >= 5) then
begin
EndScript('Item Not Activated.');
Exit;
end;
Goto Starting;
end
else
Inc(Tries);
if (Tries >= 5) then
begin
EndScript('Trouble Clicking Item.');
Exit;
end;
Goto Starting;
end
else
Writeln('Item Is Activated.');
if (FindCRange) then
begin
AntiRandoms;
ClickMouse2(True)
WriteLn('Clicked On The Cooking Range.');
R_Flag; //If A Couple Tiles Away From The Range.
if WaitUntil(4000, FindText(A, B, 'Raw', StatChars, MCx1, MCy1, MCx2, MCy2)) then
begin
WriteLn('Found The RawDTM In The Chat Box.');
MouseBox(230,395,290,455,1);
if R_WaitUpText('ook', 5000) then
begin
ClickMouse2(True);
WriteLn('Clicked The Chat In An Attempt To Cook.');
if (IsCooking) then
begin
Writeln('Finished Cooking Food.');
IncEx(Players[CurrentPlayer].Integers[7], 1);
stats_IncVariable('Loads Done', 1);
end
else
Inc(Tries);
if (Tries >= 5) then
begin
EndScript('IsCooking Is False.');
Exit;
end;
Goto Starting
end
else
EndScript('Incorrect Uptext in Main Chat for Raw ' + Players[CurrentPlayer].Strings[1] + '.');
end
else
EndScript('Could not find RawText in Chat Box.');
end
else
EndScript('Unable To Find Cooking Range And Cook Food.')
end
else
EndScript('No Raw ' + Players[CurrentPlayer].Strings[1] + ' Exist In The Inventory.')
else
EndScript('Not On The Correct Floor.');
end;
end;
You can use a for loop. for tries := 0 to 5 do. Then just break when failed or whatever.
Great idea... I might try but are the labels bad in the script?
People are saying that they are bad in general. I am not sure of the reasons why although I was taught that it was a bad practice :p.
People are saying that they are bad in general. I am not sure of the reasons why although I was taught that it was a bad practice :p.
Yeah but no one really brings up a good reason ... Its working for me so far...
Obviously it will work. To me, it makes the code look dirty but yeah..
HarryJames
04-01-2011, 01:59 AM
When you're expanding your script to do multiple things, you're probably going to have to do some fancy coding to make it work as intended.
I hope you're not talking to me. Labels aren't even fancy.
Wow, funny how I wrote the script I just don't remember one of the main reason's why I used the labels. I wanted to be able to debug... You see the endscript() procedure I made lets me see the problems in the script. Its prints that line out in the finalreport... If I created a loop then I don't know a way i can do that ...
You can do it the same way you're doing it. But when you put goto just replace that with continue.
HarryJames
04-01-2011, 02:13 AM
Sex, I was under the impression that is was the script. I didn't have time to read it. So adding new
Features to a script without procedures, that is just a load of loops would take a lot of time and effort ;)
Right. Adding on to that, I'm sure you could split that big procedure in to a couple smaller procedures maybe following a naming scheme like Cookfood_proc1 etc.
i luffs yeww
04-01-2011, 02:34 AM
Yago, I did post reasons not to. The letter by Edsger Dijkstra.
I didn't have time to read the whole thing, but I read page 1 and some of page 2 and it was worded weirdly or I was just not in the comprehensive mood or not. Anyways thanks you guys for your help
Zyt3x
04-01-2011, 11:20 PM
Instead of using goto statements in a function, you could use repeat...until(true) and do Continue; instead of goto StartofFunctionLabel;
Zyt3x
04-01-2011, 11:32 PM
That's what I said :p.Right.. I didn't read the whole thread :p
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.