Jump to content

Can't make my script loop..


Recommended Posts

Hello again

I've been trying to make my function to loop but it just doesn't work. The script works if I run it the first time but it won't loop like I've been trying to make it do.

The problem is in the function "retry()" and my Else command never seem to happen.

#include <INet.au3>
Global $aArray = IniReadSection(@ScriptDir & "\herolist.ini", "heros")
Global $html = _INetGetSource('http://localhost:1201')
Call("retry")

Func _FindHero($sString)
    For $i = 1 to $aArray[0][0]
        If StringInStr($sString, $aArray[$i][1]) Then Return $aArray[$i][1]
    Next
    Return "none"
EndFunc

Func retry()
   For $u = 1 to $aArray[0][0]
      If StringInStr($html, $aArray[$u][1]) Then
         SoundPlay(@WindowsDir & "\media\tada.wav", 1)

         Msgbox(0,"Success", "The hero found was: " & _FindHero($html) )

         Exit

      Else
      Sleep(30000)
call("retry")
      EndIf
      Next
EndFunc

 

Link to comment
Share on other sites

There are actually quite some problems in your retry() function:

  1. You call the _FindHero() function from within the MsgBox, which makes it just display the return value of the _FindHero in the "success" MsgBox, even if it failed.
    Solution: assign the value of the _FindHero($html) call to a variable, then decide between success and failure based on that value.
  2. The retry() function calls itself. That is called recursion, and you should not use it in this way because if a function calls itself too many times the script will break. In fact, I'd say don't use it at all before you understand what the power is and what the potential problems are.
  3. Your retry() function loops through the same array as your _FindHero() function. While that is not technically an error, it's probably not what you meant.

Also, your $html is read in the beginning of the script, and never again. You seem to be waiting for an update on that localhost site, which means that you must re-read that site every time you want to check for that update.

No way to actually test your code without that localhost site and your herolist ini, but this should probably come a lot closer to what you want:

#include <INet.au3>
Global $aArray = IniReadSection(@ScriptDir & "\herolist.ini", "heros")
Call("doMainLoop")

Func _FindHero($sString)
    For $i = 1 To $aArray[0][0]
        If StringInStr($sString, $aArray[$i][1]) Then Return $aArray[$i][1]
    Next
    ; If we got here, the whole array was searched but nothing was found.
    ; If a hero was found, we will not get to this point because of the Return above.
    ; So, as a fallback return value, we will now:
    Return False ; <-- was Return "none"

EndFunc   ;==>_FindHero

Func doMainLoop() ; <-- was "retry"

    While True ; <-- loop forever, or until we explicitly make it stop

        Local $html = _INetGetSource('http://localhost:1201') ; <-- re-read the site
        Local $foundHero = _FindHero($html) ; <-- store result value in variable
        ; note that $foundHero will now be set to False if no hero found

        If Not $foundHero Then
            Sleep(30000) ; <-- $foundHero was False, so we just wait 30 seconds
                         ; ... and let the loop restart itself. No need to call
                         ; the doMainLoop() function again!
        Else
            SoundPlay(@WindowsDir & "\media\tada.wav", 1)
            MsgBox(0, "Success", "The hero found was: " & $foundHero)
            Exit
        EndIf
    WEnd ; <-- loop restarts.

EndFunc   ;==>doMainLoop

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

22 hours ago, SadBunny said:

There are actually quite some problems in your retry() function:

  1. You call the _FindHero() function from within the MsgBox, which makes it just display the return value of the _FindHero in the "success" MsgBox, even if it failed.
    Solution: assign the value of the _FindHero($html) call to a variable, then decide between success and failure based on that value.
  2. The retry() function calls itself. That is called recursion, and you should not use it in this way because if a function calls itself too many times the script will break. In fact, I'd say don't use it at all before you understand what the power is and what the potential problems are.
  3. Your retry() function loops through the same array as your _FindHero() function. While that is not technically an error, it's probably not what you meant.

Also, your $html is read in the beginning of the script, and never again. You seem to be waiting for an update on that localhost site, which means that you must re-read that site every time you want to check for that update.

No way to actually test your code without that localhost site and your herolist ini, but this should probably come a lot closer to what you want:

#include <INet.au3>
Global $aArray = IniReadSection(@ScriptDir & "\herolist.ini", "heros")
Call("doMainLoop")

Func _FindHero($sString)
    For $i = 1 To $aArray[0][0]
        If StringInStr($sString, $aArray[$i][1]) Then Return $aArray[$i][1]
    Next
    ; If we got here, the whole array was searched but nothing was found.
    ; If a hero was found, we will not get to this point because of the Return above.
    ; So, as a fallback return value, we will now:
    Return False ; <-- was Return "none"

EndFunc   ;==>_FindHero

Func doMainLoop() ; <-- was "retry"

    While True ; <-- loop forever, or until we explicitly make it stop

        Local $html = _INetGetSource('http://localhost:1201') ; <-- re-read the site
        Local $foundHero = _FindHero($html) ; <-- store result value in variable
        ; note that $foundHero will now be set to False if no hero found

        If Not $foundHero Then
            Sleep(30000) ; <-- $foundHero was False, so we just wait 30 seconds
                         ; ... and let the loop restart itself. No need to call
                         ; the doMainLoop() function again!
        Else
            SoundPlay(@WindowsDir & "\media\tada.wav", 1)
            MsgBox(0, "Success", "The hero found was: " & $foundHero)
            Exit
        EndIf
    WEnd ; <-- loop restarts.

EndFunc   ;==>doMainLoop

 

Wow, that makes so much more sense now. Thank you for the great explanation, will help me so much to advance further. Your code worked just like I wanted it to.

Much appreciated

Link to comment
Share on other sites

2 hours ago, AliOzturk said:

Wow, that makes so much more sense now.

Yes? Feel like the blindfold was taken off? :) Awesome.

 

2 hours ago, AliOzturk said:

Much appreciated

No problem B)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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...