Is there a way to create something like this in AutoHotKey?
bool isReady = false;
while (!isReady) {
// Do something here
isReady = true;
}
I tried to experiment with While loop, but it ended with just 1 loop regardless of the condition I give the program. I am currently using Version 1.0.47.06.
I read the documentation here: http://www.autohotkey.com/docs/commands/While.htm I tried to give the while loop a value of true. But it only executed once. (I was expecting it to loop forever, until I terminate the script).
condition := true
while (condition)
{
MsgBox, condition
}
while (true)
{
MsgBox, true
}
Your code is correct, but the While command requires version 1.0.48+.
You should update to the latest version of AHK here - http://ahkscript.org/download/
To create a infinite loop , you can use this syntax:
Loop
{
; Your other code goes here
}
Related
I have a coroutine function written for Unity which is as below:
IEnumerator initialize_reference_att()
{
Quaternion zero = new Quaternion(0, 0, 0, 0);
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
reference_attitude = Input.gyro.attitude;
Debug.Log("Reference attitude set to " + reference_attitude);
yield return null;
}
In this way it works as expected however when I write the while loop as below:
while (Input.gyro.attitude == zero)
yield return null;
the execution of while loop ends although Input.gyro.attitude == zero condition is still true. Apparently it has something to do with returning from coroutine immediately after while loop without doing any other thing. Because when I add an debug log line(as in the first code snippet), it works as expected. Does anybody have any idea why it behaves in that way? My issue is solved but I only want to understand why it happens. Thanks everyone.
This is basically the good old missing curly braces story:
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
basically equals writing
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
=> without the { } for enclosing a scope the while only applies to the first following expression after it
what you want is
while (Input.gyro.attitude == zero)
{
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
}
Because when I add an debug log line(as in the first code snippet), it works as expected.
Let me doubt that. Your first snippet is an endless loop that the way it is written should crash your entire app as soon as the condition is true once.
In contrary actually I would expect
while (Input.gyro.attitude == zero)
yield return null;
which basically equals
while (Input.gyro.attitude == zero)
{
yield return null;
}
or also simply
yield return new WaitWhile(() => Input.gyro.attitude == zero);
would allow to yield once a frame => continue in the next frame and check again. Except something happened we all waited for and Unity suddenly made the API multi-threading ready you would always get the same value for Input.gyro.attitude during the same frame
I'm new to AutoHotKey and I wanted to create a script macro for a flash game but when I run it, it creates an error.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
condChecker := false
state := true
Mine()
{
Sleep, rand(10,80)
Send, {Space}
while(state = true)
{
PixelGetColor, gemColor, 982, 433
if(gemColor = B93557)
{
state := true
{
else(gemColor = 96885A)
{
state := false
}
Sleep, rand(90,120)
}
}
^-::
loop 10000
{
getState()
if(state = true)
{Mine()}
else
{Sleep, rand(70,150)}
}
When I press Run Script on the ahk file, a menu pops up saying
Error at line 20.
Line Text else(gemColor = 96885A)
Error: Functions cannot contain functions.
The program will now exit.
I don't know where to start with this error and I read up on other forums saying that my formatting was incorrect.
A couple of various things:
The curly brace after state := true should be the other way (}, not {)
There is no default rand function in AHK, you are probably either looking for Random, or you have a custom function called rand that you is not shown in your question. In any case, I'll write a function rand(a,b) that will return an integer value between a and b
rand(a, b)
{
Random, rand, a, b
return rand
}
Additionally, there is another function getState() that is being invoked inside the loop 10000. I'm not sure what it is supposed to do (or if you meant something like GetKeyState instead), but I'll assume that you have that covered on your end.
As #Pranav Hosangadi mentioned, you likely wanted an else if statement instead of just an else statement on this line: else(gemColor = 96885A)
Are you sure you want SendMode Input? Although it does have superior speed than standard Send, its use is normally limited to typing text in a text box. It seems that you are trying to send a keystroke to a flash game, so you might want to check whether that functioning as you intend it to.
When writing a end curly brace (}) to conclude an if() or else() clause, you need to put it on its own line. (i.e. change
if(state = true)
{Mine()}
else
{Sleep, rand(70,150)}
to something like
if(state = true)
{
Mine()
}
else
{
Sleep, rand(70,150)
}
or even (since the if and else statements here only trigger one line of code each)
if(state = true)
Mine()
else
Sleep, rand(70,150)
So, that was a bit long, but here is the final code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
; ---> Double check this! ---> SendMode Input
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
condChecker := false
state := true
Mine()
{
Sleep, rand(10,80)
Send, {Space}
while(state = true)
{
PixelGetColor, gemColor, 982, 433
if(gemColor = B93557)
{
state := true
}
else if(gemColor = 96885A)
{
state := false
}
Sleep, rand(90,120)
}
}
rand(a, b)
{
Random, rand, a, b
return rand
}
^-::
loop 10000
{
;getState()
if(state = true)
Mine()
else
Sleep, rand(70,150)
}
lmk if something doesn't work properly, and I'll try to update this response
I am trying to figure out, why, if I put breakpoints on if and on else line, why my if else {} condition breaks on else if the condition was true in if block?
I am using realm but I do not think, that is an issue.
//Check if Object already exists in database
if !RealmService.shared.ifPortfolioExists(name: portfolio.name){//Breakpoint on this line which is true
//Create portfolio
RealmService.shared.create(portfolio)
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}else{//Breakpoint on this line
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}
Am I working out of my mind or am I just stupid? Can please someone explain me this.
The compiler reads them to see if it should proceed into the following block of code. A breakpoint inside of the if will trigger when true and else when false
Do this to better understand where your breakpoints take effect:
if x != y {
print("if breakpoint")//set breakpoint here
} else {
print("else breakpoint")//set breakpoint here
// you should see that the breakpoint doesn't fire here
}
I'm new to coding and currently teaching myself swift using swift playgrounds on the iPad. My code runs and completes the puzzle but it continues to loop and I don't know why. I can't find any way to correct this code. Although I have found videos on YouTube with various code written differently. I don't just want to copy it though. I want to understand why this isn't working. I can send a video of the puzzle if needed.
while !isOnGem || !isOnClosedSwitch {
moveForward()
if isBlocked && !isBlockedRight {
turnRight()
}
if isBlocked && isBlockedRight {
turnLeft()
}
if isOnGem {
collectGem()
}
if isOnClosedSwitch {
toggleSwitch()
}
}
Without any other information regarding the functions in each of your if blocks, I'd say that it is due to your boolean values for isOnGem and isOnClosedSwitch. If the function collectGem() does not change the value of isOnGem to the opposite of what it was initially set to (true or false) and toggleSwitch() doesn't change the value of isOnClosedSwitch to the opposite of it's original value then you will be stuck in the loop. Since the loop will run "while" at least one of those values remain unchanged.
I believe adding a isOnGem = false and isOnClosedSwitch = false to their respective if blocks will be the solution.
You are missing the exit condition. while !isOnGem || !isOnClosedSwitchwill continue to loop as long as either condition is true, therefore your exit condition will be having both values set to false.
Note that both Booleans are inverted in your check so to make both conditions false you have to set the Booleans to true.
Since you code runs and yet does not exit to loop, you will want to check for changes to isOnGem and isOnClosedSwitch there might be one of the two that is always false resulting in the loop not exiting or the function that runs after each checks might have reset them to false
check for code like:
func collectGem(){
...
isOnGem = false
...
}
or one of the functions might not even have run, you can log each function like :
func toggleSwitch() {
print("toggleSwitchRunning")
}
and if "toggleSwitchRunning" did not print into the console, check that the condition that set isOnClosedSwitch to true is working properly
I was looking though a fellow developers code when I saw this ..
for (;;){
....
....
....
}
I have never seen ";;" used in a loop. What does this do exactly?
It loops forever. ';;' equates to no start value, no stop condition and no increment condition.
It is equivalent to
while (true)
{
...
}
There would usually be a conditional break; statement somewhere in the body of the loop unless it is something like a system idle loop or message pump.
All 3 parts are optional. An empty loop initialization and update is a noop. An empty terminating condition is an implicit true. It's essentially the same as
while (true) {
//...
}
Note that you it doesn't have to be all-or-nothing; you can have some part and not others.
for (init; cond; ) {
//...
}
for (; cond; update) {
//...
}
for (init; ; update) {
//...
}
Just like in C, the for loop has three sections:
a pre-loop section, which executes before the loop starts.
a continuing condition section which, while true, will keep the loop going.
a post-iteration section which is executed after each iteration of the loop body.
For example:
for (i = 1, acc = 0; i <= 10; i++)
acc += i;
will add up the numbers from 1 to 10 inclusive (in C and, assuming you use Perl syntax like $i and braces, in Perl as well).
However, nothing requires that the sections actually contain anything and, if the condition is missing, it's assumed to be true.
So the for(;;) loop basically just means: don't do any loop setup, loop forever (breaks notwithstanding) and don't do any iteration-specific processing. In other words, it's an infinite loop.
Infinite loop. A lot of the time it will be used in a thread to do some work.
boolean exit = false;
for(;;) {
if(exit) {
break;
}
// do some work
}
Infinite Loop (until you break out of it).
It's often used in place of:
while(true) { // Do something }
It's the same as
while(true) {
...
}
It loops forever.
You don't need to specify all of the parts of a for loop. For example the following loop (which contains no body, or update token) will perform a linear search of myArray
for($index = -1; $myArray[++$index] != $target;);