Using class instances as Function Objects Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Greno Zee
Posts: 5
Joined: 02 Jul 2022, 11:01
Contact:

Using class instances as Function Objects

Post by Greno Zee » 11 Jul 2022, 06:24

Hi all!
I'm trying to wrap my head around regular objects acting as function object in the SetTimer command and can't figure it. Any help highly appreciated.

My idea is to set a timer for every instantiated object. The responsibility of creating the timer is given to another object though. Looks something like this:

Code: Select all

class CButtonMap {
	inputButton := {}
	
	Call() {
		...
		}

	load( inputString ) {
		this.inputButton := New CInputButton( inputString, this.() )
		}
	}

class CInputButton {
	__New( inputString, onButtonPressed ) {
		; checking onButtonPressed here shows the variable being empty
		Hotkey % inputString, onButtonPressed
		}
	}
The documentation shows examples that include defining __Call as well but at the same time it explicitly states that for usage in SetTimer is the __Call method not required.

I suspect using this.() doesn't give me a proper Function Object, as well as %this%() but can't find any examples of how to actually do it.

User avatar
neovis
Posts: 34
Joined: 12 Jun 2022, 03:56
Location: Republic of Korea
Contact:

Re: Using class instances as Function Objects

Post by neovis » 11 Jul 2022, 11:12

ObjBindMethod

Code: Select all

this.inputButton := New CInputButton(inputString, ObjBindMethod(this, "Call"))
'onButtonPressed' as an expression.

Code: Select all

Hotkey % inputString, % onButtonPressed

Greno Zee
Posts: 5
Joined: 02 Jul 2022, 11:01
Contact:

Re: Using class instances as Function Objects

Post by Greno Zee » 11 Jul 2022, 19:16

Thanks for your reply, it looked very promising, unfortunately it doesn't seem to cut it. I have simplified it to this:

Code: Select all

onButtonPressed := ObjBindMethod( this , "Call" )
MsgBox % IsFunc( onButtonPressed )
and the message says 0. And indeed the consequent HotKey fails.

User avatar
neovis
Posts: 34
Joined: 12 Jun 2022, 03:56
Location: Republic of Korea
Contact:

Re: Using class instances as Function Objects

Post by neovis » 11 Jul 2022, 21:33

Greno Zee wrote:

Code: Select all

onButtonPressed := ObjBindMethod( this , "Call" )
MsgBox % IsFunc( onButtonPressed )
and the message says 0. And indeed the consequent HotKey fails.
In ahk, Bound function cannot be checked with the IsFunc function.

Try use this function:

Code: Select all

IsBoundFunc(func) {
    static p := NumGet(&Func("Func").bind(""))
    return p == NumGet(&func)
}
It works fine.

Code: Select all

func := ObjBindMethod(CButtonMap, "Method")
func.call()

class CButtonMap {
    
    Method() {
        MsgBox Called!
    }
}

Greno Zee
Posts: 5
Joined: 02 Jul 2022, 11:01
Contact:

Re: Using class instances as Function Objects

Post by Greno Zee » 12 Jul 2022, 01:59

This works indeed. Thanks for pointing this testing method out, silly I haven't figured it out myself.
I was able to confirm everything working up to this point:

Code: Select all

class CInputButton{
	activate() {
		this.onButtonPressed.Call()
		HotKey, Joy1, % this.onButtonPressed
;		HotKey, % this.inputString, % this.onButtonPressed
		active := true
		}
	}
The activate method does manage to call the Function Object. But the HotKey still fails. I have attached a screenshot of the message. Because Joy1 in my test case IS a valid first parameter, I assume the message is a demonstration of this report: "Error: Nonexistent Hotkey" when passing the hotkey command a bound func that does not exist, stating that HotKey reports the first parameter as incorrect when in fact the second one is not a callable.
Attachments
Nonexistent hotkey.png
Nonexistent hotkey.png (18.04 KiB) Viewed 465 times

User avatar
neovis
Posts: 34
Joined: 12 Jun 2022, 03:56
Location: Republic of Korea
Contact:

Re: Using class instances as Function Objects  Topic is solved

Post by neovis » 12 Jul 2022, 09:53

Greno Zee wrote:

Code: Select all

class CInputButton{
	activate() {
		this.onButtonPressed.Call()
		HotKey, Joy1, % this.onButtonPressed
;		HotKey, % this.inputString, % this.onButtonPressed
		active := true
		}
	}
The activate method does manage to call the Function Object. But the HotKey still fails.
In ahk again, Hotkey command only supports a single variable when passing a function object. I think this is weird.

so.. Use like this

Code: Select all

class CInputButton{
	activate() {
		func := this.onButtonPressed
		HotKey, Joy1, % func
	}
}
See Label of Parameters
https://www.autohotkey.com/docs/commands/Hotkey.htm#Parameters

Greno Zee
Posts: 5
Joined: 02 Jul 2022, 11:01
Contact:

Re: Using class instances as Function Objects

Post by Greno Zee » 12 Jul 2022, 10:34

Thanks for your fantastic help, this last bit did it.

Post Reply

Return to “Ask for Help (v1)”