Jump to content
Sign in to follow this  
CannedAnswers

Help with functions

Recommended Posts

CannedAnswers

I'm trying to clean up some code I've inherited that's just a mess. I think the biggest thing I need to do is make it use functions, but I need to be able to call up slightly different flavors of the same function, so I need to make new functions that take a handful of parameters and do similar but slightly different actions as a result. What's going wrong here for example. I'd like to set a hotkey to test one section of my code. I understand I can't pass parameters with hotkeyset so I'm calling a function that's specifically set up for a single parameter. HotkeySet ("{F10}", "SelectCrew1") Func SelectCrew1() Call("SelectCrew", 1) EndFunc Func SelectCrew($crew) If $crew = 1 Then MouseClick("Left", 190, 470, 1) Sleep(100) ElseIf $crew = 2 Then MouseClick("Left", 190, 570, 1) Sleep(100) ElseIf $crew = 3 Then MouseClick("Left", 190, 680, 1) Sleep(100) ElseIf $crew = 4 Then MouseClick("Left", 190, 800, 1) Sleep(100) Else MsgBox(0,"Error","Non-existent Crew # called") EndIf EndFunc

Share this post


Link to post
Share on other sites
NewPlaza

First off, let's tidy this up a bit.

HotKeySet("{F10}", "SelectCrew1")

Func SelectCrew1()
    Call("SelectCrew", 1)
EndFunc   ;==>SelectCrew1

Func SelectCrew($crew)
    If $crew = 1 Then
        MouseClick("Left", 190, 470, 1)
        Sleep(100)
    ElseIf $crew = 2 Then
        MouseClick("Left", 190, 570, 1)
        Sleep(100)
    ElseIf $crew = 3 Then
        MouseClick("Left", 190, 680, 1)
        Sleep(100)
    ElseIf $crew = 4 Then
        MouseClick("Left", 190, 800, 1)
        Sleep(100)
    Else
        MsgBox(0, "Error", "Non-existent Crew # called")
    EndIf
EndFunc   ;==>SelectCrew

This little bit of code exacutes then quickly exits.

What exactly are you looking for?

Share this post


Link to post
Share on other sites
Realm

Hello CannedAnswers,

First, Welcome to the AutoIt Forums!

Another approach is to use a global variable to pass your parameters. While using global variables is not generally recommended, this is a slightly cleaner way to approach your problem instead of writing several functions to declare one variable.

Write your code to change the variable as needed or add some sort of input. I have used NewPlaza's code to give you another example.

HotKeySet("{F10}", "SelectCrew")

Global $crewMembers = 2


Func SelectCrew()
    
    If $crewMembers = 1 Then
        MouseClick("Left", 190, 470, 1)
        Sleep(100)
    ElseIf $crewMembers = 2 Then
        MouseClick("Left", 190, 570, 1)
        Sleep(100)
    ElseIf $crewMembers = 3 Then
        MouseClick("Left", 190, 680, 1)
        Sleep(100)
    ElseIf $crewMembers = 4 Then
        MouseClick("Left", 190, 800, 1)
        Sleep(100)
    Else
        MsgBox(0, "Error", "Non-existent Crew # called")
    EndIf
EndFunc

You could even include the InputBox inside your function eliminating your need to pass a parameter.

Func SelectCrew()
    Local $crewMembers = $crewMembers = InputBox('Selct Crew Options', 'Enter number of crew members needed')
    If $crewMembers = 1 Then
        MouseClick("Left", 190, 470, 1)
        Sleep(100)
    ElseIf $crewMembers = 2 Then
        MouseClick("Left", 190, 570, 1)
        Sleep(100)
    ElseIf $crewMembers = 3 Then
        MouseClick("Left", 190, 680, 1)
        Sleep(100)
    ElseIf $crewMembers = 4 Then
        MouseClick("Left", 190, 800, 1)
        Sleep(100)
    Else
        MsgBox(0, "Error", "Non-existent Crew # called")
    EndIf
EndFunc

Happy Coding!

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
CannedAnswers

Sorry for my horrid formatting. How do I get the properly formatted code like you've both posted? I think it turns out my main problem was with some formatting of some form (unrelated to my formatting problems here). The code copied from NewPlaza actually compiled and ran whereas my original stuff didn't. The Second issue I had was resolved thanks to your mention the code just ended, I've copied a bit of code from the original project that basically just loops unless I've hit the hotkey to force it the actually use the function I wanted to test.

And about how clunky and nearly do nothing as this is, this is one function I wanted to test on its own. I just wanted to be able to verify by hand that when passed my various variables if the program was up to the correct point, that lone function would work correctly. The other things this code is doing means using a global variable wouldn't have been a great idea, I really needed to figure out passing parameters.

Share this post


Link to post
Share on other sites
Realm

Use the Full Editor.... the button on the toolbar that matches the AutoIt icon will start a popup where you can paste code to display as NewPlaza and I have. Another way is to use tags [ autoit] ;place code here [ /autoit]. Just delete the preceding spaces inside the brackets to actually post as tags.

;place code here

Give a more indepth example of what your trying to achieve, there maybe another solution to your project that would be better. I personally would rather change the value of one variable and call one function, rather than write several functions to call the same one. Good coding practice is about simplifying rather than having a large amount of data stored on memory.

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
CannedAnswers

When it's called in practice I will have a hotkey to start my main loop which will in turn at a handful of points call this function with one of its parameters. I just needed to test it alone, and I can't pass parameters with Hotkey. This means both the kludgy function to call a function and the hotkey to call the kludge won't be in production code.  In pseudo code, with the unwritten functions as pretty much what they describe, it looks something like this(but with crew going to 4 and menus to 6, not 2 for each).  This is pseudo code because a)I don't have my code here where I'm writing this response and b)the specifics of a lot of this really don't matter, without knowing my particular application this pseudo code is if anything more readable than my real code.

Hotkeyset("f5", start)
hotkeyset("f6", stop)
#include imagesearch

func start
 while true
 call("clearscreen")
 call("processdata")
 call("opencrew"
 call("selectcrew", 1)
 call("scannow")
 call("selectcrew", 2)
 Call("scannow")
 etc
 Sleep(30000)
wend
endfunc

func processdata()
 deals with outcome of previous scans and actions as they need to be out of the way before we scan for more
endfunc

func scannow()
 call("selectmenu", 1)
 call("findandclick")
 call("selectmenu", 2)
 call("findandclick")
 etc
endfunc

Func selectmenu
 pretty much the same type of thing as selectcrew, except we're expanding different number menus
endfunc

Func findandclick()
 uses imagesearch to locate a matching image within a predefined area, then take action on it
endfunc


while true
 sleep(1)
wend


func end
exit
endfunc

I'm going to be doing a number of other things with image recognition, and I need other functions to be able to easily navigate these menus, so passing those actions as parameters with a function that's got a human readable name that explains what i'm doing seemed the simplest way to think about it to me.  But when I'm adding these indavidual menu navigation functions I need to make sure they work as intended before putting them into a larger script,  hence the testing it on its own with a hotkey calling a kludge calling one of the subfunctions.

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
Sign in to follow this  

×