zuladabef 0 Posted November 6 I am rewriting a series of Funcs that I have for work and I am looking for some better ideas on how to organize and optimize my code. I have thousands of lines of code and a lot of duplication, minus a few variables. My funcs do a few basic operations, or combination of operations like: Get the coordinates on the screen MouseClick() Wait for something to appear (wait for the screen to load) I have 50+ functions that do something similar, and the only difference is that they have a slight change in the variable. Is there a better way to optimize this? Func _GetCoords_BottomCorner_ByImageSearchArea() Local $sPathOf_Image = @ScriptDir & "\images\" & "BottomRightBorderCorner.bmp" Local $iPersonal_XCoord_Offset = 0 Local $iPersonal_YCoord_Offset = 0 Local $iTop = 0 Local $iLeft = @DesktopWidth / 2 Local $iRight = @DesktopWidth Local $iBottom = @DesktopHeight Local $iTolerance = 10 Local $sCenterPos = True ;==>Where you found the image - Return Position Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) If $aCoords[0] = 1 Then Return $aCoords ElseIf $aCoords[0] = 0 Then ;~ MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2]) EndIf Return $aCoords EndFunc ;==>_GetCoords_BottomCorner_ByImageSearchArea Func _GetCoords_YearMonthCombo_ByImageSearchArea() Local $sPathOf_Image = @ScriptDir & "\images\" & "AttendanceYearMonthCombo.bmp" Local $iPersonal_XCoord_Offset = 0 Local $iPersonal_YCoord_Offset = 0 Local $iTop = 0 Local $iLeft = 0 Local $iRight = @DesktopWidth / 2 Local $iBottom = @DesktopHeight Local $iTolerance = 10 Local $sCenterPos = False ;==>Where you found the image - Return Position Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) If $aCoords[0] = 1 Then Return $aCoords ElseIf $aCoords[0] = 0 Then MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2]) EndIf Return $aCoords EndFunc ;==>_GetCoords_YearMonthCombo_Text_InmateAssignmentPage_ByImageSearchArea Share this post Link to post Share on other sites
seadoggie01 44 Posted November 6 I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time. Instead, you could wrap it once and set some default values if you wanted... something like this Func My_GetCoordsByImageSearchArea($sPathOf_Image, $iLeft = 0, $iTop = 0, $iRight = @DesktopWidth, $iBottom = @DesktopHeight, $iTolerance = 10, $sCenterPos = True) Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) Return SetError(@error, @extended, $ret) EndFunc This will give you some default values that you don't have to type every time Hide seadoggie01's signature Hide all signatures All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Share this post Link to post Share on other sites
zuladabef 0 Posted November 6 3 hours ago, seadoggie01 said: I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time. Is there a way make one function, so that the "user" can just call the Func, and the Func defines the "properties", instead of having to try and remember the filepath, X, Y, coords and stuff? Does it make sense to do something like Func My_GetCoordsByImageSearchArea($sWhatToDo) Select Case $sWhatToDo = "Look For Pixel Color" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True Case $sWhatToDo = "Look For Text" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True EndSelect Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) Return SetError(@error, @extended, $ret) EndFunc Share this post Link to post Share on other sites
iosman123 0 Posted November 8 3 On 11/7/2019 at 2:39 AM, zuladabef said: Is there a way make one function, so that the "user" can just call the Func, best cordless vacuum for hardwood floors and the Func defines the "properties", instead of having to try and remember the filepath, X, Y, coords and stuff? Does it make sense to do something like Func My_GetCoordsByImageSearchArea($sWhatToDo) Select Case $sWhatToDo = "Look For Pixel Color" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True Case $sWhatToDo = "Look For Text" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True EndSelect Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) Return SetError(@error, @extended, $ret) EndFunc thanks my issue has been fixed. Share this post Link to post Share on other sites
seadoggie01 44 Posted November 8 7 hours ago, iosman123 said: thanks my issue has been fixed. Are you lost? Hide seadoggie01's signature Hide all signatures All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Share this post Link to post Share on other sites