Jump to content

Two functions, Two hotkeys. Allowing them to only run after the other one has run, except for the first instance


Recommended Posts

Hi guys, playing with scripting. Does my question make sense? I have two functions and I only want them to run if the other one was run previously, so that they can't be repeated. Except for the first run of the script, where neither has run.

If someone can point me in the right direction that would be great, thanks!

Link to comment
Share on other sites

Two global flags, one indicating first run, other indicating that function has ran.

Check both when entering function.

Thanks John! I'm new to this, and I tried to put the effort in and figure this out myself, but I just couldn't figure out what step to take. The helpfile doesn't have anything I could find for Flags, and the wiki has an article for Global Variables, but I didn't see any correlation in the examples I could use.

I would love to figure this out myself, any tips on where to look next?

Link to comment
Share on other sites

When I say flag I mean variable

$firstrun would be set to 1 and when enter either of your function it sets it to 0

$ran would be set to 0 and set to 1 when enter function.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

When I say flag I mean variable

$firstrun would be set to 1 and when enter either of your function it sets it to 0

$ran would be set to 0 and set to 1 when enter function.

A lot of googling and help checking, and I have come up with this:

HotKeySet("\", "Click1")
HotKeySet("]", "Click2")

While 1
    Sleep(100)
WEnd

Global $firstrun
Global $ran

Func Click1()
    If $firstrun = 0 Then
     Send("s")
     MouseClick("left",1136,202,1,2)
     Assign ( "$firstrun", "1")
     EndIf
     If $firstrun = 1 Then
     If &ran = 0 Then
         Send("x")
         MouseClick("left",1136,202,1,2)
         Assign ( "$ran", "1")
         EndIf
EndIf
EndFunc

Func Click2()
    If $firstrun = 0 Then
     Send("s")
     MouseClick("left",1076,222,1,2)
     Assign ( "$firstrun", "1")
     EndIf
     If $firstrun = 1 Then
     If &ran = 1 Then
         Send("x")
         MouseClick("left",1136,202,1,2)
         Assign ( "$ran", "0")
         EndIf
EndIf
EndFunc

I think I did pretty good! I feel like I made progress in understanding, anyway. Couple things:

Error on line 12: Firstrun = 0 doesn't work because "variable being used without being declared"

The other thing is, while the functions I listed are simplified for testing purposes, they could end up being quite long. Could I link to them in some way instead of retyping them every time I want them to get used? In this case, after a few different If Then cases?

Link to comment
Share on other sites

Decent effort.
 
See if you understand what's going on here with a few mods to your own code.
 
Global $firstrun = 1, $ran = 0

HotKeySet("\", "Click1")
HotKeySet("]", "Click2")

While 1
    Sleep(100)
WEnd

Func Click1()
    If $firstrun = 1 Then
        $firstrun = 0
        $ran += 1
        ;Your Code or func
        Click1Func()
    ElseIf $ran > 0 Then
        ;Your Code or func
        Click1Func()
    EndIf
EndFunc   ;==>Click1

Func Click2()
    If $firstrun = 1 Then
        $firstrun = 0
        $ran += 1
        ;Your Code or func
        Click2Func()
    ElseIf $ran > 0 Then
        ;Your Code or func
        Click2Func()
    EndIf
EndFunc   ;==>Click2

Func Click2Func()
    MsgBox(0, "Click2", "Click2Func")
EndFunc   ;==>Click2Func

Func Click1Func()
    MsgBox(0, "Click1", "Click1Func")
EndFunc   ;==>Click2Func
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I think he means that he wants each function to be run one after the other and not repeated.
Something like this:

Global $iLastRun = 0 ; 0 means nothing has run yet

HotKeySet("\", "Click1")
HotKeySet("]", "Click2")
HotKeySet("{ESC}", "_End")

While 1
    Sleep(100)
WEnd

Func Click1()
    If $iLastRun = 1 Then Return ; Will return from the func if it was run last
    ; NOTE: We will only get here if Click1() was not run previously.
    $iLastRun = 1 ; Set the global so that the rest of the script knows what ran last

    ; ... your code here ...
    Click1Func()
EndFunc ;==>Click1

Func Click2()
    If $iLastRun = 2 Then Return ; Will return from the func if it was run last
    $iLastRun = 2 ; Set the global so that the rest of the script knows what ran last

    ; ... your code here ...
    Click2Func()
EndFunc ;==>Click2

Func Click1Func()
    MsgBox(0, "Click1", "Click1Func")
EndFunc ;==>Click2Func
Func Click2Func()
    MsgBox(0, "Click2", "Click2Func")
EndFunc ;==>Click2Func

Func End()
    Exit
EndFunc ;==>Click2Func
Edited by DatMCEyeBall

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Thanks guys! you have both been very helpful, this was a lot of fun and I couldn't have done it without you! MCEyeBall is correct, I didn't want either function to be able to repeat. Only one after the other.

I have a couple questions, if you guys swing through this thread again.

If I used the $ran variable as presented in John's function, why "$ran +=1" and not just "$ran = 1"?

Edit: nevermind the second question, thanks for the example code guys. I learned a lot

Edited by Ravehammer
Link to comment
Share on other sites

Thanks guys! you have both been very helpful, this was a lot of fun and I couldn't have done it without you! MCEyeBall is correct, I didn't want either function to be able to repeat. Only one after the other.

I have a couple questions, if you guys swing through this thread again.

If I used the $ran variable as presented in John's function, why "$ran +=1" and not just "$ran = 1"?

Edit: nevermind the second question, thanks for the example code guys. I learned a lot

You should mark the thread as solved.

You can find the button in the bottom right corner of the each post.

post-79729-0-05684900-1389096672_thumb.p

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...