Bite-sized AutoHotkey Guide: Toggles

Status
Not open for further replies.

Cheesy

Gold Member
Joined
Jun 1, 2012
Messages
2,101
Likes
157
IG Name
Brian Johnsson
#1


Bite-sized AutoHotkey guide: Toggles


Introduction
This is the first of hopefully many step-by-step guides on AutoHotkey, the bit more advanced part of it to be more precise. My focus will be to try bring more variety and ease of use to people who use AutoHotkey, as well as to hopefully show why it's the one and only key binder one should use. All along I will keep this as simple as possible and try to stick to the topic as best as I can. Before you read this, look up Fleurs guide on the basics of AutoHotkey, as this will be at the limits of advanced.

This guide covers usage of toggles; meaning to press a button for event 1 to happen, then press it again for event 2 to happen. Try to experiment with these yourself, there's many different usages I can think of. Some examples are found throughout the text.





The two types of toggles
In this guide we will make use of two types, or two ways to make toggles. These two are usually referred to as integer and boolean. Anyone who's the slightest familiar with a programming language (most languages at least) will know these two words. The first one, integer is a data type which can hold values consisting of well, integers. For those who do not know, integers are numbers. Plain and simple, numbers like 1, 6, 15, 91, etc. Boolean is the easier data type. Boolean is weird and hard word for a data type that can simply hold two different values. True (1) and false (0). Now before you click this away because it seems hard ("Like, wtf is up with these words - and programming languages?! I didn't sign up for that!") these are two simple data types. They are so extremely easy but the names makes you believe they're hard. Just follow along and you'll understand what I mean! (I mean, this is after all a step-by-step guide.)


Boolean
Just for the ease of it, let's start easy with boolean variables. Boolean is a data type that can only carry two values. True and false. This is great as it allows us to easily without creating a mess to a script, toggle between two actions. For example this is great for a weapon draw/holster bind. The first press draws your weapon, the second holsters it again. This way you can keep it to a single rather than two keys.

To create a boolean toggle, we first need to declare for AutoHotkey that we want a data type of the form - boolean. In AutoHotkey this can easily be done by typing a name for the variable then assigning a value to it. There's no need to declare for AutoHotkey that we want to make a boolean variable else than typing "true" or "false", unlike many other languages.

I advise you to give the variable the default value of false, as it makes more sense as e.g. equipped gear usually starts of as not-drawn (Look below to get a better idea of what I mean.) As a general tip, always declare all variables at the top of your script. This makes is SO much easier to change later down the road as well as it keeps AutoHotkey from reading things the way you don't want.

Example on how your AutoHotkey file could look, with a variable named "GunDrawn" given the value "false". (I use this to draw/holster my weapon and so the name: GunDrawn. (See where I'm getting at with that false is a more logical value?))
Code:
GunDrawn = false
AutoHotkey knows from the equal (=) sign that we're declaring and giving a value to a variable. It automatically understands its a boolean variable from "false", as false is a unique value to boolean.


Now that we've declared the variable we will continue onto using it for one of our binds. I will use it for a draw/holster weapon bind. To use it, we're using what's called "if" and "else" statements. Pretty much, we ask AutoHotkey to check if GunDrawn is false (Which it is, as we gave it the value "false" in the beginning.) and if it is, it will perform an action. If GunDrawn isn't false, AutoHotkey will proceed to "else". As you might guess, it will then perform the action we assign to it. Rather than trying to explain it in words, view the example below.

Code:
GunDrawn = false

;Weapon Draw Bind
Numpad7::
if GunDrawn = false
{
Sendinput t/ame Unholsters his pistol and turns the safety off.{enter}
GunDrawn = true
return
}
else{
Sendinput t/ame Turns the safety on and holsters his pistol.{enter}
GunDrawn = false
return
}
Once again, this might look hard, but it really isn't. I start off by declaring the key to use for the bind just like normal. After which I start the "if" statement. As GunDrawn is false from the start, it will play the first code block (The area within the {}'s - it's vital that you use them so AutoHotkey can read it (Unlike normal binds where you can use "return" only instead.))[/i] For me this code block consists of the /ame which will display in game as well as "GunDrawn = true". This line (GunDrawn = true) is used to change the value of the variable so it won't keep getting stuck and repeating the "if" code block when we press the key the next time. Note how AutoHotkey only reads the variable declaration at the top of the script once, which is when it starts. We can use this to our advantage as the next time you press the button and GunDrawn is true, it will skip the "if" code block as the conditions don't match. Instead it will play the "else" code block, which works in pretty much the same way. Except for the different /ame. (Remember to change your variable back to its original state in the else statements code block as well.)

tl;dr it will toggle back and forth between "Sendinput t/ame Unholsters his pistol and turns the safety off.{enter}" and "Sendinput t/ame Turns the safety on and holsters his pistol.{enter}"


Integer
Alright, so you've mastered boolean variables and you can now make a functional toggle. What if I told you, there's another way to make toggles, one that comes with more flexibility and customization? Well, integer variables are just this. Rather only being able to hold two values like boolean, integer (Int for short) can have unlimited different values. As many number there are, as many different values integer can hold. The principles are still same however. One variable can only one one value at a time.

Integers are in nearly the exact same ways as boolean. You give the variable a name, then use the equal (=) sign to give it a value. There's nothing else to it. Remember that integer data type can hold numbers between 0 to infinity. As in, 0 is also a value. When it comes to computers, they unlike humans nearly always start to county at 0. (While we start at 1.)

Integers got many different uses. One of the uses I found might be the most appealing to RCRP, is how you can bind more than two actions to one button. Below I'm making a three toggle script to once again dray my pistol. Unlike last time, I this time start of by placing my hand on the firearm to 'threaten' whoever the subject is. This can be used as instead of just drawing a gun on someone, you can give them a chance to realise you're armed first. Watch below for how I do it:

Code:
GunThreat = 0
Just as before I give it a name and value. I start at 0, as it's the first possible value. Nothing keeps you from using 1 or any other value if that seems more logical to you however. Remember, you're the one who's supposed to be able to read it.


Next up we're going to use it in a script. Once again, this is very similar to the way I wrote it when using boolean values. The difference now is that I will use three different values: 0, 1, 2. Another difference is that this time I will add another statement. This statement is "if else". The way it works is that the script first looks at the conditions of the "if". If they don't match, it goes onto the next statement. If it matches the conditions I give the "if else" statement, it continues to perform that code block. Else it simply goes onto the "else" statement and performs the code I assigned to it. Watch below for an example:

Code:
Numpad6::
if GunThreat = 0
{
Sendinput t/ame Places his hand on his holstered pistol.{enter}
GunThreat = 1
return			
}
else if GunThreat = 1
{
Sendinput t/ame Draws the pistol from his holster and turns the safety off.{enter}
GunThreat = 2
return
}
else{
Sendinput t/ame Turns the safety on and holsters his pistol.{enter}
GunThreat = 0
return
}
Once again, it looks harder than it is. I start of by making an "if" statement. As the conditions match it will play the code block. In the code block, I give GunThreat a new value - 1. As the conditions don't match the next time I press the key, it will move onto the "else if" statement. The conditions match, and therefore it will perform the code block assigned to it. In the same code block, I also assign the value 2. The third time the button is pressed, neither the "if" or "if else" conditions match - as the value is now 2. Instead it will play the "else" code block, where I set the value back to 0.

The "else if" statement can be repeated an unlimited amount of times. If the first ones conditions don't match, it will move onto the next "if else" statement. Remember to include an "else" statement however, else AutoHotkey will likely not want to run the script.


As you can see, the possibilities with "if", "if else" and "else" statements are near endless. Use your imagination and I'm sure you'll find far more clever ways to utilize them than I have. Post the scripts you wrote using it below! If you need any help, you can also respond to this topic and I'll do my best to help you. If there's any interest, I can also possibly add a section about strings. For now I'm not familiar with the way AutoHotkey uses them, so I will not write regarding them until I've experimented with them more.


©Cheesy 20/04/2014
 

Cheesy

Gold Member
Joined
Jun 1, 2012
Messages
2,101
Likes
157
IG Name
Brian Johnsson
#2
Change log

20/04/2014 - 00:03
Changed some of the longer segments to explain better and to use more proper English.

20/4/2014
Even more changes throughout the day to make it easier to read and understand.

20/4/2014 - 17:35
Added a section about integer.
 

Rich

Donator
Joined
Sep 29, 2010
Messages
4,445
Likes
171
IG Name
Raymond Campbell
#3
How does it know that the variable GunDrawn is true or false? I thought variables had to be defined.
 

Cheesy

Gold Member
Joined
Jun 1, 2012
Messages
2,101
Likes
157
IG Name
Brian Johnsson
#4
Rich said:
How does it know that the variable GunDrawn is true or false? I thought variables had to be defined.

The variable is defined at the top of the script. In AutoHotkey, you never actually have to tell that it's a boolean you want to create, it understands so automatically when it reads the "true" or "false" variable. But yes, they have to be defined, the line I do it in is marked in bold:

GunDrawn = false

;Weapon Draw Bind
Numpad7::
if GunDrawn = false
{
Sendinput t/ame Unholsters his pistol and turns the safety off.{enter}
GunDrawn = true
return
}
else{
Sendinput t/ame Turns the safety on and holsters his pistol.{enter}
GunDrawn = false
return
}


It's at that line, before the rest of the script that I give the variable(s) their value. When AutoHotkey later runs the script, it only reads this once, which is on the initial read. That way I can change the variable within the code block, which allows us to make a toggle. And lastly, in some programming languages the boolean data type can be used without giving it a value directly. The value in such case equals 0, false.

And as a general tip for all: Try these examples within the
Code:
 tags for yourselves. Try getting something like notepad++ with a syntax highlighter. Else than looking cool, it will make it so much easier to read.[/b][/font]
 

Manu

Retired Admin
Joined
Apr 7, 2013
Messages
4,867
Likes
1,009
IG Name
Nona Yurbizniz
#5
I never knew AHK could do this, thanks for the tutorial on it!
 

Cheesy

Gold Member
Joined
Jun 1, 2012
Messages
2,101
Likes
157
IG Name
Brian Johnsson
#6
Manu said:
I never knew AHK could do this, thanks for the tutorial on it!
The limits of autohotkey go faaaaar beyond the simple key binds. I believe myself that AHK can be used to its limits even if you're just using it for keybind, ranging from GUI to notifications, etc. I'm planning on writing more like these, directed to those who play on RCRP. You'll see what I mean with there's so much one might have use of - even though it's believed that only basic keybinds are the way to go.
 

Manu

Retired Admin
Joined
Apr 7, 2013
Messages
4,867
Likes
1,009
IG Name
Nona Yurbizniz
#7
Well I can't wait for the int part, I've got no idea how you could include that in your code :)
 

Cheesy

Gold Member
Joined
Jun 1, 2012
Messages
2,101
Likes
157
IG Name
Brian Johnsson
#8
Manu said:
Well I can't wait for the int part, I've got no idea how you could include that in your code :)
Yeah, I'm still heavily experimenting with integers as well as strings. I'm honestly not fully used to AHK myself, I usually write Java. Things you learn I guess :)
 
Joined
Sep 13, 2014
Messages
989
Likes
72
#9
I apologise if I am breaking a rule consisting of not being able to bump threads, but I have a question.

In the following code:
GunDrawn = false

;Weapon Draw Bind
Numpad7::
if GunDrawn = false
{
Sendinput t/ame Unholsters his pistol and turns the safety off.{enter}
GunDrawn = true
return
}
else{
Sendinput t/ame Turns the safety on and holsters his pistol.{enter}
GunDrawn = false
return
}
What does the code in bold do?
 

Manu

Retired Admin
Joined
Apr 7, 2013
Messages
4,867
Likes
1,009
IG Name
Nona Yurbizniz
#10
Jcsef said:
I apologise if I am breaking a rule consisting of not being able to bump threads, but I have a question.

In the following code:
GunDrawn = false

;Weapon Draw Bind
Numpad7::
if GunDrawn = false
{
Sendinput t/ame Unholsters his pistol and turns the safety off.{enter}
GunDrawn = true
return
}
else{
Sendinput t/ame Turns the safety on and holsters his pistol.{enter}
GunDrawn = false
return
}
What does the code in bold do?
Since there's a semi-colon in front of the 'code', nothing will happen. It's just a comment about what the code underneath it is about.
 
Status
Not open for further replies.