SaeidN

Select or Switch or If ?

16 posts in this topic

#1 ·  Posted (edited)

Hi,

I want to tell pixelsearch to search for red color shades in for example 5 different x,y,h,w of the screen. If color red was not found in these 5 area, then do something.

I wrote this, but it's working only if the first case is not found. Is select a good choice? or it's better to use switch or if or something else?

ٍEdit: all "do something"s are same function. (search for red color untill in these 5 areas, it couldnot find red color, then if it couldn't find red color, perform that 1 function)

Thanks

Select
        Case 1
            $color1 = PixelSearch(67, 614, 77, 617, 0xE62121, 10)
            If @error Then
                 do something...
            EndIf
        Case 2
            $color2 = PixelSearch(165, 614, 175, 617, 0xE62121, 10)
            If @error Then
                do something...
            EndIf
        Case 3
            $color3 = PixelSearch(265, 614, 275, 617, 0xE62121, 10)
            If @error Then
                do something...
            EndIf
        Case 4
            $color4 = PixelSearch(365, 614, 375, 617, 0xE62121, 10)
            If @error Then
                do something...
            EndIf
        Case 5
            $color5 = PixelSearch(465, 614, 475, 617, 0xE62121, 10)
            If @error Then
                do something...
            EndIf
    EndSelect

 

Edited by SaeidN

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think ContinueCase does this...

....moves on to the next Case.

Never used it though.

 

Edit or Case Else:

from the Help File

Select
        Case $iValue = 1
            MsgBox($MB_SYSTEMMODAL, "", "The first expression was True.")
        Case $sBlank = "Test"
            MsgBox($MB_SYSTEMMODAL, "", "The second expression was True")
        Case Else ; If nothing matches then execute the following.
            MsgBox($MB_SYSTEMMODAL, "", "No preceding case was True.")  ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    EndSelect
EndFunc   ;==>Example

 

Edited by l3ill

Share this post


Link to post
Share on other sites

I'm not sure if this is the cleanest option, but I found confortable doing this trough 2 Switches.

Test it and let me know if it works well ^_^

 

Local $x,$var
Local $color = "0xE62121"

Func DoSomething()
   ConsoleWrite("Doing something..."&@crlf)
EndFunc

Func DoNothing()
   ConsoleWrite("Doing nothing..."&@crlf)
EndFunc

For $x=0 to 4
   Switch $x
   Case 0
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(67, 614, 77, 617,$color,10)
   Case 1
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(165, 614, 175, 617,$color,10)
   Case 2
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(265, 614, 275, 617,$color,10)
   Case 3
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(365, 614, 375, 617,$color,10)
   Case 4
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(465, 614, 475, 617,$color,10)
   EndSwitch

   Switch @error
      Case True
      DoNothing()
      Case False
      DoSomething()
   EndSwitch
Next

 

1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

6 hours ago, l3ill said:

I think ContinueCase does this...

....moves on to the next Case.

Never used it though.

 

Edit or Case Else:

from the Help File

Select
        Case $iValue = 1
            MsgBox($MB_SYSTEMMODAL, "", "The first expression was True.")
        Case $sBlank = "Test"
            MsgBox($MB_SYSTEMMODAL, "", "The second expression was True")
        Case Else ; If nothing matches then execute the following.
            MsgBox($MB_SYSTEMMODAL, "", "No preceding case was True.")  ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    EndSelect
EndFunc   ;==>Example

 

My other problem is what should I have in front of Case? 1-2-3 ? or something else? I think pixelsearch returns 0 only (not sure)

If nothing matches the cases, I want to exit the Select. 

Edited by SaeidN

Share this post


Link to post
Share on other sites

Not in front...

see post 3 from r3dbullo88 this should give you some ideas..

and of course the Help file :P

For instance Pixel search returns:

Return Value

Success: a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y).
Failure: sets the @error flag to 1 if the color is not found.

Share this post


Link to post
Share on other sites
3 hours ago, r3dbullo88 said:

I'm not sure if this is the cleanest option, but I found confortable doing this trough 2 Switches.

Test it and let me know if it works well ^_^

 

Local $x,$var
Local $color = "0xE62121"

Func DoSomething()
   ConsoleWrite("Doing something..."&@crlf)
EndFunc

Func DoNothing()
   ConsoleWrite("Doing nothing..."&@crlf)
EndFunc

For $x=0 to 4
   Switch $x
   Case 0
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(67, 614, 77, 617,$color,10)
   Case 1
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(165, 614, 175, 617,$color,10)
   Case 2
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(265, 614, 275, 617,$color,10)
   Case 3
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(365, 614, 375, 617,$color,10)
   Case 4
      ConsoleWrite("Searching pixel in area n° "&$x+1&@crlf)
      $var = PixelSearch(465, 614, 475, 617,$color,10)
   EndSwitch

   Switch @error
      Case True
      DoNothing()
      Case False
      DoSomething()
   EndSwitch
Next

 

For this part:

 Switch @error
      Case True
      DoNothing()
      Case False
      DoSomething()

in case of True, I don't want it to do anything, So is it better to remove "Case True" part? or I can use exitloop or something similar?

All of these codes (that you wrote) are in a function.

Share this post


Link to post
Share on other sites

You can just remove Case True

1 person likes this

Share this post


Link to post
Share on other sites
24 minutes ago, r3dbullo88 said:

You can just remove Case True

It's working perfect, thanks.

Whole this function is in a loop. How can I tell the program that if you see "Case true (doing something)" then don't call that function again?

Thank you for the help 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

27 minutes ago, r3dbullo88 said:

You should add ExitLoop

While 1 
 ...
 ...
 ...
 
 Switch @error
      Case False
      DoSomething()
      ExitLoop
 EndSwitch

 

Because whole this codes are in a function, so it exits this function, but how can I do it in parent function, I want it to exit the parent funtion, How can I call this in parent funtion? something like: If thisfunc is done once, Then don't do it again.

Thanks again

Edited by SaeidN

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

The simplest method would be setting some sort of Variable to handle it.

Example:

Global $var = FALSE ;(Can be anything)

Func CrazyThings()
    $var = TRUE; We set $var to TRUE the first time the Func is called, then it will not be called anymore.
    ConsoleWrite("Crazy things happens once only.")
EndFunc

while 1
    If $var = FALSE Then ; Only call function if $var is FALSE
        CrazyThings()
    Else
        Sleep(1000)
        ConsoleWrite("Nothing happens."&@crlf)
    EndIf
wend

 

 

 

Edited by r3dbullo88

Share this post


Link to post
Share on other sites

Like this?

$FuncRun = 0
Example()
Example()
Example()

Func Example()
    If $FuncRun = 0 Then
        ; Run your Actual Function code here
        MsgBox(0,"1AndDone", "I'm Done")
    EndIf
    $FuncRun = 1
EndFunc

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

7 minutes ago, MuffinMan said:

Like this?

$FuncRun = 0
Example()
Example()
Example()

Func Example()
    If $FuncRun = 0 Then
        ; Run your Actual Function code here
        MsgBox(0,"1AndDone", "I'm Done")
    EndIf
    $FuncRun = 1
EndFunc

 

I've set a flag=0 in parent func, and in "case true" I changed the flag to 1. Then after calling this func, I wrote: if flag=1 then exitloop.

before this it was calling in loop, but with this flag it's calling 1 more time after the flag sets to 1. I don't know why.

 

Edit: I found it why, because I added "if flag=1 then exitloop" is in another loop in the parent func. So I don't know what to do with this. I wish there was a exitfunc.

Edited by SaeidN

Share this post


Link to post
Share on other sites

Post your code here, it's easier.

Share this post


Link to post
Share on other sites
2 minutes ago, r3dbullo88 said:

Post your code here, it's easier.

Func parentfunc()
    $flag = 0
    some codes...
    
    Do
        some codes...
        
        thisfunc()
        If $flag = 1 Then ExitLoop
        
        some codes...
    Until $result = 1
EndFunc

Func thisfunc()
    Local $x,$var
    Local $color = "0xE62121"

    For $x=0 to 4
       Switch $x
           Case 0
              $var = PixelSearch(67, 614, 77, 617,$color,10)
           Case 1
              $var = PixelSearch(165, 614, 175, 617,$color,10)
           Case 2
              $var = PixelSearch(265, 614, 275, 617,$color,10)
           Case 3
              $var = PixelSearch(365, 614, 375, 617,$color,10)
           Case 4
              $var = PixelSearch(465, 614, 475, 617,$color,10)
           EndSwitch

       Switch @error
           Case True
               some codes...
               $flag= 1
       EndSwitch
    Next
EndFunc

I want to run thisfunc() only once. When the flag = 1 then exit parentfunc().

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

This code will allow the parent function to run multiple times, but the child function will only run once

$flag = 0
$result = 0
parentfunc() ; Running the function 3 times to test
parentfunc()
parentfunc()

Func parentfunc()
    MsgBox(0,"Parent Func", "Parent Fumction is Running") ; Just for testing
    ;some codes...

    Do
        ;some codes...

        thisfunc()
        If $flag = 1 Then ExitLoop

        ;some codes...
        MsgBox(0,"Debug", "You should never see this text")
    Until $result = 1
EndFunc

Func thisfunc()
    Local $x,$var
    Local $color = "0xE62121"
    If $flag <> 1 Then
        MsgBox(0,"Child Function", "Child Function Will Only Run Once") ; Just for testing

        For $x=0 to 4
           Switch $x
               Case 0
                  $var = PixelSearch(67, 614, 77, 617,$color,10)
               Case 1
                  $var = PixelSearch(165, 614, 175, 617,$color,10)
               Case 2
                  $var = PixelSearch(265, 614, 275, 617,$color,10)
               Case 3
                  $var = PixelSearch(365, 614, 375, 617,$color,10)
               Case 4
                  $var = PixelSearch(465, 614, 475, 617,$color,10)
               EndSwitch

           Switch @error
               Case True
                   ;some codes...

           EndSwitch
        Next
    EndIf
    $flag= 1
EndFunc

 

Edited by MuffinMan

Share this post


Link to post
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

  • Similar Content

    • WoodGrain
      By WoodGrain
      Hi guys,
      Could someone please tell me what I'm doing wrong with this code?
      I'm trying to take user input and then run a function with the same name, but it's not calling the function.
      So in the below after pressing Shift+F8 I would enter the text (without quotes) "testFunc".
      As you can see I've also tried this with IF statements, I'm new to using Switch/Case.
      HotKeySet("+{F8}", RunManually) Func RunManually() ;Use a case statement with 1 hot key and an InputBox to manually run functions $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run") MsgBox(0, "Entered value", $funcName) ;If $funcName = testFunc Then testFunc() ;If $funcName = test2Func Then test2Func() Switch $funcName Case testFunc MsgBox(0, "Calling", "Calling Function") testFunc() MsgBox(0, "Called", "Function call finished") Case test2Func MsgBox(0, "Calling", "Calling Function") test2Func() MsgBox(0, "Called", "Function call finished") EndSwitch EndFunc While 1 ;testFunc() Sleep(1000) WEnd Func testFunc() MsgBox(0, "func running", "Seems to work!") EndFunc Func test2Func() MsgBox(0, "func 2 running", "2 Seems to work!") EndFunc Thanks!
    • ur
      By ur
      I have never used switch in AutoIT earlier.
       
      But today, when I started to implement it in my code, I am getting errors.
      So I tried to execute default example, but still getting the same error.

      Any suggestions please...
    • Miliardsto
      By Miliardsto
      Is there possibility to do it like this?
      Cause as I see compiler doesn't see If statement in case of #include functions.
      It includes these two files all time.
      If $var == True Then #include 'func/funcsByTabs/main_Moda.au3' Else #include 'func/funcsByTabs/main_Others.au3' EndIf  
    • Warriorfoe
      By Warriorfoe
      Okay so here's the problem.
      I have a script I'm testing out where I want it to search for a certain color and if it finds that certain color I want it to check for a second color. I only want it to check for the second color after checking for the first color. If it finds the first color it will check for the second color and whether or not that second color is present it will cause certain actions to be undertaken. Well at least that's what I want it to do. I want this all to be done in a loop where it's constantly checking for the first color. However, once it does find the first color and it executes one of the actions regarding the second, I want the program to quit. I'm having trouble figuring out how to nest conditional statements here. If anyone could help me out it would be much appreciated.
      HotKeySet("{ESC}", "Terminate") HotKeySet("!c", "getColor") HotKeySet("!l", "ClickPositions") Global $color Global $interval $interval = "10000" MsgBox (0, "Starting Up", "Starting, use ESC to quit, Alt + C to get color, and Alt + L to begin Test") Func Terminate() Exit 1 EndFunc Func ClickPositions() MsgBox(0, "Beginning Test", "Hope to god this works") While 1 $Coords = PixelSearch(1006, 48, 1074, 79, 0xECECEC) $1stcoords = PixelSearch(930, 621, 1066, 649, 0x8A211E) $point = MouseGetPos() $posx = Random(930, 1066) $posy = Random(621, 649) $clicklogx = Random(1076, 1093) $clicklogy = Random(38, 54) If IsArray($Coords) Then ;check to see if the first color is there If IsArray($1stcoords) Then ;if it is check for the second color and if that one is there: MouseClick( "left", $point[0], $point[1], 1) If IsArray(<>$1stcoords) Then ;if the second color is not there: MouseClick("left", $clicklogx, $clicklogy, 1, Random(1000, 2500)) Sleep(3000) MouseClick( "left" , $posx, $posy, 1, Random ( 1000 , 2500)) EndIf EndIf EndIf Sleep(2000) WEnd EndFunc While 1 Sleep(250) WEnd  
    • Masum
      By Masum
      Hi all,
      I'm going crazy while I try and work out why a fairly simply if condition isn't working. I will try and explain it clearly, in the hope someone can find out the cause. The portion of the script for these if conditions is copied in below. The first PixelGetColor func is denoted in Screenshot 1, the RED dot signifies where the color is extracted from. The second PixelGetColor func is denoted in Screenshot 2, the YELLOW dot signifies where the color is extracted from. The script is tested for 3 scenarios, where 1. no resource is found (Screenshot 1), 2. multiple resources are found (Screenshot 2), and 3. only 1 resource is found (Screenshot 3). This application is being run via RDP hence I can't access the controls, and therefore relying on the pixel colours. The issue is, for all three scenarios, only the first If condition's first result is shown, even though the actual program shows the 3 screenshots for each of the three scenarios. Any ideas where I'm going wrong?
      If PixelGetColor(697, 446, 0xF0F0F0) Then ;If resource is not found, cancel out
              Sleep(200)
              MsgBox(64, 'Error', 'Resource is not found, therefore this pool will not be saved and will cancel out. Click OK to continue.')
              _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Fail', 'I' & $ColumnIRow)
              _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Resource not found. ' & $Notes, 'J' & $ColumnIRow)
              Sleep(500)
              WinActivate('Scheduling Help')
              Sleep(500)
              Send('{ENTER}') ;Click OK
              Sleep(500)
              Send('{SHIFTDOWN}{TAB}{SHIFTUP}{ENTER}') ;Click Cancel
              Sleep(500)
              Send('{SHIFTDOWN}{TAB}{TAB}{TAB}{TAB}{SHIFTUP}{ENTER}') ;Click Close
              Sleep(500)
              WinActivate('Scheduling: DB Flex Rule Tool - Inbox Pool Routing')
              Sleep(500)
              Send('{TAB}{ENTER}') ;Click Yes
              Sleep(500)
              WinActivate('Rules for Pool')
              Sleep(500)
              Send('{TAB}{TAB}{TAB}{ENTER}') ;Click Cancel
              Sleep(500)
              MouseClick('Primary', 1259, 653, 1) ;Click Cancel
          Else
              Send('{TAB}{TAB}{DOWN}') ;Find resource
              If PixelGetColor(725, 383, 0x3399FF) Then ;If there are multiple resources, cancel out
                  Sleep(200)
                  MsgBox(64, 'Error', 'There are multiple resources, therefore this pool will not be saved and will cancel out. Click OK to continue.')
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Redo', 'I' & $ColumnIRow)
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Multiple resources. ' & $Notes, 'J' & $ColumnIRow)
                  Sleep(500)
                  WinActivate('Scheduling Help')
                  Sleep(500)
                  Send('{TAB}{TAB}{ENTER}') ;Click Cancel
                  Sleep(500)
                  Send('{SHIFTDOWN}{TAB}{TAB}{TAB}{TAB}{SHIFTUP}{ENTER}') ;Click Close
                  Sleep(500)
                  WinActivate('Scheduling: DB Flex Rule Tool - Inbox Pool Routing')
                  Sleep(500)
                  Send('{TAB}{ENTER}') ;Click Yes
                  Sleep(500)
                  WinActivate('Rules for Pool')
                  Sleep(500)
                  Send('{TAB}{TAB}{TAB}{ENTER}') ;Click Cancel
                  Sleep(500)
                  MouseClick('Primary', 1259, 653, 1) ;Click Cancel
              Else ;Select the resource
                  Send('{ENTER}') ;Select Order Physician
                  MsgBox(0, '', 'save button clicks now')
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Complete', 'I' & $ColumnIRow)
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Success. ' & $Notes, 'J' & $ColumnIRow)
                  ;Send('{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{ENTER}') ;Click Save
              EndIf
          EndIf