ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Chat => Topic started by: 12TimesOver on October 31, 2008, 11:12:43 AM

Title: Looping without GoTo
Post by: 12TimesOver on October 31, 2008, 11:12:43 AM
Just a quick one here.

In the quest for learning alternative methods for setting up loops within my scripts without the use of GoTo statements (just a personal quest to enhance my creative thinking) I wondered if the following example is syntacticly accurate in it's use of the #RETURN sysvar preloaded with a value. I've done this before with my own user vars but just wondered what other people thought about this as a general practice.

This example uses TM's Gumpwait sub which returns #FALSE if the timeout value is reached before the Gump is found.

Code: [Select]
   set #RESULT #FALSE
   while #RESULT = FALSE
      {
      set #lobjectid %Container
      event macro 17
      gosub GumpWait Generic_Gump Generic_Gump
      finditem %ItemInContainer
      if #findkind <> 1
         set #RESULT #FALSE
      else set #RESULT #TRUE
      }

Thanks for your input!

XII
Title: Re: Looping without GoTo
Post by: TrailMyx on October 31, 2008, 11:37:16 AM
Hmm, well I think you are kinda "over-writing" the intention of the return value for GumpWait since you aren't really using it now in this context.  So I think there's some unintended consequences to your usage.

With that said, I have used the #RESULT variable for general purposes many times; sometimes I just don't want to create a variable in a quicky sub, i.e.:

Code: [Select]
;------------------------------------------------------------
sub TM_GetPersistantVariable
  set #RESULT %1 , _ , #CHARID
  set #RESULT * . #RESULT
return #RESULT
;------------------------------------------------------------
sub TM_SetPersistantVariable
  set #RESULT %1 , _ , #CHARID
  set * . #RESULT %2
return

Personally, I get a chuckle out of people who will jump through no end of hoops to rid themselves of goto's and tags.  Oh, *chuckes*
Title: Re: Looping without GoTo
Post by: 12TimesOver on October 31, 2008, 12:10:54 PM
Hmm, well I think you are kinda "over-writing" the intention of the return value for GumpWait since you aren't really using it now in this context.  So I think there's some unintended consequences to your usage.

With that said, I have used the #RESULT variable for general purposes many times; sometimes I just don't want to create a variable in a quicky sub, i.e.:

<snip>

Personally, I get a chuckle out of people who will jump through no end of hoops to rid themselves of goto's and tags.  Oh, *chuckes*

Hehe, yeah that wasn't really a good example since I probably wouldn't be reopening a bag when a #finditem failed. I was just more concerned with the concept or syntax itself than the logic. Sounds like I'm not breaking any laws then.

As for the goto thing, well I'm just really trying to understand both sides and trying to think about things in more than one way. It really is more of a learning exercise for me than simply an evil goto statement remove activity. Personally I always get a feeling that I'm being inefficient when I pre-load a variable for a loop constraint just for the sake of loading the variable rather than as an activity of some other  logic within the program.

Did that make sense?

XII
Title: Re: Looping without GoTo
Post by: Cerveza on October 31, 2008, 12:59:16 PM
GOTO - Bad  >:(

While - Good  :D

Repeat/Until - Good  ;D
Title: Re: Looping without GoTo
Post by: TrailMyx on October 31, 2008, 01:56:13 PM
Wow, I'm surprised it took you so long Cerveza!  heh
Title: Re: Looping without GoTo
Post by: 12TimesOver on November 01, 2008, 01:37:22 AM
Wow, I'm surprised it took you so long Cerveza!  heh

LOL I was thinking the same thing!
Title: Re: Looping without GoTo
Post by: Endless Night on November 01, 2008, 07:10:54 AM
Well since basic 101 .. way back in the distant pass when commadors and sinclairs walked the earth .. i have never really found a need to use a goto statment.  Takes a slight readjustment of thinking but once you have it i...f your like me you will find gotos a real pain in the backside to logic flow.

Title: Re: Looping without GoTo
Post by: Nicar on November 01, 2008, 09:49:16 AM
Once I get around to my bod sorter or gaman quest heartwood run script, I think in spite, I'm going to do it with nothing but gotos.   no subs, just all gotos. goto here, do this, sysmsg GoT0s Ru73!
Title: Re: Looping without GoTo
Post by: TrailMyx on November 01, 2008, 11:22:48 AM
Once I get around to my bod sorter or gaman quest heartwood run script, I think in spite, I'm going to do it with nothing but gotos.   no subs, just all gotos. goto here, do this, sysmsg GoT0s Ru73!

Unfortunately, that's where the GOTO got the bad rap.  Typically it's the first looping construct people learn because of past experience with the Jurassic period of computers as EN alluded to, and then are sooooo overused.  I kinda give EasyUO scripters a pass because to fuel the fires of using gotos, that was the only looping construct available other than the "FOR" statement in the original EasyUO 1.42 and before.  So you didn't have the nice "WHILE" or "REPEAT/UNTIL".  Also there was no "ELSE" with the "IF" statement so every one of your "IF" statements had to be prefaced with a 3rd argument if you wanted to skip a block of code on failure.  Now that is some crappy scripting, but some have held to the old ways so it makes code super difficult to read and understand the flow.

So just be thankful that you have these other nice loopers available, else you'd still be dining on Sinclair steaks.
Title: Re: Looping without GoTo
Post by: Cerveza on November 14, 2008, 04:31:36 AM
I do use this as my main loop on all my scripts

Code: [Select]
TM:
repeat
.....
..... loop goes here
.....
until #CharGhost = YES
while #CharGhost = YES
  wait 0
GoTo TM

Ready for the punch line?

Because I've always told TM that my scripts would only have gotos......

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

OVER MY DEAD BODY!

 :o :o :o :o :o :o
 ;D ;D ;D ;D ;D ;D

Get it? #CharGhost = YES, my DEAD body!!

I could actually code that around the goto, but I have it in just for TM.

Only Corpses use gotos  :P
Title: Re: Looping without GoTo
Post by: _C2_ on November 14, 2008, 07:11:45 AM
Funny stuff!  I've always been able to count on Cerv. for hating goto's but I also have to thank him for it too.   Ever since you started ripping them on past boards, I've challenged myself to try to write without them just for the challenge.  Nowadays it isn't a challenge anymore and I can write very differently.  I see a flow in my code that couldn't exist using strictly goto's.  I still use one here and there for basic ignoring other NPC loops occasionally even though i could edit that out too but it has helped me bring coding up to another level. 
Title: Re: Looping without GoTo
Post by: Xclio on November 14, 2008, 08:22:55 AM
Cerv is my reason for hating the goto's as well.  I don't really "hate" them I guess but since I started to avoid them I have been much happier with the flow of my code and it makes debugging alot easier for myself and others I think.
Title: Re: Looping without GoTo
Post by: Endless Night on November 15, 2008, 04:54:03 PM
Most of the funny stuff that happens in the inexperienced scripters scripts are the result of using  gotos and not using Returns to exit a sub.
Title: Re: Looping without GoTo
Post by: TrailMyx on November 15, 2008, 07:08:46 PM
Most of the funny stuff that happens in the inexperienced scripters scripts are the result of using  gotos and not using Returns to exit a sub.


That actually was part of my motivation for ScriptUO.  EasyUO gives ZERO feedback regarding the syntax and style of the code that you write.  Worst is that you could have the code line:

Code: [Select]
  barney loves ice %cream

and EasyUO will happily execute past it (doing nothing of course).  That zero feedback is really bad to have especially since EasyUO scripting is the first taste that many have had to "structured" programming languages.  Without a slap on the wrist, bad habits are developed quickly.  This is also why the first real feature I added to ScriptUO was the syntax checker.  Even in the early days of AppleSoft BASIC, there was a general syntax check attempted as the interpreter did its execution.
Title: Re: Looping without GoTo
Post by: bliss on December 11, 2008, 08:03:07 AM
ok you guys have totaly confused me now. for some one who is just starting on his path to scripting is it easyer to learn with or with out goto's ?
Title: Re: Looping without GoTo
Post by: TrailMyx on December 11, 2008, 08:23:22 AM
ok you guys have totaly confused me now. for some one who is just starting on his path to scripting is it easyer to learn with or with out goto's ?

You know, whatever works for you.  There are a few people here who are militantly against using gotos because some strange GOTO monster scared them when they were young.  If you find them useful, then by all means.  They are just another tool in the toolbox.  The problem with most people is they OVERUSE them. 

For you as you are just starting out, try to understand how the WHILE, REPEAT/UNTIL looping structures work also.  Then you can determine for yourself how you might want your code to loop.
Title: Re: Looping without GoTo
Post by: 12TimesOver on December 11, 2008, 08:27:21 AM
Use whatever method suits the purpose.

If you do use goto's remember that they aren't the only way to setup a loop and that it's bad, bad, bad to jump between subs with goto's.

I've taken a liking to repeat...until statements myself but I still see the value of an appropriately placed goto now and again.

XII
Title: Re: Looping without GoTo
Post by: TrailMyx on December 11, 2008, 08:29:49 AM
If you do use goto's remember that they aren't the only way to setup a loop and that it's bad, bad, bad to jump between subs with goto's.

Amen, brotha!
Title: Re: Looping without GoTo
Post by: Cerveza on December 11, 2008, 08:53:08 AM
Don't listen to them.... FEAR THE GOTO for they are evil  :-\

GOTO's prance around scripts like they are Gods. No matter what is going on, they have absolute control over where the code flows. And you know what they say about absolute power... yep, it Corrupts Absolutely.

Lets have a look at this little gosub. He's just enjoying his part in the script waiting patiently until he's needed, then he'll perform his function as programmed, then graciously return to the main script. He's a team player.

Code: [Select]
gosub Our_Friend
the rest of the script

sub Our_Friend
waits patiently until called
finally the moment comes
performs his part
knows he's a small part of the bigger picture
finishes up his role
happily returns control back to the script

Now lets see what GOTO does to him....

Code: [Select]
gosub Our_Friend
the rest of the script

sub Our_Friend
waits patiently until called
finally the moment comes

GOTO SOMEWHERE_ELSE

looks around confused
wonders where the script went
wonders why the script doesn't need him any more
worries it might have been something he said, or did
wonders if he should return, or not....
feels that if he did return it might be awkward for the mainscript
confusion sets in
sits idly by, crying quietly to himself, wondering why the script turned it's back on him

...... sorry..... something in my eye...... sec.......

Title: Re: Looping without GoTo
Post by: TrailMyx on December 11, 2008, 08:58:10 AM
It's frightening what goes on in Cervezas mind!!!  ;D
Title: Re: Looping without GoTo
Post by: OMGBurgers on December 11, 2008, 09:23:53 AM
I used to use goto's a LOT but I've been really trying to cut down on them.  The main thing that is making me cut down on them is because of ScriptUO.  I hate to clutter that nifty right panel that shows all my tags/gotos with a buncha stupid goto's because I just never used whiles/repeatuntil etc.

Can't say it made the code more stable, because if you do it right it's all the same.  But I can say wow, it's really made debugging and reading it a LOT easier because you don't have to jump aroudn the tags with your eyes you just see the brackets and indents and know where your loops and all are.

I would personally use whiles/repeatuntils as much as possible, but don't be afraid of using gotos for your "main loops".  I still have a LOT to learn.  I'm sure people who read my code know that.  But hey, it works! :D
Title: Re: Looping without GoTo
Post by: Endless Night on December 13, 2008, 09:08:24 AM
Wow this thread has not imploded yet...

Gotos = Evil

Repeat..until / While  = ! Evil.

LOL

Gotos should be used like crack .. sparingly and precautions taken.. otherwise you will soon be headed down the ugly road of addiction....