Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

fix this code, i want it to start runnin indefinately when i press a key until i press…

  1. User

    fix this code, i want it to start runnin indefinately when i press a key until i press the same key again

    ; Define a hotkey to start/stop the loop ToggleLoop := true ; Flag to control the loop

    F8:: ; Replace ‘F8’ with your preferred hotkey ToggleLoop := !ToggleLoop ; Toggle the flag

    if (ToggleLoop)  ; If the flag is true, start the loop
    {
        while (ToggleLoop)  ; Keep looping while the flag is true
        {
            ; Your macro actions go here
            send, {F1}  ; 1 magia atk rotacao
            Random, sleepDuration, 50, 60
            Sleep, %sleepDuration%
            send, {F4}  ; cura
            Sleep, %sleepDuration%
            send, 2  ; mana potiond
            Sleep, %sleepDuration%
            send, {F6}  ; 1 magia suporte 
            Sleep, %sleepDuration%
            send, {F2}  ; 2 magia atk rotacao
            Sleep, %sleepDuration%
            send, {F4}  ; cura
            Sleep, %sleepDuration%
            send, 2  ; mana potion
            Sleep, %sleepDuration%
            send, {F7}  ; 1 magia suporte 
            Sleep, %sleepDuration%
            send, {F3}  ; 3 magia atk rotacao
            Sleep, %sleepDuration%
            send, {F4}  ; cura
            Sleep, %sleepDuration%
            send, 2  ; mana potion
            Sleep, %sleepDuration%
            send, {F7}  ; 3 magia suporte 
            Sleep, %sleepDuration%
            Send, {Alt down}
            Random, offsetX, -5, 5
            Random, offsetY, -5, 5
            MouseClick, Left, 3134 + offsetX, 215 + offsetY  ; sqm battle 1 inimigo
            Send, {Alt up}
            Sleep, %sleepDuration%
            Send, {Shift down}
            Click, Right, 2070 + offsetX, 700 + offsetY  ; sqm cima direita
            Click, Right, 2060 + offsetX, 800 + offsetY  ; sqm direita
            Click, Right, 2062 + offsetX, 900 + offsetY  ; sqm baixo direita
            Click, Right, 1940 + offsetX, 930 + offsetY  ; sqm baixo
            Click, Right, 1820 + offsetX, 930 + offsetY  ; sqm baixo esquerda
            Click, Right, 1820 + offsetX, 800 + offsetY  ; sqm esquerda
            Click, Right, 1820 + offsetX, 700 + offsetY  ; sqm cima esquerda
            Click, Right, 1940 + offsetX, 700 + offsetY  ; sqm cima
            Click, Right, 1940 + offsetX, 820 + offsetY  ; sqm meio
            Send, {Shift up}
            Sleep, %sleepDuration%
    
            ; This small delay can help prevent CPU overload
            Sleep, 10
        }
    }
    

    Return

    Also this is just one block of my ahk script, there are other blocks

  2. Assistant

    To fix your script so that it runs indefinitely when a key is pressed and stops when the same key is pressed again, you need to ensure the toggle mechanism is correctly implemented. Your code logic is generally correct, but there are a few improvements and adjustments I can suggest for clarity and reliability.

    Below is a revised version of your script:

    ; Define a flag to control the loop
    ToggleLoop := false  ; Start with the loop off
    
    F8:: ; Hotkey to toggle the loop state
        ToggleLoop := !ToggleLoop  ; Toggle the state of the loop
    
        if (ToggleLoop)  ; If loop is now enabled
        {
            SetTimer, RunLoop, 0  ; Start the loop using a timer
        }
        else
        {
            SetTimer, RunLoop, Off  ; Stop the loop
        }
    Return
    
    RunLoop:
        ; Your macro actions go here
        send, {F1}  ; 1 magia atk rotacao
        Random, sleepDuration, 50, 60
        Sleep, %sleepDuration%
        send, {F4}  ; cura
        Sleep, %sleepDuration%
        send, 2  ; mana potion
        Sleep, %sleepDuration%
        send, {F6}  ; 1 magia suporte 
        Sleep, %sleepDuration%
        send, {F2}  ; 2 magia atk rotacao
        Sleep, %sleepDuration%
        send, {F4}  ; cura
        Sleep, %sleepDuration%
        send, 2  ; mana potion
        Sleep, %sleepDuration%
        send, {F7}  ; 1 magia suporte 
        Sleep, %sleepDuration%
        send, {F3}  ; 3 magia atk rotacao
        Sleep, %sleepDuration%
        send, {F4}  ; cura
        Sleep, %sleepDuration%
        send, 2  ; mana potion
        Sleep, %sleepDuration%
        send, {F7}  ; 3 magia suporte 
        Sleep, %sleepDuration%
        Send, {Alt down}
        Random, offsetX, -5, 5
        Random, offsetY, -5, 5
        MouseClick, Left, 3134 + offsetX, 215 + offsetY  ; sqm battle 1 inimigo
        Send, {Alt up}
        Sleep, %sleepDuration%
        Send, {Shift down}
        Click, Right, 2070 + offsetX, 700 + offsetY  ; sqm cima direita
        Click, Right, 2060 + offsetX, 800 + offsetY  ; sqm direita
        Click, Right, 2062 + offsetX, 900 + offsetY  ; sqm baixo direita
        Click, Right, 1940 + offsetX, 930 + offsetY  ; sqm baixo
        Click, Right, 1820 + offsetX, 930 + offsetY  ; sqm baixo esquerda
        Click, Right, 1820 + offsetX, 800 + offsetY  ; sqm esquerda
        Click, Right, 1820 + offsetX, 700 + offsetY  ; sqm cima esquerda
        Click, Right, 1940 + offsetX, 700 + offsetY  ; sqm cima
        Click, Right, 1940 + offsetX, 820 + offsetY  ; sqm meio
        Send, {Shift up}
        Sleep, %sleepDuration%
    
        ; Short delay for stability and CPU management
        Sleep, 10
    Return
    

    Explanation

    • ToggleLoop: This flag controls whether the loop is running or not. It starts as false (off).

    • SetTimer: This is used to repeatedly call RunLoop like a loop, but it's generally more reliable and makes your code cleaner. When ToggleLoop is true, it starts the loop; otherwise, it stops.

    • RunLoop: This label contains all the actions you want to perform in each iteration of your pseudo-loop.

    • Immediate Toggle: Pressing the hotkey (F8 in your example) will immediately start or stop the loop.

    This structure should handle the start/stop of your macro actions more robustly, and using SetTimer allows other parts of your script to remain responsive even while the looping actions occur.