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

    • chacoya121
      By chacoya121
      can anyone help me plz
      how can you put mouseclick in if statement or is it possible
      example:
      if Mouseclick = "left" then Tooltip(" you click left mouse") thank you
    • InunoTaishou
      By InunoTaishou
      I know on other languages when you try to use a string like this
      Global $sString = "Some String" If ($sString) Then ; ... EndIf The compiler will fail because string cannot be used in an expression where it needs a bool (something along those lines). But I see instances in other languages where using $sString in an expression will work and implicitly use the length of the string (or because there is some value in that variable).
      I'm wondering why AutoIt doesn't do the same. If the $sString is used like in my example (or some other expression where it wants a bool type) implicitly use the length of the string instead of the converting to 0?
    • AlexFing17
      By AlexFing17
      Hello, i am creating  a program that 
      1. it will check if the computer is connected to the internet 
      2. will display a message if it is connected 
      3.if not connected then it will continue to check until the computer connects to the internet then it runs the function which would have been executed if the computer was connected to the internet.
      what i tried.
      i connected my computer to the internet and run the code. Which displayed the msgbox that am connected
      i later turn my internet off and run the cold , i saw it running in process list.. i waited for 5 minutes and it was still waiting for internet connection (running), but when i turned the internet on and got internet connectivity, the app just close without displaying the msg that am connected.

      Please need help where i went wrong. or if anything should be added.
       
      this is the code
      #AutoIt3Wrapper_icon=1.ico $ping = Ping("www.google.com") If Not @error Then MsgBox("", "AlexFing", "You are connect") Else Do $ping = Ping("www.google.com") Until Not @error EndIf  
    • 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...