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

#12 ·  Posted

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

#14 ·  Posted

Post your code here, it's easier.

Share this post


Link to post
Share on other sites

#15 ·  Posted

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

    • Warriorfoe
      Nesting If then Statements
      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
      If conditions not working
      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

    • david1337
      GUI Case when item is selected
      By david1337
      Hey guys
      I hope that I can get a little help with this one
       
      In this GUI example using GUIListViewEx, I have a list based on items found in test.txt.
      _____________________________________________
      item1
      item2
      item3
      ____________________________________________
      etc....
       
      When an item is selected, and I click the GetInfo button, a message will show the text of that item.
      Is it possible to activate a case like that as soon as the item is selected, so I don't need a button to start the case?
       
      #include <GUIConstantsEx.au3> #include <GUIListViewEx.au3> Global $MainGUI_ManageItemList Global $File = "test.txt" Global $FileToArray = FileReadToArray("test.txt") Call ("MainGUI_ManageItemList") Func MainGUI_ManageItemList() Local $Button1 $MainGUI_ManageItemList = GUICreate("Manage Item List", 800, 400, -1, -1) $cLV = GUICtrlCreateListView("[items]", 10, 10, 400, 775, $LVS_NOCOLUMNHEADER) GUICtrlSetFont(-1, 12, 800, 0, "@Arial Unicode MS") _GUICtrlListView_SetColumnWidth($cLV, 0, 378) $Button1 = GUICtrlCreateButton("Button 1", 425, 10, 80, 30) $RemoveItem = GUICtrlCreateButton("Remove Item", 425, 50, 80, 30) $GetInfo = GUICtrlCreateButton("GetInfo", 425, 120, 80, 30) GUISetState(@SW_SHOW, $MainGUI_ManageItemList) ; Intialise ListView Global $iLV_Index = _GUIListViewEx_Init($cLV) ; Insert lines _GUIListViewEx_Insert($FileToArray, True) ; Register required messages _GUIListViewEx_MsgRegister(True, False, False, False) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Button1 MsgBox(0,"","Button 1 is pressed") Case $RemoveItem _GUIListViewEx_Delete() Case $GetInfo $ItemSelected = _GUICtrlListView_GetSelectedIndices($cLV, True) If IsArray($ItemSelected) And $ItemSelected[0] <> 0 Then ;This part makes sure it doesn't crash when no item is selected. $ItemSelectedText = _GUICtrlListView_GetItemText($cLV, $ItemSelected[1]) msgbox (0, "Selected item", $ItemSelectedText) EndIf EndSwitch WEnd EndFunc ;==>Main  
    • ZeroByDevide
      while loop
      By ZeroByDevide
      so right now i have a small project and i wanted to print this text out every second.
      i have made a random number for the sleep function. but when i want to print out with the while loop it just doesn't work.
      Run("notepad.exe") Sleep(1000) Local $rndSleep = Int (Random(180000,240000,1000)) While $rndSleep <> 0 $rndSleep - 1 If Mod ( $rndSleep, 1000 ) == 0 Then Send("This note will show the sleeptime before closing the tabs, you got " & $rndSleep & " seconds left.") EndIf WEnd plis halp me 
    • wzc0014
      Select string of text between quotes
      By wzc0014
      Hello, 
      I am interested in looking at a chunk of text, selecting the string that is in quotes, and copying it to the clipboard. It will always have quotes at the beginning and end, start with https:// and will always contain .mp3 . But everything else is pretty random. This is an example of a piece of text
      I tried doing something like this but it didn't like the quotes. 
      $sStart = "file: "" $sEnd = ""," $aExtract = _StringBetween($sStart, $sEnd) ClipPut($aExtract)  
      Or is there a better/easier way to do this?