ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Chat => Topic started by: Coragin on February 07, 2010, 04:11:24 AM

Title: repeat until or goto, which do you use?
Post by: Coragin on February 07, 2010, 04:11:24 AM
depending on if i can remember repeat until I use it, but for the most part I use goto loop and have the sub handle if it should return or stop.  I guess its pretty much the exact same thing just different wording.

like going from mi to ca, car vs bus, both will get you there, just car gives you more freedom over your time vs bus you have less to do besides sitting and waiting but it takes longer.
Title: Re: repeat until or goto, which do you use?
Post by: 12TimesOver on February 07, 2010, 04:50:14 AM
A few threads where this has been discussed a bit Coragin, some good reading in these:

http://www.scriptuo.com/index.php?topic=1754.0
http://www.scriptuo.com/index.php?topic=1091.0
http://www.scriptuo.com/index.php?topic=523.0
Title: Re: repeat until or goto, which do you use?
Post by: Cerveza on February 07, 2010, 04:51:51 AM
goto's are for losers. ;)
Title: Re: repeat until or goto, which do you use?
Post by: Coragin on February 07, 2010, 05:08:26 AM
I like if x goto y else return
Title: Re: repeat until or goto, which do you use?
Post by: Paulonius on February 07, 2010, 11:14:03 AM
I read the title of your post and opened it to see who flamed you for it.  I don't think you are done catching S@#$ for this one, but I do see the value in GoTo's, or at least I am not advanced enough to get around using them and therefore am still a loser.

The one use for them that I can't figure out a workaround for is for when I return a negative result from a gumpwait or target cursor wait and I need to go back to the beginning of a sub with multiple clicks like this one:


Code: [Select]
Sub SetCraftGump
      finditem %TinkersTools C_ , #BackpackID
      if #findcnt < 1
         {
         display Get a tinker tool in your backpack and hit play in the EUO window
         pause
         }
      if #findcnt < 2
         gosub MakeTinkersTools
      finditem %CurrentToolType C_ , #BackpackID
      if #findcnt < 2
         gosub MakeTwoTools
      CraftLoop:
      finditem %CurrentResource C_ , #BackpackID
      if #findstack < %CurrentResourceCnt
         Gosub MoveResources %CurrentResource %CurrentResourceCnt %ResourceSecure #BackpackID
      Finditem %CurrentToolType C_ , #backpackid
      Set #lobjectid #findid
      Event macro 17 0
            gosub waitforgump %cwin
            if ! #result
               Goto CraftLoop
      gosub offsetClick %CurrentResourceXClick1 %CurrentResourceYClick1
            gosub waitforgump %cwin
            if ! #result
               Goto CraftLoop
      gosub offsetClick %CurrentResourceXClick2 %CurrentResourceYClick2
            gosub waitforgump %cwin
            if ! #result
               Goto CraftLoop
      gosub offsetClick %CurrentItemxclick1 %CurrentItemyclick1
            gosub waitforgump %cwin
            if ! #result
               Goto CraftLoop
      gosub OffsetClick %CurrentItemxclick2 %CurrentItemyclick2
            gosub waitforgump %cwin
            if ! #result
               Goto CraftLoop
      If %xclick2 = 380
         {
         gosub OffsetClick %CurrentItemxclick3 %CurrentItemyclick3
               gosub waitforgump %cwin
               if ! #result
                  Goto CraftLoop
         }
Gosub OffsetClick 30 450
wait 5
Return

I can't think of an efficient way to replace the GoTo's in this sub.  However, I am willing to embrace my ignorance and learn from my betters if someone wants to teach me something...
Title: Re: repeat until or goto, which do you use?
Post by: Cerveza on February 07, 2010, 11:46:59 AM
If it's a condition you can use while... like

while C in #charStatus
{
cast cure
wait 20
}

See, that will constantly check your #charStatus and keep casting cures until your not poisoned.

Whiles work great for stuff like that. Repeat/Until works better for stuff that needs to be checked.... like if your making something and you want to keep trying until you make it.

repeat
{
findItem XXX C_ , #backpackID.
make item XXX
}
until #findCnt > 0

That will keep making something until you have more then 0 of them in your pack.

You can use While the same as Repeat, but it really works best by itself when looking for a EUO condition flag.

goto's are just bad mojo... they lead to writing scripts that have you use them to exit gosubs... which is just bad form.
Title: Re: repeat until or goto, which do you use?
Post by: Paulonius on February 07, 2010, 01:02:55 PM
Cerv, I don't mean to be a complete loser or to be annoying.  Like I tell my wife: I want to do things better, I just don't know how to fix the problem. What do you think of using them in the sub I posted?  Is there an alternative?
Title: Re: repeat until or goto, which do you use?
Post by: Cerveza on February 07, 2010, 01:05:11 PM
I'll dig deeper into that at work ;)
Title: Re: repeat until or goto, which do you use?
Post by: TrailMyx on February 07, 2010, 01:09:20 PM
Don't forget that 1.5 version of EUO also included the use of "break" and "continue" for the new looping constructs.
Title: Re: repeat until or goto, which do you use?
Post by: Endless Night on February 07, 2010, 05:23:43 PM
paulo .. do the negative.. instead if ! #result goto blabla ..   do if #result do action .. until #result = #true

Code: [Select]
Sub SetCraftGump
      finditem %TinkersTools C_ , #BackpackID
      if #findcnt < 1
         {
         display Get a tinker tool in your backpack and hit play in the EUO window
         pause
         }
      if #findcnt < 2
         gosub MakeTinkersTools
      finditem %CurrentToolType C_ , #BackpackID
      if #findcnt < 2
         gosub MakeTwoTools
      repeat   ; CraftLoop
        finditem %CurrentResource C_ , #BackpackID
        if #findstack < %CurrentResourceCnt
           Gosub MoveResources %CurrentResource %CurrentResourceCnt %ResourceSecure #BackpackID
        Finditem %CurrentToolType C_ , #backpackid
        Set #lobjectid #findid
        Event macro 17 0
        gosub waitforgump %cwin
         if #result
           {
           gosub offsetClick %CurrentResourceXClick1 %CurrentResourceYClick1
           gosub waitforgump %cwin
            }
         if  #result
           {
           gosub offsetClick %CurrentResourceXClick2 %CurrentResourceYClick2
           gosub waitforgump %cwin
           }
         if  #result
           {
           gosub offsetClick %CurrentItemxclick1 %CurrentItemyclick1
           gosub waitforgump %cwin
           }
         if #result
           {
           gosub OffsetClick %CurrentItemxclick2 %CurrentItemyclick2
           gosub waitforgump %cwin
        if #result
           {
           If %xclick2 = 380
              {
             gosub OffsetClick %CurrentItemxclick3 %CurrentItemyclick3
             gosub waitforgump %cwin
             }
           }
  Until #result
  Gosub OffsetClick 30 450
  wait 5
Return



GOTOs are beyond evil.. if you can overcome using gotos you have a chance at creating real scripts.  Thats just my opinion thow.
Title: Re: repeat until or goto, which do you use?
Post by: Scrripty on February 07, 2010, 11:08:42 PM
Or you could do a repeat..until and sub out the craft part I'm guessing.  So instead of a goto.. it's a gosub craftloop...? :)
Title: Re: repeat until or goto, which do you use?
Post by: UOMaddog on February 08, 2010, 11:42:13 AM
This has always been my understanding of goto's:

NEVER use a goto! LoL

Anyways, the main reason is due to reliability of code. There are ways of mathematically "proving" code to be perfect (error-free) and goto's violate the math required (it's like trying to divide by zero). Granted, none of us are working as programmers for Solaris or anything like that, but it's still a good habit to have!

Most of the goto's used in scripts on this site are harmless and will not cause issues, but it's just one of those things that is SOOO easily avoided by using other methods that it just shouldn't be used. If anyone does not know how to get around using a goto, just ask and you'll learn a lot when you see the answer! Scripting is 90% preparatory thought and 10% typing correctness! LOL  ;D