How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Khenzy
Posts: 4
Joined: 16 Jan 2022, 04:00

How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by Khenzy » 16 Jan 2022, 04:54

Hello everyone.

Dayum this tittle may be confusing and it's going to be hard as heck to explain so here it goes:

Let's say I have three different stances or states: A, B and C.

I usually always start at state B, or rather, to understand it better, with B in the middle: A<--B-->C
Pressing the key 'z' moves the states to the left so it cycles through A<--B-->C to: B<--C-->A
Pressing the key 'x' moves the states to the right, so if we initially had A<--B-->C, now we would have: C<--A-->B

I want to set up this system in place in the script. I want the script to remember every state I'm in at all times, then I want to do as follows:
With that fixed cycling system in place I want to freely go from A to B to C or in any order by pressing the keys 1, 2 or 3, where 1 always puts me in state A, 2 in state B and 3 in state C, regardless of what state I'm currently at.
Just for reference, the state in the middle is the one I want to be at when I press either 1, 2 or 3.

Let's say I'm in state B as a standard and the script knows it, I press 1, so I want to go to state A, the script now sends the x input to go to state A, now the script knows I'm in state A.
I'm in state B but now I want to go to state C, I press 3, the script recognizes I'm in B and that I want to go to C, so it sends the z imput to cycle to state C, now the script knows I'm in state C.
Let's say I'm at state A and I want to go to C, so I press 3, it knows I'm at A so it sends the input x to cycle to the right to go to state C.
It should work with every single combination possible:
A to B -> pressing 2
B to A -> pressing 1
A to C -> pressing 3
C to A -> pressing 1
B to C -> pressing 3
C to B -> pressing 2

Basically I want to freely go from A to B to C by pressing 1, 2 or 3 respectively, but the catch is that I can only cycle through them one at a time with either z (moves to the left) or x ( moves to the right).

Any ideas on how to do this?

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

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by boiler » 16 Jan 2022, 06:48

Run this, press the 1, 2, or 3 keys, and see the result as a ToolTip that pops up next to the mouse pointer.

Code: Select all

States := ["A", "B", "C"]
Moves := {A2: "x/right", A3: "z/left", B1: "z/left", B3: "x/right", C1: "x/right", C2: "z/left"}
CurState := "B"
return

1::
2::
3::
	DesState := States[A_ThisHotkey]
	if Moves.HasKey(CurState . A_ThisHotkey)
		ToolTip, % "Moving " Moves[CurState . A_ThisHotkey] " to move from " CurState " to " DesState
	else
		ToolTip, % "Already in state " DesState
	CurState := DesState
return

Khenzy
Posts: 4
Joined: 16 Jan 2022, 04:00

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by Khenzy » 16 Jan 2022, 07:13

boiler wrote:
16 Jan 2022, 06:48
Run this, press the 1, 2, or 3 keys, and see the result as a ToolTip that pops up next to the mouse pointer.

Code: Select all

States := ["A", "B", "C"]
Moves := {A2: "x/right", A3: "z/left", B1: "z/left", B3: "x/right", C1: "x/right", C2: "z/left"}
CurState := "B"
return

1::
2::
3::
	DesState := States[A_ThisHotkey]
	if Moves.HasKey(CurState . A_ThisHotkey)
		ToolTip, % "Moving " Moves[CurState . A_ThisHotkey] " to move from " CurState " to " DesState
	else
		ToolTip, % "Already in state " DesState
	CurState := DesState
return
Woah bolier, that's very clean and elegant! Thank you!
The tooltip was a pretty nice touch.
I also liked that if you're already in a particular state, say B and you press 2 it does nothing and stays in the current state, really well thought.

Could you make it so instead of showing the tooltips, the corresponding x and z keys are registered as inputs when changing stances instead?

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

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by boiler » 16 Jan 2022, 07:39

Khenzy wrote: Could you make it so instead of showing the tooltips, the corresponding x and z keys are registered as inputs when changing stances instead?
What do you mean by “registered as inputs”? Do you mean you want it to simulate pressing the x and z keys? If so, this would do that:

Code: Select all

States := ["A", "B", "C"]
Moves := {A2: "x", A3: "z", B1: "z", B3: "x", C1: "x", C2: "z"}
CurState := "B"
return

1::
2::
3::
	DesState := States[A_ThisHotkey]
	if Moves.HasKey(CurState . A_ThisHotkey)
		Send, % Moves[CurState . A_ThisHotkey]
	CurState := DesState
return

Khenzy
Posts: 4
Joined: 16 Jan 2022, 04:00

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by Khenzy » 16 Jan 2022, 07:41

boiler wrote:
16 Jan 2022, 07:39
Khenzy wrote: Could you make it so instead of showing the tooltips, the corresponding x and z keys are registered as inputs when changing stances instead?
What do you mean by “registered as inputs”? Do you mean you want it to simulate pressing the x and a keys?
Indeed, when pressing 1, 2 or 3 it should also simulate pressing either z or x when appropiate.

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

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)  Topic is solved

Post by boiler » 16 Jan 2022, 07:44

See the above script added to my previous post. Actually, it no longer needs the if line or the DesState variable, so it can simply be:

Code: Select all

States := ["A", "B", "C"]
Moves := {A2: "x", A3: "z", B1: "z", B3: "x", C1: "x", C2: "z"}
CurState := "B"
return

1::
2::
3::
	Send, % Moves[CurState . A_ThisHotkey]
	CurState := States[A_ThisHotkey]
return

Or this:

Code: Select all

CurState := "B"
return

1::
2::
3::
	Send, % {A2: "x", A3: "z", B1: "z", B3: "x", C1: "x", C2: "z"}[CurState . A_ThisHotkey]
	CurState := ["A", "B", "C"][A_ThisHotkey]
return

Khenzy
Posts: 4
Joined: 16 Jan 2022, 04:00

Re: How to cycle between 3 different states/stances? (Hard to describe in tittle, please read the post)

Post by Khenzy » 16 Jan 2022, 08:20

boiler wrote:
16 Jan 2022, 07:44
See the above script added to my previous post. Actually, it no longer needs the if line or the DesState variable, so it can simply be:

Code: Select all

States := ["A", "B", "C"]
Moves := {A2: "x", A3: "z", B1: "z", B3: "x", C1: "x", C2: "z"}
CurState := "B"
return

1::
2::
3::
	Send, % Moves[CurState . A_ThisHotkey]
	CurState := States[A_ThisHotkey]
return

Or this:

Code: Select all

CurState := "B"
return

1::
2::
3::
	Send, % {A2: "x", A3: "z", B1: "z", B3: "x", C1: "x", C2: "z"}[CurState . A_ThisHotkey]
	CurState := ["A", "B", "C"][A_ThisHotkey]
return
Thank you so much boiler! It works flawlessly! You're an ace! :thumbup:

Post Reply

Return to “Ask for Help (v1)”