Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Alternatives to goto?


  • Please log in to reply
4 replies to this topic
frankomisko
  • Members
  • 13 posts
  • Last active: Dec 03 2015 11:17 PM
  • Joined: 29 Apr 2013

My scripts usually need to jump around a lot and i get the "goto/gosub cannot go to a block that doesnt enclose it" error a lot because of it.

I basically need a simple way of jumping to different blocks which i can have anywhere in the script~

an example being:

attack:
pixelsearch... ;to find a monster
mouseclick, left %x% %y% ;attacks
goto, checkhp
return

checkhp:
pixelsearch... ;checks hp bar level
if errorlevel ;fails to find hp at proper level
goto potionsearch
else 
sleep, 500
goto, combatcheck
return

potionsearch:
pixelsearch... ;checks inventory for potion
if errorlevel
click... ;teleports away, out of potions
else
click %x% %y% ;uses available potion in inventory
goto, checkhp
return

combatcheck:
pixelsearch... ;checks if monster is dead yet
if errorlevel ;if dead finds new monster
goto, attack
else
sleep, 500
goto, checkhp
return

im pretty new so my scripts arent very organized so if anyone has advice for making them cleaner/more efficient id appreciate the help



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

I don't know this error: "goto/gosub cannot go to a block that doesnt enclose it"

 

Can you give a sample script that pops up that error?

 

As for organizing your code, here's how I would rewrite yours using blocks and Loops

 

I have left the labels intact so you understood where your code moved.

attack:
Loop
{
   pixelsearch... ;to find a monster
   mouseclick, left %x% %y% ;attacks
  
   checkhp: 
   Loop
   {
      pixelsearch... ;checks hp bar level
      if errorlevel ;fails to find hp at proper level
      {
         potionsearch:
         pixelsearch... ;checks inventory for potion
         if errorlevel
         click... ;teleports away, out of potions
         else
         click %x% %y% ;uses available potion in inventory
      }
      else
          sleep, 500
      combatcheck:
      pixelsearch... ;checks if monster is dead yet
      if errorlevel ;if dead finds new monster
         Break ; break the inner loop. Jumps itself back up to the "to find a monster" pixelsearch
      else
      sleep, 500
   }
}
return ; technically this return would never be reached with this script


frankomisko
  • Members
  • 13 posts
  • Last active: Dec 03 2015 11:17 PM
  • Joined: 29 Apr 2013

The error text is: Error: A Goto/Gosub mus not jump into a block that doesn't enclose it.

found in this~

.::
lestart:
checkinv4ess:
pixelsearch, ix, iy, 1907, 1296, 1946, 1326, 0xFAFAF1, 3, fast
if errorlevel
{
    pixelsearch, rx, ry, 805, 483, 1765, 1134, 0x383335, 3, fast
    mouseclick, left, %rx%, %ry%
    sleep, 2000
    goto, lestart
    }
else
{
look4vortex:
pixelsearch, fx, fy, 805, 483, 1765, 1134, 0x6768B5, 3, fast
if errorlevel
    {
    sleep, 1000
    goto, look4vortex
    }
else
{
    mouseclick, left, %fx%, %fy%
    sleep, 1000
    checkxp:
    imagesearch, xpx, xpy, 1239, 470, 1266, 63, 0x1B1B1B, 2, fast
    if errorlevel
    {
        goto, lestart
    }
    else
    {
        sleep, 1000
        goto, checkxp
    }
}
mouseclick, left, %fx%, %fy%
sleep, 1000
goto, checkxp
}

return

it seems to always occur in scripts where that goto would be pretty essential ><



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Yeah, trying to move over to the Loop commands and using Break to move out of it should work best in your situation. It's the use of If/Else that GoTo isn't liking so much, because it is jumping into a block associated with an else statement. Maybe Lexikos or a designer can explain why that's such a problem (if it's not already in the documentation), but regardless, here's that code reworked:

 

I introduce the Continue command as well here.

.::
Loop ; lestart and checkinv4ess
{
pixelsearch, ix, iy, 1907, 1296, 1946, 1326, 0xFAFAF1, 3, fast
if errorlevel
    {
    pixelsearch, rx, ry, 805, 483, 1765, 1134, 0x383335, 3, fast
    mouseclick, left, %rx%, %ry%
    sleep, 2000
    Continue ; this goes back to the beginning of the loop. Goes back to where lestart would be
    }
else
    {
    Loop ; look4vortex
        {
        pixelsearch, fx, fy, 805, 483, 1765, 1134, 0x6768B5, 3, fast
        if errorlevel
            {
            sleep, 1000
            Continue ; technically could be omitted, as the loop will go back to look4vortex as there's no other code to execute.
            }
       else
           {
           mouseclick, left, %fx%, %fy%
           sleep, 1000
           Break ; ends this loop, goes to the next line of code which is the checkxp loop
           }
       }
   Loop ; this is the checkxp
       {
       sleep, 1000
        imagesearch, xpx, xpy, 1239, 470, 1266, 63, 0x1B1B1B, 2, fast
        if errorlevel
           {
           Break ; exits this loop, reaches the end of the outer loop and goes back to the top. Thus starting at the first pixelsearch, or where lestart is
           }
       else
          {
          sleep, 1000
         Continue
          }
       }
    }
;mouseclick, left, %fx%, %fy%
;sleep, 1000
;goto, checkxp
; those three lines could never be executed in your original code's intentions. Either you went back to lestart or you went back to checkxp before you could get here.
}
return

Edit: Whoops, I left out a bracket which may have messed things up. Should be fixed now.



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

The best solution would be to use functions, for cleanliness of code (to be able to edit it when/if necessary).


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++