Jump to content

Continuous loop problem. Help me if you can. Thanks!


Amnael
 Share

Recommended Posts

Hi everyone!

I'm pretty much new to the forum and to the programming world of AutoIt 3.

I tried to make a script that will help me do certain functions as I press a hotkey.

I defined a hotkey to start the function and another one to stop it.

Problem is that I can run the script only once and then i have to use the .exe file again :( which is pretty much annoying.

Another issue that it occurred is that when I try to use more functions it won't work at all, only the first one is executed.

Here what I did so far:

Global $Paused
HotKeySet("{1}","Start") ;script started by pressing 1.
HotKeySet("{2}", "Stop") ;script stopped by pressing 2.
HotKeySet("{3}","superclick")
HotKeySet("{4}","superclickstop")
HotKeySet("{5}","rumble")
HotKeySet("{6}","rumblestop")

$Start = False
$superclick = False
$rumble = False

While 1

If $Start Then

While 1
Send("{SPACE}")
Sleep(100)
Send("{SPACE}")
Sleep(100)
Wend
Endif
WEnd

While 1

If $superclick then
While 2
MouseClick("left",148, 585)
WEnd
EndIf
WEnd

While 1

If $rumble Then
While 1
MouseClick("left",171, 294)
Sleep(10)
MouseClick("left",233, 293)
Sleep(10)
MouseClick("left",568, 470)
Sleep(10)
MouseClick("left",510, 614)
Sleep(10)
MouseClick("left",505, 664)
Sleep(10)
WEnd
EndIf

WEnd


Func Stop()
Exit
EndFunc

Func Start()
$Start=True
EndFunc

Func superclick()
$superclick=True
Endfunc

Func superclickstop()
Exit
EndFunc

Func rumble()
$rumble=True
EndFunc

Func rumblestop()
Exit
EndFunc

What I'm trying to do is:

When I start the script I want to start each function whenever I need. Meaning: when I press key 1 function Start will do its job, when I press key 2 it'll stop. Thing is I want to be able to run it again without starting the script all over again.

Also I wish I could start all the functions in any order I please so that they could work in pairs or even simultaneous.

I know I've to do some loops here. I tried putting the whole block in a For, If, While block... it didn't work. I tried with ContinueLoop, failed as well.

If you people could help me it would mean a lot to me. Thank you very much for reading this post!

I'm looking forward to your answers!

Link to comment
Share on other sites

A couple of pointers.

1. For starters your first loop is infinite ("while 1", in effect, means "go on forever") so you will never reach the second loop.

2. Also, your ...stop() functions execute the "exit" command, no wonder the script stops when you tell it to, right? :)

3. Try maintaining three boolean variables, and having your hotkey functions toggle the relevant variable. ($myBooleanVar = not $myBooleanVar will toggle a boolean var.) Having toggle functions cuts your need for hotkeys in half in this script.

4. In your infinite loop, check the status of all three boolean vars and if any of them is true, execute the relevant code. Putting the relevant code in a separate method is a nice way to keep your code readable. Something like: if $myBooleanVar1 then doPieceOfCode1().

There's a myriad of ways to approach this. Have fun! :)

/edit: here's a pretty stupid example script that continually (well, every 0.5 seconds) plays the notes of a C seventh chord. You can turn playing all four notes on and off at will by pressing hotkeys 1 to 4. This should be a usable example for you I guess. It starts by playing nothing (all boolean vars are false) so try the hotkeys. (And turn on your speakers :))

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")
HotKeySet("4", "setNote4")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false
$note4 = false

Dim $octave5_notes[13] = [523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8]

$timer = TimerInit()

While 1

If $note1 Then Beep($octave5_notes[0], 100)
If $note2 Then Beep($octave5_notes[4], 100)
If $note3 Then Beep($octave5_notes[7], 100)
If $note4 Then Beep($octave5_notes[10], 100)

While TimerDiff($timer) < 500
WEnd
$timer = TimerInit()
WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func setNote4()
$note4 = Not $note4
ConsoleWrite("Toggled $note4, it is now: " & $note4 & @CRLF)
EndFunc

Func quitter()
exit

; end the funk :)
EndFunc
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Man.. your example does the job its meant to be, but when I try to replace the $note functions with my functions it”s a total mess. :(

For some reason... I”m less too skillful and I can”t make it work right.

I tried using the Case procedure... ffs this is getting me exhausted :)).

..help ... help ... HELP!

Link to comment
Share on other sites

Something...Something... ouch it hit hard:

Select
Case $note1
Beep($octave5_notes[0], 100)
Case $note2
Beep($octave5_notes[4], 100)
Case $note3
Beep($octave5_notes[7], 100)
Case $note4
Beep($octave5_notes[10], 100)
EndSelect

Working???

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Something...Something... ouch it hit hard:

Select
Case $note1
Beep($octave5_notes[0], 100)
Case $note2
Beep($octave5_notes[4], 100)
Case $note3
Beep($octave5_notes[7], 100)
Case $note4
Beep($octave5_notes[10], 100)
EndSelect

Working???

That won't do, since select/case only executes the first matching case, while my musical script or TS's script needs to execute for *any* matching case. But thanks for trying... Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Man.. your example does the job its meant to be, but when I try to replace the $note functions with my functions it”s a total mess. :(

For some reason... I”m less too skillful and I can”t make it work right.

I tried using the Case procedure... ffs this is getting me exhausted :)).

..help ... help ... HELP!

Please post what you tried with my example?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Did you really try it? It works just like script in ur previous post:

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")
HotKeySet("4", "setNote4")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false
$note4 = false

Dim $octave5_notes[13] = [523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8]

$timer = TimerInit()

While 1

Select
Case $note1
Beep($octave5_notes[0], 100)
Case $note2
Beep($octave5_notes[4], 100)
Case $note3
Beep($octave5_notes[7], 100)
Case $note4
Beep($octave5_notes[10], 100)
EndSelect

While TimerDiff($timer) < 500
WEnd
$timer = TimerInit()
WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func setNote4()
$note4 = Not $note4
ConsoleWrite("Toggled $note4, it is now: " & $note4 & @CRLF)
EndFunc

Func quitter()
exit

; end the funk :)
EndFunc

And the OP himself needed assistance on Case thing...

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Did you really try it? It works just like script in ur previous post:

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")
HotKeySet("4", "setNote4")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false
$note4 = false

Dim $octave5_notes[13] = [523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8]

$timer = TimerInit()

While 1

Select
Case $note1
Beep($octave5_notes[0], 100)
Case $note2
Beep($octave5_notes[4], 100)
Case $note3
Beep($octave5_notes[7], 100)
Case $note4
Beep($octave5_notes[10], 100)
EndSelect

While TimerDiff($timer) < 500
WEnd
$timer = TimerInit()
WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func setNote4()
$note4 = Not $note4
ConsoleWrite("Toggled $note4, it is now: " & $note4 & @CRLF)
EndFunc

Func quitter()
exit

; end the funk :)
EndFunc

And the OP himself needed assistance on Case thing...

Yes I really tried it, and it kept just playing the C note as I expected...

/edit... and I tried AGAIN with your example above. Still only the C is played even when all four toggles are True. Ehm... Did YOU really try it? :)

/edit 2... and by the way - if I read correctly, the TS only said he tried to use case, not that he needed assistance using it. What he's asking for is a working approach to his situation. And 'case' usage doesn't seem to be it... Anyway, this discussion is not helping the topic.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Ah, I get what you are saying, yeah it doesn't do that multi-tasking type thing...

Edited by MKISH

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

And NOW:

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")
HotKeySet("4", "setNote4")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false
$note4 = false

Dim $octave5_notes[13] = [523.3,554.4,587.3,622.3,659.3,698.5,740.0,784.0,830.6,880.0,932.3,987.8]

$timer = TimerInit()

While 1

Select
Case $note1 = true
Beep($octave5_notes[0], 100)
Case $note2 = true
Beep($octave5_notes[4], 100)
Case $note3 = true
Beep($octave5_notes[7], 100)
Case $note4 = true
Beep($octave5_notes[10], 100)
EndSelect

While TimerDiff($timer) < 500
WEnd
$timer = TimerInit()
WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func setNote4()
$note4 = Not $note4
ConsoleWrite("Toggled $note4, it is now: " & $note4 & @CRLF)
EndFunc

Func quitter()
exit

; end the funk :)
EndFunc

Sheesh... That does the EXACT same thing as your previous post. Since the truth evaluation of the expression "$var" is the same as the truth evaluation of the expression "$var =(=) true", your select/case piece is logically exactly equivalent in both of your attempts. Please, if you want me to try your code, try it yourself first. And apart from that... Your select/case thing spans 10 lines while mine only spans four. What exactly is so incredibly preferable about select/case in this example - even if it would actually work to the same effect? (And why do you let it hit you so hard?) Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Well, now I guess, I lose clearly, yet I end my discussion with a simple suggestion that you could simply use arrays to trim down your script... Sorry for being unable to understand your point first.

Good luck

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Well, now I guess, I lose clearly, yet I end my discussion with a simple suggestion that you could simply use arrays to trim down your script... Sorry for being unable to understand your point first.

Good luck

Haha, thanks for the graceful acceptance of your loss :) Case/select is useful for shorthand and readable notation of "if - elseif - elseif - elseif - elseif - else - endif" kinda stuff... Not for multiple things you need to check to decide whether or not to fire multiple pieces of code, like is necessary here. Sure, nice arrays could do nicer tricks, but I was afraid TS would be overwhelmed. That's why I just used a simple array for the octave notes to still hint at the concept of arrays :) No hard feelings I hope - just some lessons learned about sarcasm? ;) Hope TS understands what we are going on about... :)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Hey folks!

Thanks for sharing your thoughts with me!

This is what i”ve tried so far, using if and else if within a While loop:

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")
HotKeySet("{q}", "ex1")
HotKeySet("{w}", "ex2")
HotKeySet("{e}", "ex3")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false

While 1
If $note1 Then
While 1
Send("{SPACE}")
Sleep(100)
Send("{SPACE}")
Sleep(100)
WEnd
ElseIf $note2 Then
While 2
MouseClick("left",148, 585)
WEnd
ElseIf $note3 Then
While 1
MouseClick("left",171, 294)
Sleep(10)
MouseClick("left",233, 293)
Sleep(10)
MouseClick("left",568, 470)
Sleep(10)
MouseClick("left",510, 614)
Sleep(10)
MouseClick("left",505, 664)
Sleep(10)
WEnd
EndIf

WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func setNote4()
$note4 = Not $note4
ConsoleWrite("Toggled $note4, it is now: " & $note4 & @CRLF)
EndFunc

Func ex1()
$note1=not $note1
EndFunc

Func ex2()
$note2=not $note2
EndFunc

Func ex3()
$note3=not $note3
EndFunc

Func quitter()
Exit
EndFunc

Thing is... I added those q w e hotkeys to exit somehow from the loop... but it doesn”t work at all.

Also... I”m guessing that if - else if block won”t let me activate two or three functions simultaneously.

Please help me to nail it!

Thanks again!

Link to comment
Share on other sites

The problem seems to be your understanding of the while loop. A while loop goes like this:

while <expression is true>

do something

wend

Look at your code and you'll see that your "expression" is: 1. 1 is true. Always. So, the loop will keep on going forever. No matter what you do in hotkey functions, that loop will go on as long as 1 is true, which comes down to forever.

By changing the expression from something that's always true to something that is only true when you toggle the hotkey. That's why you set the $note1 to Not $note1 (if it's True, it becomes False, if it's False it becomes True).

There is an easy trick to exit a loop that goes on forever. The command ExitLoop will jump right out of the loop where the script is in at that point. (So putting if $note1 == False then ExitLoop into your loop should do something.) But for a while loop like this, I suggest While $note1 = True. (Equivalent shorthand: While $note1.)

Your problem is, however, that you want simultaneous functionality based on multiple settings, without knowing beforehand when they will be toggled. This means that you cannot use a loop that goes on while one hotkey toggle is True, because then the loop will not care whether another hotkey toggle is True or False.

The thing with If, ElseIf and Else is what I was going on about with MKISH.

If $x Then
<say hi>
ElseIf $y Then
<say bye>
Else
<say blah>
Endif

... will "say hi" if $x is true and forget about the ElseIf or Else that's below it. If $x is not true, it will go on to the next step and check whether $y is true. In that case it says bye. If even that isn't true, it finally goes to the Else block that handles all other cases. (If no else block would have been supplied, it would just have stopped after checking $y.

In your case, you'll want to find some way to stay inside a loop that keeps checking for any of the hotkey variables and do stuff based on the hotkey once, then go on to the next check. After you've checked all, you want your loop to start over again. That's why you need the main loop, the only While 1 you should want to use in this example. This is exactly what I did in my example script. Keep the main loop going with While 1, and every time the loop starts over because, well, 1 is still true, it goes checking for $note1, $note2, $note3 and $note4. All in separate if blocks because they are evaluated independantly.

Hope this clears stuff up a bit?

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Sad or not that sad Bunny, you made my day!

Look how simple this was :)))

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false

While 1
If $note1 Then
While $note1
Send("{SPACE}")
Sleep(100)
Send("{SPACE}")
Sleep(100)
WEnd
ElseIf $note2 Then
While $note2
MouseClick("left",148, 585)
WEnd
ElseIf $note3 Then
While $note3
MouseClick("left",171, 294)
Sleep(10)
MouseClick("left",233, 293)
Sleep(10)
MouseClick("left",568, 470)
Sleep(10)
MouseClick("left",510, 614)
Sleep(10)
MouseClick("left",505, 664)
Sleep(10)
WEnd
EndIf

WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func quitter()
Exit
EndFunc

Now that I see it makes me laugh about it :D!

Thank you very much for explaining me those very basic stuff, it helped!

Now I'm on my way on changing the world as we know it!

Thank you all for helping me :)

Link to comment
Share on other sites

Sad or not that sad Bunny, you made my day!

Look how simple this was :)))

HotKeySet("1", "setNote1")
HotKeySet("2", "setNote2")
HotKeySet("3", "setNote3")

HotKeySet("{ESC}", "quitter")

$note1 = false
$note2 = false
$note3 = false

While 1
If $note1 Then
While $note1
Send("{SPACE}")
Sleep(100)
Send("{SPACE}")
Sleep(100)
WEnd
ElseIf $note2 Then
While $note2
MouseClick("left",148, 585)
WEnd
ElseIf $note3 Then
While $note3
MouseClick("left",171, 294)
Sleep(10)
MouseClick("left",233, 293)
Sleep(10)
MouseClick("left",568, 470)
Sleep(10)
MouseClick("left",510, 614)
Sleep(10)
MouseClick("left",505, 664)
Sleep(10)
WEnd
EndIf

WEnd

Func setNote1()
$note1 = Not $note1
ConsoleWrite("Toggled $note1, it is now: " & $note1 & @CRLF)
EndFunc

Func setNote2()
$note2 = Not $note2
ConsoleWrite("Toggled $note2, it is now: " & $note2 & @CRLF)
EndFunc

Func setNote3()
$note3 = Not $note3
ConsoleWrite("Toggled $note3, it is now: " & $note3 & @CRLF)
EndFunc


Func quitter()
Exit
EndFunc

Now that I see it makes me laugh about it :D!

Thank you very much for explaining me those very basic stuff, it helped!

Now I'm on my way on changing the world as we know it!

Thank you all for helping me :)

No problem, I'm happy that I was able to teach you something! :huggles:

Your script looks better. Note, though, that you can leave out the if/elseif/endif in the code you have now, since it's a double check for the same thing. If $note1 is true, then you enter a while loop, whose first action is to check whether $note1 is true (and if so, keep doing what's in the loop until that changes). So, in other words, the while loop actually implies the if already. And since the while loop keeps on running while $note1 is true, it will not get to checking for $note2 before $note1 becomes false. So the two while loops after eachother actually imply the ElseIf.

Also, because you are still using if/elseif and are still using while $note1, you are still not having your actions performed at the same time. You could work on that if you want to perfect it further. Good luck! :)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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...