Pass values to a SetTimer function? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 09:33

Hello good day, how can I pass the values to a SetTimer function?
Thank you and have a nice day

Code: Select all

#Requires AutoHotkey v2
#Singleinstance
Persistent 	

Tictoc := 5000
SetTimer MyTimer(Tictoc), 1000

MyTimer(x){
ToolTip x
x++
}

RussF
Posts: 1303
Joined: 05 Aug 2021, 06:36

Re: Pass values to a SetTimer function?

Post by RussF » 09 Oct 2023, 09:45

I don't think you can pass an argument, however, since you are setting a variable just before your call to SetTimer anyway, you can:

Code: Select all

#Requires AutoHotkey v2
#Singleinstance
Persistent 	

Tictoc := 5000
SetTimer MyTimer, 1000

MyTimer(){
    Global Tictoc
    ToolTip Tictoc
    Tictoc++
}
Russ

User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Re: Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 10:07

Okay, thank you

neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: Pass values to a SetTimer function?  Topic is solved

Post by neogna2 » 09 Oct 2023, 10:25

You can Bind parameters

Code: Select all

SetTimer MyTimer.Bind("hello", "world"), 1000
MyTimer(A, B){
    MsgBox A "`n" B
    ExitApp
}

User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Re: Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 10:50

Thank you very much! This is exactly what I had in mind

XMCQCX
Posts: 254
Joined: 14 Oct 2020, 23:44

Re: Pass values to a SetTimer function?

Post by XMCQCX » 09 Oct 2023, 11:01

Another option. Use an object, pass the key name as a parameter and update the values.

Code: Select all

obj := {Tictoc: 1000, Tictoc2:2000}
obj.Function := MyTimer.Bind('Tictoc')
SetTimer(obj.Function, 1000)

^1:: {
	SetTimer(obj.Function, 0)
	obj.Function := MyTimer.Bind('Tictoc2')
	SetTimer(obj.Function, 1000)
}

^2::SetTimer(obj.Function, 0)

MyTimer(param) {
	obj.%param%++
	Tooltip obj.%param%
}

User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Re: Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 11:19

Wow!
That's too complicated for me, but thanks for the suggestion.

User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Re: Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 11:30

I notice that SetTimer can be written in 2 different ways. I thought AHK2 was written to remove the ambiguity.

SetTimer(MyTimer.Bind("hello", "world"), 1000)
or:
SetTimer MyTimer.Bind("hello", "world"), 1000

RussF
Posts: 1303
Joined: 05 Aug 2021, 06:36

Re: Pass values to a SetTimer function?

Post by RussF » 09 Oct 2023, 12:17

google wrote: I notice that SetTimer can be written in 2 different ways. I thought AHK2 was written to remove the ambiguity.
That has been somewhat of an annoyance for me as well in moving to V2. I don't know why the decision was made to allow statement-style syntax for functions rather than purely function-style (with parens). Plus, it only works with select functions - Send, MsgBox, etc. For others, you must use parens. I personally stick with function-style for consistency as I was completely confused for a long time by the different syntax options within V1.

Russ

User avatar
google
Posts: 30
Joined: 16 Nov 2022, 17:19

Re: Pass values to a SetTimer function?

Post by google » 09 Oct 2023, 13:27

@Russ, thanks for the comment, it's nice to know I'm not alone.

iseahound
Posts: 1467
Joined: 13 Aug 2016, 21:04
Contact:

Re: Pass values to a SetTimer function?

Post by iseahound » 09 Oct 2023, 17:01

Actually every function can be written two ways:

Code: Select all

obj := []
obj.push "A"
and

Code: Select all

if (3 != 4)
    FAIL
    
FAIL() {
   MsgBox "Function failed"
}

User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Pass values to a SetTimer function?

Post by j46 » 28 Dec 2023, 20:47

Can I hijack this thread to ask another question, because I was so confused when I couldn't get my variables to work inside my settimer..

In the example below, you declare the Tictoc variable as global INSIDE the timer.

RussF wrote:
09 Oct 2023, 09:45
I don't think you can pass an argument, however, since you are setting a variable just before your call to SetTimer anyway, you can:

Code: Select all

#Requires AutoHotkey v2
#Singleinstance
Persistent 	

Tictoc := 5000
SetTimer MyTimer, 1000

MyTimer(){
    Global Tictoc
    ToolTip Tictoc
    Tictoc++
}
Russ

This was new to me and not the way I did it in V1 (right or not, I guess). I assigned variables as Global first thing when declaring them, and the timer (or other) would read them fine.
What is this all about ? Im a bit confused
thx

User avatar
boiler
Posts: 17328
Joined: 21 Dec 2014, 02:44

Re: Pass values to a SetTimer function?

Post by boiler » 28 Dec 2023, 21:33

j46 wrote: In the example below, you declare the Tictoc variable as global INSIDE the timer.
...

This was new to me and not the way I did it in V1 (right or not, I guess). I assigned variables as Global first thing when declaring them, and the timer (or other) would read them fine.
What is this all about ? Im a bit confused
Declaring a variable as global outside of a function no longer creates a "super-global" variable since they no longer exist in v2, as this part about Scope in Changes from v1.1 to v2.0 describes. It is described in the main part of the documentation in Global Variables.

just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Pass values to a SetTimer function?

Post by just me » 29 Dec 2023, 03:36

@j46,

as @boiler already said, AHK v2 does not support "super-global variables" any more. But you can achieve a comparable effect if you replace them with properties of an object define in the global scope:

Code: Select all

#Requires AutoHotkey v2
#Singleinstance
Persistent 	

MyGlobals := {Tictoc: 5000, OtherVar: 7000}

SetTimer MyTimer, 1000

MyTimer(){
    ToolTip MyGlobals.Tictoc
    MyGlobals.Tictoc++
}
This works because MyGlobals contains only an object reference which does not change when you assign values to properties.

User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Pass values to a SetTimer function?

Post by j46 » 29 Dec 2023, 09:54

@boiler, ive checked the links and read up a bit. Thank you. Reading the Global variables, the more familiar way seems to be using the "assume-global mode" by putting a global in the beginning of a function. I might roll with that for now as it seem to work fine.

User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Pass values to a SetTimer function?

Post by j46 » 29 Dec 2023, 10:20

@just me, interesting. Im not very familiar with objects, but I guess this might be the way to go now.

In my testing I wanted to add a value from another variable to tictoc, and sure it worked just fine:

Code: Select all

MyGlobals := {Tictoc: 5000, OtherVar: 7000}
Stepping := {MyVal: 100}

SetTimer MyTimer, 1000

MyTimer(){
    OutputDebug MyGlobals.Tictoc
    MyGlobals.Tictoc += Stepping.MyVal	; 5000, 5100, 5200..
}
...but seems a bit overkill in this case maybe.
Well, I will put objects to the end of my read up on-list for v2 then..

Thanks for the input!

just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Pass values to a SetTimer function?

Post by just me » 29 Dec 2023, 10:51

You can add all 'super-global variables' to just one object:

Code: Select all

MyGlobals := {Tictoc: 5000, Stepping: 100, OtherVar: 7000}
or

Code: Select all

MyGlobals := {}
MyGlobals.Tictoc := 5000,
MyGlobals.Stepping := 100,
MyGlobals.OtherVar := 7000
You can even add or delete properties within functions.

User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Pass values to a SetTimer function?

Post by j46 » 29 Dec 2023, 11:51

@just me wow 🤯 yeah thats true, didnt think about that. Would that be considered the "correct" way of doing it ? Seems pretty neat actually

User avatar
boiler
Posts: 17328
Joined: 21 Dec 2014, 02:44

Re: Pass values to a SetTimer function?

Post by boiler » 29 Dec 2023, 12:48

What just me showed is a clever way to simulate super-globals, and there is no “correct” way because it’s all for your convenience. If it’s easier to put them all under the same object, do that. If it helps you to group them, visualize them, or create tiers that allow the property name to be re-used across object names by using multiple objects, such as Primary.Stepping and Secondary.Stepping, then do that. It’s all up to you.

User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Pass values to a SetTimer function?

Post by j46 » 29 Dec 2023, 14:01

@boiler right

Post Reply

Return to “Ask for Help (v2)”