SetTimer behavior inside classes Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
KuroiLight
Posts: 261
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

SetTimer behavior inside classes

17 Nov 2017, 22:33

I have a large bit of code that relies on the use of timers in classes and it was working just fine up til recently and I can't really find a cause, but I've been able reproduce it in a reduced test.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Semantically this seems correct based on Example#4 from the SetTimer doc, but addTick never runs.
Any ideas?
Last edited by KuroiLight on 17 Nov 2017, 22:55, edited 1 time in total.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
User avatar
Gio
Posts: 228
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: SetTimer behavior inside classes  Topic is solved

17 Nov 2017, 22:55

Hello KuroiLight.

This

Code: [Select all] [Download] GeSHi © Codebox Plus

this.tick := tick := ObjBindMethod(this, "Test.addTick")


Should go like

Code: [Select all] [Download] GeSHi © Codebox Plus

this.tick := tick := ObjBindMethod(this, "addTick")


And tickCount should be set to 0 at first (null + 1 is NOT 1).

Final code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Best wishes.
"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!
User avatar
KuroiLight
Posts: 261
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

Re: SetTimer behavior inside classes

17 Nov 2017, 22:57

That did it, Strange that ObjBindMethod takes only the function name and not the class name too, guess I still don't quite understand how classes are implemented. Thanks.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]



A_AhkUser
Posts: 626
Joined: 06 Mar 2017, 16:18
Location: France

Re: SetTimer behavior inside classes

17 Nov 2017, 23:38

Hi KuroiLight,

You can also use alternatively:

Code: [Select all] [Download] GeSHi © Codebox Plus

this.addTick.bind(this)


(but in this case, as one can see, it is necessary to explicitly pass the first hidden parameter named this, proper to each method, containing generally a reference to the instance).

Also note that, if later you intend to use a __Delete meta-function, you'll have to free stuck references:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Video Game Reader
Image
github
demo
User avatar
KuroiLight
Posts: 261
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

Re: SetTimer behavior inside classes

18 Nov 2017, 15:19

A_AhkUser wrote:You can also use alternatively:

Code: [Select all] [Download] GeSHi © Codebox Plus

this.addTick.bind(this)

Ahk has some confusing rules for references and objects, Wasn't aware you could act directly on the function like that, too used to C# and Lua style objects.
I like ObjBindMethod(this, "functionname") better though, its meaning is more explicit.

A_AhkUser wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

i.free() ; so that the line below triggers the __Delete meta-function
i := ""

Ah, Yea this is something that should be expanded on in the docs, I assumed you could nop an objects properties in __Delete itself and not have to worry about it.

so if I have __Delete like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

...
__Delete() {
prop1 := ""
return ""
}
...
myObj := myObj.__Delete()

It would technically call __Delete twice?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


it does twice.
but this doesn't at all:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


I could do this to dispose of objects, seems to work:

Code: [Select all] [Download] GeSHi © Codebox Plus

DeleteObject(ByRef object) {
for i, v in object {
object[i] := ""
}
object := ""
}
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]



A_AhkUser
Posts: 626
Joined: 06 Mar 2017, 16:18
Location: France

Re: SetTimer behavior inside classes

18 Nov 2017, 21:21

Actually __Delete method can be called like any other one and can even be a buit-in or user-defined function:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



In your second example the __Delete meta-func is not called since the last reference to the instance hasn't been released (this.delete_ref := "") beforehand - compare:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



That's also the reason why DeleteObject doesn't work in the example below unless you release the function reference set as hotkey label:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Video Game Reader
Image
github
demo

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, Class1, DarkKitten, geekgarage, Rastahammer and 45 guests