Can you help out a begginer? Topic is solved

Ask gaming related questions (AHK v1.1 and older)
papagajptica

Can you help out a begginer?

06 Apr 2018, 18:01

Hello,
I am new to this coding program but I have 5 years experience of coding.
Anyway I was wondering how do I make a toggle script.
For example I want to have my XButton1 to be a toggle , when i press it the script toggles on , something like Send e so it just writes "e" .
I got to that part working , but when I press XButton1 again the script keeps on running , is there way to stop it?
Thanks!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Can you help out a begginer?

07 Apr 2018, 00:46

Code: Select all

~LButton::    ; change accordingly
    toggle := !toggle
    If (toggle)
        Send, e
    Return
Not tested.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Can you help out a begginer?

07 Apr 2018, 01:40

BoBo wrote:

Code: Select all

~LButton::    ; change accordingly
    toggle := !toggle
    If (toggle)
        Send, e
    Return
Not tested.
BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:

Code: Select all

#MaxThreadsPerHotkey 2
Toggle := 0
~LButton:: ; change accordingly
Toggle := !Toggle
If Toggle
    Send, e
Return
Okay so I'll explain.

~LButton:: is the hotkey. ~ is a modifier that prevents the native function from being blocked.

Firstly #MaxThreadsPerHotkey 2 ensures that a hotkey can be called even when it is still executing. This will be useful if your script is longer than just sending e. It doesn't matter in your cause, but if you have a long script, you need to use this.

Toggle := !Toggle sets the value of toggle to the opposite of what it was. So if toggle was true it will be turned false and vice versa. It sets the value of Toggle to !Toggle. and ! means opposite, this only works for true and false.

0 means true. 1 means false.

So we set Toggle to zero at the beginning which BoBo had forgotten. Without assigning a variable to a value, the script won't know what the opposite of the variable is.

If Toggle If *variable name*, checks whether the variable is false. If it is false then it will do an action.

In your case it was sending e. If it was not then it won't do anything. If you still want it to do something instead of nothing then add an else after Send, e.

Lastly return marks an end to the thread.
I am your average ahk newbie. Just.. a tat more cute. ;)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Can you help out a begginer?

07 Apr 2018, 07:06

Nwb wrote:BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:
May I try to correct you? (your signature says it's OK)

1. AHK allows writing short and crisp code. The way this works may be considered quirky or just helpful. Pick your favourite.
In essence, AHK does not spit your code back at you, where other interpreters/compilers would. This is undesirable for some, but not for all.
In particular, when you use a variable that has not been assigned a value, AHK gives away for free the following implicit assignment: Toggle := ""

As you can see here, there is no error and an empty string resolves to False:

Code: Select all

MsgBox, % Toggle ? "yes" : "no"
BoBo's code took the freebee, your code does not. Either way is correct, not your's alone.

2. You seem to associate #Maxthreadsperhotkey with toggling. While is is true there is a connection, it is not how you put it.
In particular: BoBo's script does not need #Maxthreadsperhotkey, it's a good example for a toogle hotkey that does not need it.
Edit: sorry i misunderstood

In my humble opinion, your "correct" version is much more elaborate then it needs to be (by using AHK's features),
and it introduces an unnecessary directive, and I would lean towards avoiding such, rather than calling "correct".
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Can you help out a begginer?  Topic is solved

07 Apr 2018, 07:24

wolf_II wrote:
Nwb wrote:BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:
May I try to correct you? (your signature says it's OK)

1. AHK allows writing short and crisp code. The way this works may be considered quirky or just helpful. Pick your favourite.
In essence, AHK does not spit your code back at you, where other interpreters/compilers would. This is undesirable for some, but not for all.
In particular, when you use a variable that has not been assigned a value, AHK gives away for free the following implicit assignment: Toggle := ""

As you can see here, there is no error and an empty string resolves to False:

Code: Select all

MsgBox, % Toggle ? "yes" : "no"
BoBo's code took the freebee, your code does not. Either way is correct, not your's alone.

2. You seem to associate #Maxthreadsperhotkey with toggling. While is is true there is a connection, it is not how you put it.
In particular: BoBo's script does not need #Maxthreadsperhotkey, it's a good example for a toogle hotkey that does not need it.
Edit: sorry i misunderstood

In my humble opinion, your "correct" version is much more elaborate then it needs to be (by using AHK's features),
and it introduces an unnecessary directive, and I would lean towards avoiding such, rather than calling "correct".
My bad but when I try to run such a script it says that the variable has not been defined. Must be because of #Warn makes sense. But I had no bad intentions I just wanted to help.
Last edited by Nwb on 07 Apr 2018, 07:27, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 49 guests