Trigger after 5 seconds, error same name as a globa Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
robozao
Posts: 4
Joined: 20 Apr 2024, 08:53

Trigger after 5 seconds, error same name as a globa

03 May 2024, 04:06

Hello,
I'm trying to create a script and I need it to do a specific action every 5 seconds.
However, apparently AHK V2 doesn't allow you to declare different values for the same variable:

Code: Select all

hs := DateAdd(A_Now, 5, "seconds")
Loop 
{ 
ha := A_Now
if (ha > hs) {
	MsgBox "ok. 5 seconds"
	hs := DateAdd(A_Now, 5, "seconds") ;reset coldown
}
}	
Error return: Specifically: local hs (same name as a global)

Please, is it possible create this script?

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

Re: Trigger after 5 seconds, error same name as a globa

03 May 2024, 04:12

That script does not have any errors, including the one you mentioned because there are no local variables (because it is all global code and no functions). Post a script that actually demonstrates the scenario where the error is encountered, and show the exact details of the error message.
robozao
Posts: 4
Joined: 20 Apr 2024, 08:53

Re: Trigger after 5 seconds, error same name as a globa

03 May 2024, 06:44

Thanks, this is a full code:

Code: Select all

#Requires AutoHotkey v2.0


hWnd1 := 0
hWnd2 := 0
CoordMode "Pixel", "Window"
hs := DateAdd(A_Now, 5, "seconds")

1::WinActivate hWnd1 ; as an example, this activates the window using the id saved to hwin1

2::WinActivate hWnd2 ; as an example, this activates the window using the id saved to hwin1

3::hWnd1 := WinGetID("A")

4::hWnd2 := WinGetID("A")

0::ExitApp

9::
{
Loop 
{

state := GetKeyState("CapsLock", "T")
if (state = "1") 
	{ 
	Break 
	}

if PixelSearch(&Px, &Py, 698, 350, 771, 420, 0xDC2121, 3) {
    Sleep 1000
	while PixelSearch(&Px, &Py, 698, 350, 771, 420, 0xDC2121, 3) {
		SoundPlay "s.wav"
		Sleep 4000
		}
}
	
if PixelSearch(&Px, &Py, 55, 88, 55, 88, 0xD6DEDE, 3) {
	SoundPlay "i.wav"
	Sleep 4000
	Break
}
While PixelSearch(&Px, &Py, 62, 101, 62, 101, 0xBDC6CE, 3) {
	SoundPlay "p.wav"
	Sleep 10000
	
}
ha := A_Now
if (ha > hs) {
	MsgBox "usar skill"
	hs := DateAdd(A_Now, 5, "seconds")
}	
}
}

The error:

Error: This variable has not been assigned a value.

Specifically: local hs (same name as a global)

047: }
048: ha := A_Now
▶ 049: If (ha > hs)
049: {
050: MsgBox("usar skill")


[Mod edit: Replaced quote tags with code tags. Please use them yourself when posting code.]
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Trigger after 5 seconds, error same name as a globa  Topic is solved

03 May 2024, 07:15

Since you assign a value to hs in the hotkey function, it can’t be assumed that it’s meant to be the global variable of the same name, so declare it as global by adding global hs as the first line inside your hotkey function.

Explained: Global Variables

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: ByronicZero, Descolada and 44 guests