Jump to content

Select or Switch or If ?


SaeidN
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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
Link to comment
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.
Link to comment
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.

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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().

Link to comment
Share on other sites

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

×
×
  • Create New...