Jump to content

Recommended Posts

Posted (edited)

I created a script to do some tasks in a loop,, and inside the loop there's a msgbox that either pauses the script or jumps to the next step in the loop,,

 

everything was working well, until I decided to create a simple GUI, that divides those tasks in groups , and when the checkbox of the task is checked it will perform the task,, anyway,, it now crashes due to an "variable used without being declared" only after the said msgbox is set to jump to the next step..

I know that the position of the declaration line makes a difference,, but I tried to declare the variable everywhere,, without a luck.

 

I'm not very good at explaining,, so I'll attach the scripts,, there's some comments inside the second file ( the none working script)

Logo Finder (working).au3 Logo Finder (not working).au3

Edited by Dr_TDM
Typos
Posted

In your 2nd script, "$i" is declared either after the While loop, or in the checkForImage() func only with a local scope, so the localCenters() function doesn't "know" this variable in the $s=$i+1 instruction :)

Func test()
    $i = 1
    Msgbox(0,"1", $i)
EndFunc

test()
Msgbox(0,"2", $i)

 

Posted

Thank you for your reply,, I've tried to declare this "$i" everywhere,, with global sometimes with local sometimes, I've tried declaring it in multiple places at once,, yet it's not working..
can you tell me exactly what should I do..

 

sorry, I'm not a professional programmer, I'm very new to this

  • Developers
Posted
39 minutes ago, Dr_TDM said:

can you tell me exactly what should I do..

Read the answer again and play with the posted test script so you understand what is being said as the answer is given already. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted
3 minutes ago, Jos said:

Read the answer again and play with the posted test script so you understand what is being said as the answer is given already. ;) 

I really tried all the options that I can think of, it's been three days now 🤕

I've declared it everywhere,, no luck

as I stated before I'm not a professional programmer,, I'm actually a Dentist

 

thanks in advance

Posted

Its just like a hole in my teeth having pain now for 3 days buts its not getting fixed by itself ;-)  you have to rethink a little longer

Some tips but just like becoming a dentist also programming you are not learning in a few hours.

  • dont use global variables or at least minimize them by using parameters to functions
  • The $s parameter you can also make a textbox control where you set that value
  • Call like you use it can be just written as
checkForImage(1)  'or checkForImage(5)
  • use (function) parameters and variables with a decent understandable name

Example

Func checkForImage($startAtImage)
   for $i=$startAtImage to $ImgCount
  • Its a better habit to split in functionalities with functions and not jumping around
    • Main program
      • Show GUI
        • checkForImage(1) which returns to main gui when found
        • main gui thens asks "continue yes/no"
    • compared you jump further form checkForImage to localCenters/abroadCenters and then call back checkForImage with a new starting position, this will become spaghetti coding. 

 

Posted (edited)

As @junkew said +

change

$ImgCount = GUICtrlCreateInput("", 216, 40, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))


to

$input_ImgCount = GUICtrlCreateInput("", 216, 40, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))


and related code refering to it: GUICtrlRead() like this

$ImgCount = GUICtrlRead($input_ImgCount)

because your original code

$ImgCount = GUICtrlRead($ImgCount)

was totally wrong

Edited by Zedna
Posted (edited)
4 hours ago, junkew said:

just like becoming a dentist also programming you are not learning in a few hours

... yet IMHO it's still easier to handle than the avulsion of an impacted 38 ;)


Edit
In fact you don't even need this problematic $i variable ...
Honestly I don't know exactly the way you want to manage your script, but a concept like this using $s only should work :

Global $s = 1

Func checkForImage()
    While $s <= $ImgCount
        If GuiCtrlRead($localCentersCheckBox) = $GUI_CHECKED Then
            localCenters()
        EndIf
        If GuiCtrlRead($abroadCentersCheckBox) = $GUI_CHECKED Then
            abroadCenters()
        EndIf
    Wend
    
    Terminate()
EndFunc


Func localCenters()
    Local $search = _ImageSearch('Assets\Aknan.bmp', 1, $x, $y, 100)      ; 1-
    If $search = 1 Then
        $editQuestion= MsgBox(4+32,"Aknan", "You found Aknan Logo,, Do you want to edit??", 90)
        Select
          Case $editQuestion=6
              TogglePause()
          Case $editQuestion =7
              SoundPlay ("")
         EndSelect
         $s = $s + 1    ;  whatever the msgbox answer, increment for the next search
    EndIf   
    
    CheckForImage()  ;  to continue checking even if not $search = 1
EndFunc

 

Edited by mikell
Posted
8 hours ago, mikell said:

. yet IMHO it's still easier to handle than the avulsion of an impacted 38 ;)

 

hahaha,, you pray you don't have to pull out those impacted suckers 38 and 48, unless you already did, then you know how difficult it is...

 

regarding the script,, thank you so so much for this,, it's now working as intended,, only thing I had to move "$s=$s+1" to the first line after checkForImage(), because the way you wrote it; the counter wasn't increasing because it only increased if the _ImageSearch function came =1 otherwise it will stay at 1,, and I had to start from $s=0 not $s=1

and I also had to move the second call to the checkForImage() just before the WEnd of the same function,, because the way you wrote it,,, it only search for the first image and then calls the function again and that way it never reaches any other searches after the first one..

 

Global $s = 0

while 1

wend      ; inside is the script that runs the GUI



Func checkForImage()
    $s = $s + 1 ; whatever the msgbox answer, increment for the next search
	While $s <= $ImgCount
		If GuiCtrlRead($localCentersCheckBox) = $GUI_CHECKED Then
			localCenters()
		EndIf
		If GuiCtrlRead($abroadCentersCheckBox) = $GUI_CHECKED Then
			abroadCenters()
		EndIf
    CheckForImage() ; to continue checking even if not $search = 1
	Wend
    
    Terminate()
EndFunc


Func localCenters()
	Local $search = _ImageSearch('Assets\Aknan.bmp', 1, $x, $y, 100)      ; 1-
	If $search = 1 Then
	    $editQuestion= MsgBox(4+32,"Aknan", "You found Aknan Logo,, Do you want to edit??", 90)
	    Select
	      Case $editQuestion=6
		      TogglePause()
		  Case $editQuestion =7
			  SoundPlay ("")
	     EndSelect
	
    CheckForImage() ; to continue checking even if not $search = 1
	EndIf	
    
	
EndFunc
Posted

and thanks for everyone here for your time and efforts, I really appreciate it..

This is a very great place I learn from it everyday..

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
  • Recently Browsing   0 members

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