How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 07:27

How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

Code: Select all

Test_Required_Parameters := IsFunc("Test") - 1

msgbox, % Test_Required_Parameters

Test()		;how to dynamically pass "Test_Required_Parameters" to the function?

Test(a, b, c, d)
{
msgbox, Test()
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 07:55

Just create an array and pass it variadically:

Code: Select all

Test_Required_Parameters := IsFunc("Test") - 1

arr := []
Loop % Test_Required_Parameters {
	arr.push("")
}

Test(arr*)		;how to dynamically pass "Test_Required_Parameters" to the function?

Test(a, b, c, d)
{
msgbox, Test()
}
Recommends AHK Studio
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 09:46

Test(arr*) seems to solve this problem!

Code: Select all

	;dynamically pass Required Parameters to the function?
	;https://www.autohotkey.com/boards/viewtopic.php?p=255146#p255146

Test_Required_Parameters := IsFunc("Test") - 1

arr := []

Loop % Test_Required_Parameters
arr[a_index] := "x_" a_index


	;the "*" bellow from "arr*" is necessary to pass the array as parameters!
	;if arr* is used, arr[1] = Parameter1, arr[2] = Parameter2, arr[3] = Parameter3, and so on!
	;if arr  is used, all the "arr" array = parameter1

Test(arr*)	;_______________ Test (Function) _____________	

Test(a, b, c, d)
{
msgbox, % "Test() - " a " - " b " - " c " - " d
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 10:30

Wow, just found out that "OnMessage()" fails if function requires more than 4 parameters!

I think the solution above could be easily implemented in "OnMessage()"?

Anyway, fail example below:

Code: Select all

	;onMessage() does not work if function requires more than 4 parameters

OnMessage(0x201, "Test")	;0x201, left mouse down

Gui, Add, Text,, Left Click anywhere in this window.

Gui, Show, w300 h200

return

GuiClose:	;______ gui close _____
ExitApp


Test(a, b, c, d, e)	;________ Test(Function) ___________
{

    ToolTip %  a " - " b " - " c " - " d " - " e
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 10:40

OnMessage only supplies 4 parameters - it cannot call a function which explicitly states that it requires 5.
Recommends AHK Studio
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 11:36

User wrote:
28 Dec 2018, 10:30
Wow, just found out that "OnMessage()" fails if function requires more than 4 parameters!
read the manual:

https://autohotkey.com/docs/commands/OnMessage.htm
Failure
Failure occurs when Function:

1. is not an object, the name of a user-defined function, or an empty string;
2. is known to require more than four parameters; or
3. in [v1.0.48.05] or older, has any ByRef or optional parameters.

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 11:55

nnnik wrote:
28 Dec 2018, 10:40
OnMessage only supplies 4 parameters - it cannot call a function which explicitly states that it requires 5.
Sometimes it can:

Code: Select all

OnMessage( 0x201, Func("Test").Bind("Hi") )   ;0x201, left mouse down
Gui, Add, Text,, Left Click anywhere in this window.
Gui, Show, w300 h200
return

GuiClose:   ;______ gui close _____
ExitApp

Test(a, b, c, d, e)   ;________ Test(Function) ___________
{
    ToolTip %  a " - " b " - " c " - " d " - " e
}
:)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 12:06

No, it calls the the boundfunc, which requires 4 parameters ;)

Cheers :fireworks: :champagne:
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 12:24

Anyway, so we can use functions with any number of parameters. Cheers :xmas:
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

28 Dec 2018, 12:37

Yes, we can bind, define default values and variadic parameters, as long as not any required parameter is omitted the function can be called, as with all function calls.

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

05 Jan 2019, 06:20

teadrinker wrote:
28 Dec 2018, 12:24
Anyway, so we can use functions with any number of parameters. Cheers :xmas:
No we cannot - we have options to supply parameters dynamically so that we can make things fit.
Recommends AHK Studio
jsong55
Posts: 263
Joined: 30 Mar 2021, 22:02

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

29 Apr 2021, 04:10

Say I want to save the position of my Gui inside an INI file and then next time I launch it, the Gui will show at that position.

Not sure how to pass extra variables using the .bind function.

Here's the basic code

Code: Select all

inifile:="C:\Script_ini.ini"
Gui, Status: add, Text, w500 r2, Hello World!
Gui, Status: show, Autosize x%x% y%y%, vet mini
OnMessage(0x03, "storeGuiLoc")
; OnMessage(0x03, Func("storeGuiLoc").Bind(,,,,scrapper_inifile)) ; this didn't work
Return

storeGuiLoc(wParam, lParam)
{
	global
    X := lParam & 0xFFFF
    Y := lParam >> 16
 /*   if A_GuiControl
        Control := "`n(in control " . A_GuiControl . ")"
*/
    ;ToolTip You are moving the Gui window #%A_Gui% at client coordinates %X%x%Y%.%Control%
	; msgbox, % x "and Y`n" y
	IniWrite, % x, % inifile, vet_mini_pos, x
	IniWrite, % y, % inifile, vet_mini_pos, y
	x:=""
	y:=""
}
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to dynamically pass the minimum required parameters returned by "IsFunc()" to functions?

29 Apr 2021, 06:53

Code: Select all

OnMessage(0x03, Func("storeGuiLoc").Bind(scrapper_inifile))

storeGuiLoc(scrapper_inifile, wParam, lParam) {
   
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Tech Stuff, Vanda_a and 368 guests