Sign in to follow this  
Followers 0
DaRkf0x

Can't understand argumented variables..

8 posts in this topic

Hello everyone

Sorry if this seems pretty dumb to all of you, but i just can't understand how to make a function that uses two variables and then call that function giving them different values everytime.

Imagine I want the function to click four different places on the screen, and I want the call to define those places, while the function has only 2 variables for x and y.

something like:

Call("TargetFunction", ?? )

Func TargetFunction()
Mouseclick($Xvar,$Yvar, bla bla bla)
EndFunc

Is there any way around this? I've searched here, after reading the help files, but couldn't work it out. . .

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello DaRkf0x,

You don't have to use variable for MouseClick(), you can use the actual values in those parameters. However if you prefer the variables due to your own custom function, than just change the variables before calling the funtion

Examples:

MouseClick('left',100,200)
MouseClick('left',110,250)

;or for custom funtions:
TargetFunction(100,200)

Func TargetFunction($varX,$varY)
    MouseClick('left',$varX,$varY)
    Sleep(100)
    MouseClick('left',$varX+10,$varY+50)
EndFunc

Realm

Edit: Fixed Typos

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

DaRkf0x,

You need to use placeholder variables when you declare your function and then pass the real values in the function call. Here is a short example to show how it works:

; Pass the vales 1 & 2 to the function and show the returned value
$iReturn = _Add(1, 2)
MsgBox(0, "Adding", "1 + 2 = " & $iReturn)
; Now 2 & 2
$iReturn = _Add(2, 2)
MsgBox(0, "Adding", "2 + 2 = " & $iReturn)
; Then 3 & 2
$iReturn = _Add(3, 2)
MsgBox(0, "Adding", "3 + 2 = " & $iReturn)
; And finally 4 & 2
$iReturn = _Add(4, 2)
MsgBox(0, "Adding", "4 + 2 = " & $iReturn)

; Here is the function declaration - note the 2 placeholder variables
Func _Add($iA, $iB)

    ; These placeholder variables hold the values we passed
    Local $iTotal = $iA + $iB
    ; So $iTotal is the sum of the values we passed - which we return to show that it worked
    Return $iTotal

EndFunc

Note that you do not have to use Call - just the function name will suffice. :(

Please ask if you still are unclear. :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I see whre you're getting, and so far I've understood it!

Thank you both :graduated:

All I wanted to know was if I could assign a value to a variable while calling a function so that the variable will be used within the function with the value defined by the call.

In that case all I have to do is add the variable to the function argument in - Function Target($var) - and define it when calling the function with - Call("Target",[value]) right?

Also I came to realize I won't be needing a defined function for the particular case I'm analysing, but anyway it was a doubt I always add about Functions/Calls and it's now solved :(

Thanks :D

Share this post


Link to post
Share on other sites

DaRkf0x,

In that case all I have to do is add the variable to the function argument in - Function Target($var) - and define it when calling the function with - Call("Target",[value]) right?

Yes, except as I mentioned above you do not need to use Call - just the function name will suffice as shown in the code I posted. :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Ahhh I see that simplifies it even further :graduated:

I'm loving AutoIt more and more by the day :(

Share this post


Link to post
Share on other sites

Ahhh I see that simplifies it even further :graduated:

I'm loving AutoIt more and more by the day :(

Just to be complete: you would use Call if you have multiple functions and you want to use a variable to define the function to be performed... Something like:

If $theWeatherIsGood Then
    $funcToCall = "sunshine"
Else
    $funcToCall = "rain"
EndIf

Call($funcToCall)

Exit

Func sunshine()
    ; enjoy the beach
EndFunc   ;==>sunshine

Func rain()
    ; enjoy the Internet from your warm, cozy home :)
EndFunc   ;==>rain

Now this is actually a bad example, since in this case it would also be easier to directly call the functions:

If $theWeatherIsGood Then
    sunshine()
Else
    rain()
EndIf

... so the bottomline is: if possible, avoid Call, not only because it is more characters of code but also because it limits your options. (Read the Call() help, it doesn't support ByRef parameter passing.)

What is certain is that when you know exactly which function you want to call from which line of code, then you don't need Call.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Ah cool, so I can just use Target() instead of Call("Target"), that really saves a lot of code as in the script I'm making I tend to make a -lot- of calls.

(I like to leave the code all neat and tidy and find that isolating it in functions, particularly when looping, is the best way to keep it simple and makes it easier to debug, for me)

That really was helpful, thank you all :(

(Now onto my other topic on GUI docking which isn't working :graduated:' )

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