Jump to content
Sign in to follow this  
asym

Func StartScript() help

Recommended Posts

asym

So I have this script with a func startscript and it used to work fine but now I can only start the script and cant stop it later

HotKeySet("{F9}", "StartScript")

$script = False

ToolTip("Press F9 to Start/Stop Script",300,100,"ZPoB",2,2)
Sleep(5000) ;So we have time to read it
ToolTip("") ;Remove the ToolTip

While $script = True
a lot of code here
WEnd


Func StartScript() ;functions are normally placed last, make it easier to browse the code
    If $script = False Then
        $script = True
        ToolTip("Script is now Runing   |    Press F9 to Stop",200,100,"ZPoB",1,2)
        Sleep(2000)
        ToolTip("")
    Else
        $script = False
        ToolTip("Script is now Stoped   |    Press F9 to Start",200,100,"ZPoB",1,2)
        Sleep(2000)
        ToolTip("")

    EndIf
EndFunc   ;==>StartScript

And I only can start the script while the first tooltip is active after that I cant anymore... Any help with this?

It used to work fine but not now :S

Edited by kaneco

Share this post


Link to post
Share on other sites
asym

works fine for me, I suspect the problem lies with your "a lot of code"

yeah thats what had the most changes but anyway can that affect the result or functionality of the rest of the script (mainly func startscript) ?

Share this post


Link to post
Share on other sites
ShawnW

You need to realize that it only checks for the $script variable at the beginning of the loop. If your loop does so much or has other loops inside it, those will all continue running until you get to the end of the while and it checks $script again.

Edited by ShawnW

Share this post


Link to post
Share on other sites
asym

You need to realize that it only checks for the $script variable at the beginning of the loop. If your loop does so much or has other loops inside it, those will all continue running until you get to the end of the while and it checks $script again.

Yes it has some loops and a lot of other things inside...

So how can I change the code to allow it to start/Stop thee script even while the main loop is working?

Share this post


Link to post
Share on other sites
ShawnW

you need to look over any thing that might take a lot of time in your current large loop. Other loops inside it are a good place to start. Add a condition within those loop to check for the $script variable.

Examples:

While $script = True
    ; some code here
    
    ; a fixed loop that last 5 minutes
    For $i= 0 To 300
        If $script = True Then ExitLoop 2
        Sleep(1000)
        ; more code
    Next
    
    ; another conditional loop
    While some condition And $script = True
        ; more code
    WEnd
    
    ; more code
WEnd

Share this post


Link to post
Share on other sites
ShawnW

The hotkeyset should break into a running function or loop.

Maybe you have a tight loop in there without a sleep, and its affecting your cpu load?

Yes the hotkey breaks into loops but all the hotkey does is set a variable.

In order to use that variable immediately you need to test for it within the loop or else the loop will finish its current iteration before the test happens.

You could however do this to change the function to actually cause the pause rather than set a variable. The difference would be that when unpaused, the script would pick up right where it left off rather than at the beginning of the loop.

HotKeySet("{Esc}","Terminate")
HotKeySet("{F9}", "Pause")
Global $Paused = False

While True
    ToolTip("Script Running")
WEnd

Func Pause()
    $Paused = Not $Paused
    While $Paused
        ToolTip("Script Paused")
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit
EndFunc
Edited by ShawnW

Share this post


Link to post
Share on other sites
asym

Thanks for your help... Still I didnt manage to get it to work properly...

A strange thing I detected is that I can only start the script by clicking F9 while this tooltip

ToolTip("Press F9 to Start/Stop Script",300,100,"ZPoB",2,2)
is showing

If I click F9 after the tooltip disapears (5 seconds later) I cant start the script at all...

The "lot of code" I was talking about is below... Can you show me or give me an example how can I integrate that thing of calling $script while its doing the loop so I can stop and start the script?

And the whole loop takes on average 5 seconds, and about 2 mins if the pixelsearch found the defined colour...

While $script = True
    Sleep(10)
    MouseClick("left", 47, 478, 1)
    Sleep(10)
    MouseClick("left", 47, 478, 1)
    Sleep(3000)

    $n = 0
    $pixel = PixelSearch(265, 101, 820, 246, 0x00ff00)
    If Not @error Then
        SoundPlay(@WindowsDir & "\media\hallelujah.wav", 0)
        Do
            $n = $n + 1
            MouseClick("left", 73, 448, 2)
            Sleep(500)
            MouseClick("left", 106, 559, 2)
            Sleep(500)
        Until $n = 100
    Else
        Sleep(10)
        PixelSearch(265, 101, 820, 246, 0xFFF200)
        If Not @error Then
            SoundPlay(@WindowsDir & "\media\hallelujah.wav", 0)
            Do
                $n = $n + 1
                MouseClick("left", 73, 448, 2)
                Sleep(500)
                MouseClick("left", 106, 559, 2)
                Sleep(500)
            Until $n = 100
        Else
            Sleep(10)
            MouseClick("left", 277, 557, 1)
            Sleep(150)
            MouseClick("left", 277, 557, 1)
            Sleep(10)
            MouseClick("left", 277, 557, 1)
            Sleep(200)
        EndIf
    EndIf
WEnd

Btw, I wish this forum had reputation system... I want to give you all that helped me good reputation but I cant because theres no system

Edited by kaneco

Share this post


Link to post
Share on other sites
ShawnW

Well The reason for you needing to hit f9 while the tool tip is up is because the program ends after that. You need a constant while loop or something to allow you time to start your loop whenever you want.

I'll work on something with the code you mentioned.

Share this post


Link to post
Share on other sites
ShawnW

This is untested. I put breaks on both loops which will cause the big loop to finish its iteration faster. I totally obliterated your version of StartScript but this should work and still give you the proper tooltips. I took out the initial tool tip since you give good instructions in the others and one of those should always be displayed.

The program should drop right into a stopped state constantly looping and checking the $script variable

When you hit F9 the tool tip changes and loop starts executing the main code.

F9 again the script will finish its iteration but pop out of any internal loops to make it faster.

If you want more immeadiate stopage you can add lines at various places that say...

If Not $script Then Continue

Which will force the loop to start over and since $script is false it will do nothing.

Anyway here is the code:

HotKeySet("{Esc}","Terminate")
HotKeySet("{F9}", "StartScript")

$script = False

ToolTip("Press F9 to Start/Stop Script",300,100,"ZPoB",2,2)
Sleep(5000) ;So we have time to read it
ToolTip("") ;Remove the ToolTip

While True 
    If $script Then
        ToolTip("Script is now Runing   |    Press F9 to Stop",200,100,"ZPoB",1,2)
        Sleep(10)
        MouseClick("left", 47, 478, 1)
        Sleep(10)
        MouseClick("left", 47, 478, 1)
        Sleep(3000)

        $n = 0
        $pixel = PixelSearch(265, 101, 820, 246, 0x00ff00)
        If Not @error Then
            SoundPlay(@WindowsDir & "\media\hallelujah.wav", 0)
            Do
                $n = $n + 1
                MouseClick("left", 73, 448, 2)
                Sleep(500)
                MouseClick("left", 106, 559, 2)
                Sleep(500)
            Until $n = 100 Or Not $script
        Else
            Sleep(10)
            PixelSearch(265, 101, 820, 246, 0xFFF200)
            If Not @error Then
                SoundPlay(@WindowsDir & "\media\hallelujah.wav", 0)
                Do
                    $n = $n + 1
                    MouseClick("left", 73, 448, 2)
                    Sleep(500)
                    MouseClick("left", 106, 559, 2)
                    Sleep(500)
                Until $n = 100 Or Not $script
            Else
                Sleep(10)
                MouseClick("left", 277, 557, 1)
                Sleep(150)
                MouseClick("left", 277, 557, 1)
                Sleep(10)
                MouseClick("left", 277, 557, 1)
                Sleep(200)
            EndIf
        EndIf
    Else
        ToolTip("Script is now Stoped   |    Press F9 to Start",200,100,"ZPoB",1,2)
    EndIf
WEnd


Func StartScript()
    $script = Not $script
EndFunc

Func Terminate()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
asym

Wow I had a quick look at your code and your tips and just by adding

While 1 
the main code/loop I posted before
WEnd

In the main loop, everything now works perfectly... I can start stop when I want and the desired tooltips appear without any other code between the main loop...

I must have forgot this code line because I had it in previous versions of my script thats why it worked and now it wasnt working...

Thanks for all your help and time spent :blink:

Share this post


Link to post
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
Sign in to follow this  

×