Jump to content

ExitLoop in a function


Oronar
 Share

Recommended Posts

I have a function that uses ExitLoop, but whenever I use it I get an error that ExitLoop can only be used from inside a For/Do/While loop. Now where I call the function from is within a loop but the function itself isn't.

Anyway to have it exit the loop I called it from?

Link to comment
Share on other sites

It's a bot for a Stargate game incase you're wondering what it's for, well basicly I have it check the chat window of the game for commands and when it finds '!Earth' it starts clicking a sequence of buttons on screen, but I have it check if anyone has said '!abort' and it stops dialing and exits the loop to where it checks to see if anyone said '!Earth' again. When I try to run it I get an error for the ExitLoop in the AbortPause function saying it needs to be within a loop.

While 2
While 1
_CommandCheck()

If $command[2] = "!Earth" Then
;Glyph 1
MouseClick ( "left", $dhd[1], $dhd[2] )
MouseClick ( "left", $dhd[1], $dhd[2] )
Sleep("500")
_AbortPause()
MouseClick ( "left", $glyphtwentysix[1], $glyphtwentysix[2] )
WinActivate("Stargate Online")
Send("Chevron one encoded{ENTER}")
WinActivate("DHD")

;Glyph 2
Sleep("1000")
_AbortPause()
MouseClick ( "left", $glyphthirty[1], $glyphthirty[2] )
WinActivate("Stargate Online")
Send("Chevron two encoded{ENTER}")
WinActivate("DHD")
(Goes on for a bit, same stuff only differen't buttons on the screen)
EndIf

WEnd
WEnd

Func _AbortPause()
Call("_CommandCheck")
If $command[2] = "!abort" Then
WinActivate("DHD")
MouseClick("left", $enter[1], $enter[2])
WinActivate("Stargate Online")
Send("Dialing sequence aborted{ENTER}")
WinClose("DHD")
ExitLoop
EndIf
EndFunc

Func _CommandCheck()
    Do
        $strings = ControlGetText("Stargate Online", "", "RICHEDIT1")
        $array = StringSplit($strings, @LF)
        $lastline = $array[0]
        $command = StringSplit($array[$lastline], ": ", 1)
    Until @error = 0
EndFunc
Link to comment
Share on other sites

i dont know if all of your code is right, based on what you have given me, but, for one, why are you using Call to use a function ? when you make a function, and then want to use it, just type it in, you dont need to use call... !!!!!!!!!!!!!!!!

second, why did you have 2 Whiles and WEnds ? i didnt check for anything else that may be causing your script to work, but that stood out, and i replaced it with only 1 While loop.. im not sure what you were trying to do there

thirdly, you didnt use a ";" to comment out the "(Goes on for a bit, same stuff only differen't buttons on the screen)" ... so i added that too

lastly, the reason you got the error was because there was no loop in your function.. if you wanted it to exit the main while loop then it wouldnt work because the function itself is its own scope and wont check for loops outside of it..

so, i dont know if this is what you want, but it runs without error:

While 1
   _CommandCheck()
   
   If $command[2] = "!Earth" Then
     ;Glyph 1
      MouseClick("left", $dhd[1], $dhd[2])
      MouseClick("left", $dhd[1], $dhd[2])
      Sleep("500")
      _AbortPause()
      MouseClick("left", $glyphtwentysix[1], $glyphtwentysix[2])
      WinActivate("Stargate Online")
      Send("Chevron one encoded{ENTER}")
      WinActivate("DHD")
      
     ;Glyph 2
      Sleep("1000")
      _AbortPause()
      MouseClick("left", $glyphthirty[1], $glyphthirty[2])
      WinActivate("Stargate Online")
      Send("Chevron two encoded{ENTER}")
      WinActivate("DHD")
     ;(Goes on For a bit, same stuff only differen't buttons on the screen)
   EndIf
WEnd

Func _AbortPause()
   _CommandCheck()
   If $command[2] = "!abort" Then
      WinActivate("DHD")
      MouseClick("left", $enter[1], $enter[2])
      WinActivate("Stargate Online")
      Send("Dialing sequence aborted{ENTER}")
      WinClose("DHD")
   EndIf
EndFunc  ;==>_AbortPause

Func _CommandCheck()
   Do
      $strings = ControlGetText("Stargate Online", "", "RICHEDIT1")
      $array = StringSplit($strings, @LF)
      $lastline = $array[0]
      $command = StringSplit($array[$lastline], ": ", 1)
   Until @error = 0
EndFunc  ;==>_CommandCheck
FootbaG
Link to comment
Share on other sites

i dont know if all of your code is right, based on what you have given me, but, for one, why are you using Call to use a function ? when you make a function, and then want to use it, just type it in, you dont need to use call... !!!!!!!!!!!!!!!!

Yeah, used it at one point like that, not sure why

second, why did you have 2 Whiles and WEnds ? i didnt check for anything else that may be causing your script to work, but that stood out, and i replaced it with only 1 While loop.. im not sure what you were trying to do there

There's two Whiles because if the ExitLoop did work it would then close the script rather then returning to the idle part of checking for a command.

lastly, the reason you got the error was because there was no loop in your function.. if you wanted it to exit the main while loop then it wouldnt work because the function itself is its own scope and wont check for loops outside of it..

Yes, I was asking if there was anyway to make it check for loops outside of then function, I should have been clearer. So it's not possible?

Edit: I got it working using some variables and an If Then in the main part of the script. Thanks anyway!

Edited by Oronar
Link to comment
Share on other sites

You want _AbortPause to exit the While loop in your main porgram? Is that what your trying to do? If that's the case, try this:

Func _AbortPause()
Call("_CommandCheck")
If $command[2] = "!abort" Then
WinActivate("DHD")
MouseClick("left", $enter[1], $enter[2])
WinActivate("Stargate Online")
Send("Dialing sequence aborted{ENTER}")
WinClose("DHD")

; Changed this line:
Return ("abort")

EndIf
EndFunc

And then in your main program, whenever you call _AbortPause, do it this way:

$var = _AbortPause ()
If $var = "abort" Then
ExitLoop
EndIf

Hope that helps.

-DRX
Link to comment
Share on other sites

You want _AbortPause to exit the While loop in your main porgram? Is that what your trying to do? If that's the case, try this:

Func _AbortPause()
Call("_CommandCheck")
If $command[2] = "!abort" Then
WinActivate("DHD")
MouseClick("left", $enter[1], $enter[2])
WinActivate("Stargate Online")
Send("Dialing sequence aborted{ENTER}")
WinClose("DHD")

; Changed this line:
Return ("abort")

EndIf
EndFunc

And then in your main program, whenever you call _AbortPause, do it this way:

$var = _AbortPause ()
If $var = "abort" Then
ExitLoop
EndIf

Hope that helps.

Yep, that's just about what I did and it works. Thanks anyway.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...