How can i use static variables

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Shuu148
Posts: 39
Joined: 11 May 2023, 21:25

How can i use static variables

Post by Shuu148 » 25 May 2023, 02:53

Hi all. I need a variable that is saved here. If I press 4 the TimerOne variable should become 1 until Start_Counter is at 30, then it should become 0. Can't you use static variables here?

Code: Select all

~4::{
    static TimerOne := 0
    if(GetKeyState("4","P") and TimerOne = 0)
    {
        SetTimer Timer1, 1000
        Timer1
        MsgBox "Timer1 set"
    }
    if (GetKeyState("4","P") and TimerOne = 1)
	{
		MsgBox "Already Set"
	}

    }

Timer1() {

    TimerOne:= 1
    static Start_Counter1 := 0
    if (Start_Counter1 < 15)
    {
        Start_Counter1++
        ToolTip Start_Counter1
        SoundBeep 1200


		If Start_Counter1 = 30 {
            Start_Counter1 := 0 ;
            SetTimer Timer1, 0
            TimerOne := 0
            MsgBox "Timer1 ends"
            }
    }
}
Attachments
1.PNG
1.PNG (78.96 KiB) Viewed 1347 times

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

Re: How can i use static variables

Post by boiler » 25 May 2023, 03:15

Shuu148 wrote: Can't you use static variables here?
No, a static variable is local, so having a static variable in one function doesn’t make it visible to another.

GothicIII
Posts: 20
Joined: 17 May 2022, 04:16

Re: How can i use static variables

Post by GothicIII » 25 May 2023, 03:22

Call timer1() like this:

Code: Select all

{
TimerOne:=timer1(TimerOne)
}
edit timer1() function to pass arguments like this:

Code: Select all

timer1(TimerOne){
TimerOne:=0
Return TimerOne
}
Like boiler said, static variables are local. You have to pass the variables to each function. So in this example TimerOne from main()!=TimerOne from timer1(). Those are different variables but just share the same name.

Shuu148
Posts: 39
Joined: 11 May 2023, 21:25

Re: How can i use static variables

Post by Shuu148 » 25 May 2023, 03:42

Code: Select all

~4::{
    TimerOne := 0
    if(GetKeyState("4","P") and TimerOne = 0)
    {
        SetTimer Timer1, 1000
        TimerOne := Timer1(TimerOne)
        MsgBox "Timer1 set"
    }
    if (GetKeyState("4","P") and TimerOne = 1)
	{
		MsgBox "Already Set"
	}

    }

Timer1(TimerOne) {

    TimerOne:= 1
    static Start_Counter1 := 0
    if (Start_Counter1 < 15)
    {
        Start_Counter1++
        ToolTip Start_Counter1
        SoundBeep 1200


		If Start_Counter1 = 30 {
            Start_Counter1 := 0 ;
            SetTimer Timer1, 0
            TimerOne := 0
            MsgBox "Timer1 ends"
            }
    }
}
now i am Getting an error: invalid callback function.
Attachments
2.PNG
2.PNG (39.58 KiB) Viewed 1310 times
1.PNG
1.PNG (7.73 KiB) Viewed 1310 times

GothicIII
Posts: 20
Joined: 17 May 2022, 04:16

Re: How can i use static variables

Post by GothicIII » 25 May 2023, 03:46

You are missing the 'Return TimerOne' statement in timer1()

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

Re: How can i use static variables

Post by boiler » 25 May 2023, 03:48

Actually, explicitly calling the function as was suggested isn’t what you want to do in your case. You’re not calling the function (well, you are now, but it doesn’t make sense to do so), you’re having SetTimer call it. So you’d have to bind the parameter to it like this example.

It would probably be a lot easier for you to just use a global variable instead.

GothicIII
Posts: 20
Joined: 17 May 2022, 04:16

Re: How can i use static variables

Post by GothicIII » 25 May 2023, 03:53

Yes, boiler is right. I misunderstood the problem. Sorry for wasting your time.

Shuu148
Posts: 39
Joined: 11 May 2023, 21:25

Re: How can i use static variables

Post by Shuu148 » 25 May 2023, 04:57

Thx guys i use global variables first.

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: How can i use static variables

Post by ntepa » 25 May 2023, 13:21

You can also nest the function by putting Timer1 inside the hotkey. Example:

Code: Select all

4::{
    static TimerOne := 0

    if TimerOne = 0
    {
        SetTimer Timer1, 100
    }

    ToolTip "TimerOne: " TimerOne

    Timer1() {
        TimerOne++
    }
}

Shuu148
Posts: 39
Joined: 11 May 2023, 21:25

Re: How can i use static variables

Post by Shuu148 » 27 May 2023, 01:30

Hey Guys i have a problem. When i am using this programm it starts alot of .ahk after a while but i want only 1 .ahk for that Programm. Whats the Problem and how can i fix that?

Code: Select all

T1 := 0 ;globale Variable
 T2:= 0 ; globale Variable

~z::
{
	Reload
}

~4::
{
	if(GetKeyState("4","P") and T1 = 0)
	{
		SetTimer Timer1, 1000
		SoundPlay "A:\Timer\T1Set.mp3"
	}
	if(GetKeyState("4","P") and T1 = 1)
	{
		SoundPlay "A:\Timer\T1AlreadyExist.mp3"
	}

	Timer1()
	{
		global T1 := 1
		static Start_Counter1 := 0
		if(Start_Counter1 < 300)
		{
			Start_Counter1++
		}

			;ToolTip Start_Counter1
			;SoundBeep 0



			if(Start_Counter1=240)
			{
				SoundPlay "A:\Timer\T1In.mp3"
			}

			if(Start_Counter1 = 300)
			{
				Start_Counter1 := 0
				SetTimer Timer1, 0
				SoundPlay "A:\Timer\T1Ready.mp3"
				T1 := 0
			}
	}
}

~!4::
{
	if(GetKeyState("4","P") and T2 = 0)
	{
		SetTimer Timer2, 1000
		SoundPlay "A:\Timer\T2Set.mp3"
	}
	if(GetKeyState("4","P") and T2= 1)
	{
		SoundPlay "A:\Timer\T2Exist.mp3"
	}
	Timer2()
	{
		global T2:= 1
		static Start_Counter2 := 0
		if(Start_Counter2 < 300)
		{
			Start_Counter2++
		}

			;ToolTip Start_Counter1
			;SoundBeep 0



			if(Start_Counter2=240)
			{
				SoundPlay "A:\Timer\T2In.mp3"
			}

			if(Start_Counter2 = 300)
			{
				Start_Counter2 := 0
				SetTimer Timer2, 0
				SoundPlay "A:\Timer\T2Ready.mp34"
				T2:= 0
			}
	}

}
Attachments
1.PNG
1.PNG (27.77 KiB) Viewed 1080 times


Post Reply

Return to “Ask for Help (v2)”