Help with function references Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Revvilo
Posts: 6
Joined: 17 Nov 2015, 00:30

Help with function references

03 Dec 2018, 01:38

Hi there! I'm having some issues using function references in the below code.
I've condensed it down into a small readable script, therefore this won't have much context, but it seems that if I want to call a function via reference using .Call(), I have to pass an argument, or it simply won't execute the function. Why is that?
If this script is run, the first call will show the messagebox, but the second one won't, even though an argument isn't required by the function.

Code: Select all

Class TestClass {
    TestFunction() {
        Msgbox, Function Triggered
    }
}

fnc := Func("TestClass.TestFunction")

fnc.Call(test) ; Calls and shows messagebox
fnc.Call() ; Doesn't seem to call at all
I'm sure it's probably an issue on my end, but I just can't wrap my head around it. Would really appreciate any input.
swagfag
Posts: 2922
Joined: 11 Jan 2017, 17:59

Re: Help with function references  Topic is solved

03 Dec 2018, 01:53

You need to use ObjBindMethod to create function references to class methods.

apparently not, see helgef's comment below

Code: Select all

Class TestClass {
    TestFunction(msg) {
        Msgbox % msg
    }
}

fnc := TestClass.TestFunction
fnc.Call("", "Function Triggered")
Last edited by swagfag on 03 Dec 2018, 04:57, edited 1 time in total.
Revvilo
Posts: 6
Joined: 17 Nov 2015, 00:30

Re: Help with function references

03 Dec 2018, 02:14

You're a life saver. Thank you!
Helgef
Posts: 3927
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help with function references

03 Dec 2018, 02:17

You do not need to use objbindmethod. Methods are stored by reference in the class object. You can obtain a reference directly via fnc := TestClass.TestFunction, this is recommended over using func. The issue you have is that all methods have a hidden this parameter which needs to be passed. You can read about it in the objects page in the documentation.

Cheers.
Helgef
Posts: 3927
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help with function references

04 Dec 2018, 11:56

Also note that the first parameter in fnc.Call("", "Function Triggered") is the hidden this parameter, visual example,

Code: Select all

Class TestClass {
    TestFunction(msg) {
        Msgbox % this "`n" msg
    }
}

fnc := TestClass.TestFunction
fnc.Call("abc", "Function Triggered")
swagfag
Posts: 2922
Joined: 11 Jan 2017, 17:59

Re: Help with function references

05 Dec 2018, 00:20

the only real shame is u cant use this method to pass the fn reference to a timer since it expects a bound function object, but, oh well, v1 woes
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help with function references

05 Dec 2018, 00:47

:?:

Code: Select all

#Persistent

    fnc := TestClass.Timer.Bind(TestClass)
    SetTimer, %fnc%, 2000
    
return ; end of auto-execute section

class TestClass {
    Timer() {
        Msgbox,,, Function Triggered, 1
    }
}
Helgef
Posts: 3927
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help with function references

05 Dec 2018, 03:27

It is not that settimer expects a BoundFunc, you can pass a Func as long as its minparams is 0. Methods defined within a class body always have minparams > 0. You can also implement call,

Code: Select all

#Persistent

	SetTimer, %TestClass%, 2000
    
return ; end of auto-execute section

class TestClass {
	Timer() {
		Msgbox,,, Function Triggered, 1
	}
	call(){
		this.timer()
	}
}
This will pass TestClass to the hidden this parameter of the call method.
Hi wolf_II :wave:

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Cicin, Doc16743, flyingDman, kczx3, KilliK, sikongshan, sword1st, x-tremespeed and 48 guests