Moving from v1 to v2 Topic is solved

Ask gaming related questions
Elkad
Posts: 11
Joined: 27 Mar 2016, 20:11

Moving from v1 to v2

19 Dec 2023, 21:34

This works fine in v1.

F3-F8 do nothing in game (as intended).
F2 initially does nothing.
Tapping any of F3-F8 adds them to the F2 combo fire. Tapping again takes them back out.
So I can choose exactly which keys fire by hitting F2

Code: Select all

F2::

{
 
   Send {blind}{%s_F3%}{%s_F4%}{%s_F5%}{%s_F6%}{%s_F7%}{%s_F8%}
  
}
return

#UseHook
F8::
F7::
F6::
F5::
F4::
F3::
s_%A_ThisHotkey%  ?  s_%A_ThisHotkey% := ""  :  s_%A_ThisHotkey% := A_ThisHotkey
return
Doesn't work in v2, I get
"Error: Hotkey or hotstring is missing its opening brace."

How do I fix it? I've tried putting braces in various places...
User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Moving from v1 to v2  Topic is solved

19 Dec 2023, 22:52

Testing required... you may need to make adjustments since I can't test with your conditions/environment

I've added comments based on my limited understanding of v2. Read all comments first, then consult the AHK v2 docs for the final word on syntax, etc...

Code: Select all

; v2 forces global declaration of variables used across multiple hotkey/functions
; v2 forces all variables to be assigned a default value
; I doubt you had default values assigned for these before (adj as necessary)
global s_F3:=s_F4:=s_F5:=s_F6:=s_F7:=s_F8:=""

F2::
{	; v2 requires braces for multiline hotkeys (code that is not on same line as hotkey). It now acts like a function
	global s_F3,s_F4,s_F5,s_F6,s_F7,s_F8		; v2 forces global declaration or local default value assignment
	Send "{blind}{" s_F3 "}{" s_F4 "}{" s_F5 "}{" s_F6 "}{" s_F7 "}{" s_F8 "}"	; v2 requires quotes and no %
	return
}

#UseHook 1	; no longer On/Off, v2 is now True/1 or False/0
F8::
F7::
F6::
F5::
F4::
F3::
{	; v2 requires braces for multiline hotkeys (code that is not on same line as hotkey). It now acts like a function
	global s_F3,s_F4,s_F5,s_F6,s_F7,s_F8			; v2 forces global declaration or local default value assignment
	s_%A_ThisHotkey%  ?  s_%A_ThisHotkey% := ""  :  s_%A_ThisHotkey% := A_ThisHotkey
	return
}
Elkad
Posts: 11
Joined: 27 Mar 2016, 20:11

Re: Moving from v1 to v2

20 Dec 2023, 01:45

Using yours.

Looking at

Initial value of variables s_F3 to s_F8 is <blank>

Hitting keys (F3-F8) gives this, and they toggle back to blank if I hit them again.

Code: Select all

Global Variables (alphabetical)
--------------------------------------------------
A_Args: Array object {address: 0xA28AB0}
s_F3[0 of 0]:  
s_F4[2 of 3]: F4
s_F5[2 of 3]: F5
s_F6[0 of 0]:  
s_F7[0 of 0]:  
s_F8[0 of 0]:  
So it seems line 15 thru 26 work.

Hitting F2 just pops up a message box, it doesn't do anything on the next line?
Not actually Sending F4 & F5
User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Moving from v1 to v2

20 Dec 2023, 02:34

Elkad wrote:
20 Dec 2023, 01:45
Hitting F2 just pops up a message box, it doesn't do anything on the next line?
Not actually Sending F4 & F5
It sounds as though you are testing code that I posted about 10pm Tuesday. The earlier version that I posted was not sending the F3-F8 keys when pressing F2. I have updated the code since then. Have you tested the latest code yet? (it works for me)

There is also lots of notes/comments in the updated code that points out differences between v1 and v2

If the updated code is still not working for you...

Did you post the entire script or just the hotkey portion? Based on the limited code that I have seen, the variables look like simple toggles to me. If there is more to them, I would not know this without seeing the entire script. Please post any remaining code that was not included previously. It may reveal clues needed to get it working.

BTW... the script is clever...
Elkad
Posts: 11
Joined: 27 Mar 2016, 20:11

Re: Moving from v1 to v2

20 Dec 2023, 08:41

I'd copied your code (with the msgboxes) shortly after you posted it.
When I was stuck, I checked for another post here, but didn't examine your OP closely for edits.

Your update DOES work. Looks like the key bit was quotes around each variable in the Send line.
quotes inside the braces for vars, quotes outside for keynames... I feel like there is probably a rant thread about v2 elsewhere on this forum.
BTW... the script is clever...
Yes it is. I was using some terribly long thing I'd cobbled together, that wasn't reliable at all.
I posted that kludge here ~15 years ago, and someone gave me that one.
I've made minor adjustments (send method, etc) over the years, but always on v1.

Now on to my next problem, which is a followup to this one...
I want F8 to only fire every 2nd time I press F2.
But I want to try it myself first, and then come back after I come up with something that works 40% of the time, and BSODs my computer randomly.
User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Moving from v1 to v2

20 Dec 2023, 09:18

Elkad wrote:
20 Dec 2023, 08:41
Your update DOES work.
Great!
I want F8 to only fire every 2nd time I press F2.
But I want to try it myself first
Suggestion... change this line temporarily while testing and use a text editor for testing. It may save you headaches.
s_%A_ThisHotkey% ? s_%A_ThisHotkey% := "" : s_%A_ThisHotkey% := SubStr(A_ThisHotkey,2)
Last edited by andymbody on 26 Dec 2023, 16:01, edited 1 time in total.
Elkad
Posts: 11
Joined: 27 Mar 2016, 20:11

Re: Moving from v1 to v2

26 Dec 2023, 08:24

This is working. Reliably even!

Code: Select all

global s_F3:=s_F4:=s_F5:=s_F6:=s_F7:=s_F8:=skipF8:=""

F12::
{	
; v2 requires braces for multiline hotkeys (code that is not on same line as hotkey). It now acts like a function
	global s_F3,s_F4,s_F5,s_F6,s_F7,s_F8,skipF8		; v2 forces global declaration or local default value assignment
	Send "{blind}{" s_F3 "}{" s_F4 "}{" s_F5 "}{" s_F6 "}{" s_F7 "}"	; v2 requires quotes and no %
	if !skipF8
	Send "{blind}{" s_F8 "}"	;only sends if Toggle=false
	skipF8 :=!skipF8			;
	return
}

#UseHook 1	; no longer On/Off, v2 is now True/1 or False/0
F8:: 		;I want to also set Toggle=false when this key pushed
F7::
F6::
F5::
F4::
F3::
{	; v2 requires braces for multiline hotkeys (code that is not on same line as hotkey). It now acts like a function
	global s_F3,s_F4,s_F5,s_F6,s_F7,s_F8			; v2 forces global declaration or local default value assignment
	s_%A_ThisHotkey%  ?  s_%A_ThisHotkey% := ""  :  s_%A_ThisHotkey% := A_ThisHotkey
	return
}
User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Moving from v1 to v2

26 Dec 2023, 18:23

I was recently reminded of 3 points about global variables (that differ from my last post).

1. The global keyword is optional in the main section. When you assign an initial value to a variable in the main scope, it becomes global automatically.
2. You can read a global variable within a function without declaring it. But you must provide a global declaration within functions that modify the values of a global variable. Your code is modifying at least one global var in each function, so a declaration is required in those functions.
3. You can set "Assume-global" mode within a function by using the Global keyword by itself. All variables within that function will then be considered global by default unless you declare them otherwise (on an individual basis).

With all that said, I still prefer to do it the way I showed in the post on the 20th. I prefer consistency, throughout all my code.

Below you can see an example of the three points listed above

Code: Select all


; Default values are still required
s_F3:=s_F4:=s_F5:=s_F6:=s_F7:=s_F8:=""	; global keyword not required in main section, will be global automatically
skipF8 := false							; separated because this is used as a boolean

F12::
{
	global	; "Assume-global" mode - only required because of skipF8 modification
	Send "{blind}{" s_F3 "}{" s_F4 "}{" s_F5 "}{" s_F6 "}{" s_F7 "}"	; v2 requires quotes and no %
	if (!skipF8)
	Send "{blind}{" s_F8 "}"
	skipF8 :=!skipF8
	return
}

#UseHook 1
F8::
F7::
F6::
F5::
F4::
F3::
{
	global 		; can use "Assume-global" mode here to cover all vars
	s_%A_ThisHotkey%  ?  s_%A_ThisHotkey% := ""  :  s_%A_ThisHotkey% := SubStr(A_ThisHotkey,2)
	return
}
Elkad
Posts: 11
Joined: 27 Mar 2016, 20:11

Re: Moving from v1 to v2

26 Dec 2023, 20:57

If I don't plan on using it anywhere else besides that F12 function, skipF8 could also be a static just inside that function, correct?

Return to “Gaming Help (v2)”

Who is online

Users browsing this forum: No registered users and 11 guests