Togglable Script

Ask gaming related questions (AHK v1.1 and older)
1up
Posts: 1
Joined: 24 Jan 2023, 00:32

Togglable Script

Post by 1up » 24 Jan 2023, 00:36

I am attempting to make a afk script for GTA 5 casino, I was wondering how I could make it so when I press F1 this script will run until I press F1 again.

Code: Select all

#Timer
SetTimer, PressTheKey, 9000
Return

Timer:
Send, {Numpad2}
Sleep 100
Send, {Numpad5}
Sleep, 100
Send, {Enter}
Sleep, 5000
Send, {Numpad8}
Sleep, 100
Send {NumPad5}
Sleep, 100
Send, {Enter}
Return
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 24 Jan 2023, 02:04

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
F1::
If on := !on {
 SetTimer timer, 9000
 SoundBeep 1500
 timer()
} Else {
 SetTimer timer, Off
 SoundBeep 1000
}
Return

timer() {
 Send {Numpad2}
 Sleep 100
 Send {Numpad5}
 Sleep 100
 Send {Enter}
 Sleep 5000
 Send {Numpad8}
 Sleep 100
 Send {NumPad5}
 Sleep 100
 Send {Enter}
}

Code: Select all

#Requires AutoHotkey v2.0
F1:: {
 Static on := False
 If on := !on {
  SetTimer timer, 9000
  SoundBeep 1500
  timer()
 } Else {
  SetTimer timer, 0
  SoundBeep 1000
 }
}

timer() {
 Send '{Numpad2}'
 Sleep 100
 Send '{Numpad5}'
 Sleep 100
 Send '{Enter}'
 Sleep 5000
 Send '{Numpad8}'
 Sleep 100
 Send '{NumPad5}'
 Sleep 100
 Send '{Enter}'
}

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Togglable Script

Post by DuckingQuack » 29 Jan 2023, 17:49

@mikeyww So, in this example, you have an assignment used as the If statement's expression.
(1) Do assignments always perform their assigning duties no matter where you put them?
(2) In this case, the evaluation is performed before the "retroactive" assignment occurs or is it the fact that the If has taken a "snapshot" of the variable's conditions and uses those to perform the eval while the eval is changing the variables?
Thank you!
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 29 Jan 2023, 19:28

Hello,

1. Pretty much, yes. It's often just a way to use shorthand, as the same thing could be written in two lines instead of one.

Code: Select all

#Requires AutoHotkey v1.1.33
on := False
on := (!on) ; Line 1: evaluate (NOT on), and assign to "on"
If (on)     ; Line 2: If the result is neither zero nor null, execute the next statement
 Send x
Another way to look at the same thing:

Code: Select all

#Requires AutoHotkey v1.1.33
If (on := (!on))
 Send x
There, understanding is easy: inner expression is evaluated, then outer, then If.

2. The documentation answers this question.

https://www.autohotkey.com/docs/v1/Variables.htm#Operators

This shows the hierarchy or ordering of operators. ! is NOT.
The word NOT is synonymous with ! except that ! has a higher precedence.
As shown, the order of operators is always followed, unless parentheses are present; parentheses always override.

As noted on the page:
Expression Operators (in descending precedence order)
Since NOT has a higher precedence than "assign", you can now understand the line.

Since "assign" has the lowest precedence other than comma, it also explains the answer to #1.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Togglable Script

Post by DuckingQuack » 29 Jan 2023, 19:53

mikeyww wrote:
29 Jan 2023, 19:28
As noted on the page:
Expression Operators (in descending precedence order)
Thank you for pointing that out. I've referenced that list so many times but never stopped at the top to realize they were ordered by precedence. (Most of the time I've reached that page via hyperlink or the index search bar and am plopped into the middle.)

I think the part that has me searching for deeper understanding is that, if I understand correctly:
The If statement is using the assignment to ask "is ON false?" While the assignment itself says, "ON becomes the opposite" (0/1).
So my #2 question was more focused on whether the If resolved before the assignment and as I'm writing this, I realize I've answered my own question if my assumption was correct.
Therefore, I'd like to ask, will an assignment located in a function always behave this way? i.e. Assign after the function completes.
Thanks!
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 29 Jan 2023, 20:07

You can answer this question, too, yourself, by running a test script, right?

An anything in an anything else is evaluated first, not afterwards.

Code: Select all

#Requires AutoHotkey v1.1.33
a := 2
MsgBox % StrLen(a := 45 + 99)
MsgBox % a
One cannot alter how an operator works. Therefore,

Code: Select all

#Requires AutoHotkey v1.1.33
a := True
If !a := !a
 Send x
with this code, a is assigned to NOT a. Next, the first NOT operator is applied. If the result is neither zero nor null, the next statement is executed. Since a is True, NOT a is False. The NOT of the result is True. Therefore, the next line is executed.

It's as follows.

Code: Select all

#Requires AutoHotkey v1.1.33
a := True
If (!(a := (!a)))
 Send x
I often suggest running scripts yourself, to test your ideas. Why? Because I am too lazy to respond? No (OK, sometimes). It's because you can learn more that way. You are formulating your own hypotheses and seeing how to test them. You then run your test, and you have an answer quickly and in your own hands, not in someone else's hands. It's active learning instead of just reading.

How did I learn the answers to these questions at an earlier time?
1. Read the doc.
2. Ran a few scripts.
3. OK, forum helps a lot!

Everyone can do it, no magic required! Have fun. :thumbup:

Before you run the next one, take a guess at the two final values. What is the value of a? What is the value of b?

Code: Select all

#Requires AutoHotkey v1.1.33
a := 3 + b := 5
MsgBox a = %a%`n`nb = %b%

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Togglable Script

Post by DuckingQuack » 29 Jan 2023, 20:51

mikeyww wrote:
29 Jan 2023, 20:07
Before you run the next one, take a guess at the two final values. What is the value of a? What is the value of b?

Code: Select all

#Requires AutoHotkey v1.1.33
a := 3 + b := 5
MsgBox a = %a%`n`nb = %b%
Alright, I had to look up the precedence of the := and + (which I recently mastered) and found the + is higher, therefore it should resolve to a=8 and b=5... now to run the script.

I had to translate it to V2 because I disabled V1 a few weeks ago so I wouldn't use multiple syntaxes, so double check the translation is correct.

Code: Select all

#Requires AutoHotkey v2.0

Esc:: ExitApp

a := 3 + b := 5
MsgBox "a = " a "`n`n b = " b
This resolves to a=8 and b=5.
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 29 Jan 2023, 21:04

I have not been as brave about v2-- still using both versions.

Thumbs up on the answer!

How I think of it:
1. := always assigns to a variable. There is always a variable on the left, and the assignment always applies only to that variable. If that variable is part of an expression to its left, the variable assignment must occur first.
2. Therefore, b is assigned to 5 first.
3. Next, a is assigned to 3 plus that value.

You're an expert now! Time to teach others.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Togglable Script

Post by DuckingQuack » 30 Jan 2023, 05:48

mikeyww wrote:
29 Jan 2023, 21:04
I have not been as brave about v2-- still using both versions.
Thank you, but that decision was based off of the question, “should I hone my v1 knowledge that might be deprecated soon or jump into v2 and quickly surpass my v1 knowledge?” It wasn’t hard to surpass my v1 knowledge!
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 30 Jan 2023, 06:09

I think it's a good idea for new scripts to use v2 if possible. Aside from being updated as well as maintained, it's easier to use in the long run. Whether older scripts are converted may simply be a matter of time and interest!

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Togglable Script

Post by DuckingQuack » 30 Jan 2023, 06:30

My struggle with v2 so far has been the change in syntax. Using the command based v1 fit the way I process information and, as someone who isn’t a coder, made turning my thoughts into code very simple.
I feel like v2 is a completely different language but I have the benefit of the two languages using similar words and phrases much like how a native English speaker can sometimes, almost, tell what something written in Spanish says. Unfortunately, v2 would be Spanish for me in this metaphor. Just because I can get the “Spanish words” on the page doesn’t mean they’re anywhere near (syntax) grammatically correct. I’ve been reading the docs but most of it goes over my head because I lack the understanding of a coder. Even the intro section is more advanced than I feel it should be, like it was written for coders by coders to get them started quickly and easily and anyone who doesn’t understand the jargon is left in the dust. I don’t have a specific example, just that when I read the intro, I didn’t finish it feeling like I knew more than when I started.

I’m off the soapbox, now.
Thanks
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26862
Joined: 09 Sep 2014, 18:38

Re: Togglable Script

Post by mikeyww » 30 Jan 2023, 06:47

I understand that; this is a common impression, I think, as v1 requires fewer expressions, so the coder might not have to think about where to put a quotation mark or a parenthesis, for example. In addition, the documentation can be overwhelming. The bright side is that the translations (v1 to v2) themselves are often easier than imagined, and the process quickly accelerates with a small amount of practice. After doing this for a little while, I find that the v2 code becomes faster and easier to write and read, compared to v1. It has some stylistic and syntactical features that make it more natural to read. I am thinking especially about the use of output variables in v1 commands, which I always found difficult to process mentally.

FileRead, a, %b%

a := FileRead(b)

The first one is confusing, as it's constantly hard to remember which of the two parameters is the output. While for coders, it makes sense for the first parameter to be the output, from the standpoint of reading in English, the opposite is true: first, you read the file, and second, you assign the result to a variable. With the second (v2) line and syntax, all of the ambiguity disappears. You don't need to look at any documentation to understand the line. Eliminating forced expressions also enhances v2 a great deal, as expressions are used so commonly that the forced expressions and addition of % in so many places become a burden in v1 coding. The use of expressions and functions also simplifies many lines, such as MsgBox FileRead(b). Here, too, from the coding standpoint, this is very short and fast to write, and from the reading standpoint, understanding is straightforward.

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

Re: Togglable Script

Post by boiler » 30 Jan 2023, 08:13

That is an excellent example of how v2 is an improvement in inherent readability and usability. It’s something I’ve noticed when using it but never realized how striking of a contrast and what a good example it makes when placed next to its v1 counterpart. :clap:

Post Reply

Return to “Gaming Help (v1)”