Sign in to follow this  
Followers 0
Harrie38

For next from within function not working properly?

15 posts in this topic

What am I doing wrong?

I wrote a script to do a countdown (actually I'm using it to slowly get the "progress window" to zero).

Here's the script:

Global $Progr

$Progr = 10

Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $Progr = $Progr to 0 Step -1
  Sleep(500)
Next
Msgbox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $Progr = $Progr to 0 Step -1
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

I want to do a 5 seconds countdown twice, one from the basic program and one from a function.

The 5 seconds countdown from the basic program is working correctly, but the one in the function is taking less then a second to finish (It's exactly the same code!)

Am I doing something wrong here or is it an AutoIT bug? (hopefully the first item ;-))

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

HI,

$Progr = 10

Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $i = 0 to $Progr
  Sleep(500)
Next
Msgbox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $i = 0 to $Progr
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

Edit: Have a look at TimerDiff() :lmao:

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

What am I doing wrong?

I wrote a script to do a countdown (actually I'm using it to slowly get the "progress window" to zero).

Here's the script:

Global $Progr

$Progr = 10

Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $Progr = $Progr to 0 Step -1
  Sleep(500)
Next
Msgbox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $Progr = $Progr to 0 Step -1
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

I want to do a 5 seconds countdown twice, one from the basic program and one from a function.

The 5 seconds countdown from the basic program is working correctly, but the one in the function is taking less then a second to finish (It's exactly the same code!)

Am I doing something wrong here or is it an AutoIT bug? (hopefully the first item ;-))

You didn't reset the time variable. Put $Progr = 10 in your Countdown() function before the For line.

:lmao:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

You didn't reset the time variable. Put $Progr = 10 in your Countdown() function before the For line.

:lmao:

That's right, but it doesn't solve the problem. The next program is as you stated it:

Global $Progr

$Progr = 10

Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $Progr = $Progr to 0 Step -1
  Sleep(500)
Next
Msgbox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
  $Progr = 10
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $Progr = $Progr to 0 Step -1
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

This gives the same result!

Share this post


Link to post
Share on other sites

Hi,

did you try my script?

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

This gives the same result!

bad style will give bad results ;-))

Replace:

$Progr = 10 with $Start = 10

and

For $Progr = $Progr to 0 Step -1 with For $Progr = $Start to 0 Step -1

and it will work.

Now for the problem. I don't know what's causing the error. Your code should work and

if I print the value of $Progr before the For loop it shows 10. However Within the For loop

it shows 0, which immediately ends the For loop. Could be a bug....

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Hi,

the problem is that it should be

Global $Progr = 10

MsgBox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $Progr = $Progr To 0 Step - 1
    Sleep(500)
Next
MsgBox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
    Local $Progr = 10
    MsgBox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
    For $Progr = $Progr To 0 Step - 1
        ConsoleWrite($Progr)
        Sleep(500)
    Next
    MsgBox(160, "Timer", "5 Seconds passed!")
EndFunc   ;==>Countdown

otherwise $Progr is 0 in the func. The $var isn't set to 10.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

otherwise $Progr is 0 in the func. The $var isn't set to 10.

it is set, but it's changed to 0 in the for loop. See my bug report.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

That's right, but it doesn't solve the problem. The next program is as you stated it:

Global $Progr

$Progr = 10

Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
For $Progr = $Progr to 0 Step -1
  Sleep(500)
Next
Msgbox(160, "Timer", "5 Seconds passed!")

Countdown()

Func Countdown()
  $Progr = 10
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $Progr = $Progr to 0 Step -1
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

This gives the same result!

By golly... it does! :ph34r:

I have no idea why that is. If all you need is a working timer, th.meger did post a better technique, but this is an interesting bug (as far as I can tell). It has to do with using a Global variable in a function, because it works fine if you make it "Local $Progr = 10" inside the function.

Interesting... :lmao:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

By golly... it does! :ph34r:

I have no idea why that is. If all you need is a working timer, th.meger did post a better technique, but this is an interesting bug (as far as I can tell). It has to do with using a Global variable in a function, because it works fine if you make it "Local $Progr = 10" inside the function.

Interesting... :lmao:

I don't need a timer, I'm using the script to slowly show a progressbar that's come from a value from 0 to 100 and must go to zero when the script is cancelled.

There are probably more ways to do it, but I used this one (and it didn't work :geek: )

Thanks for all you're help guys!

Share this post


Link to post
Share on other sites

I don't need a timer, I'm using the script to slowly show a progressbar that's come from a value from 0 to 100 and must go to zero when the script is cancelled.

There are probably more ways to do it, but I used this one (and it didn't work :ph34r: )

Thanks for all you're help guys!

Well, you've got two good tweaks to make what you had work, and I tested both:

1. Make the variable in the function local:

Func Countdown()
  Local $Progr = 10
  Msgbox(160, "Timer", "After pressing OK, the timer is set to 5 seconds and counting down")
  For $Progr = $Progr to 0 Step -1
    Sleep(500)
  Next
  Msgbox(160, "Timer", "5 Seconds passed!")
EndFunc

I think it should have worked with the Global, and that might be a bug, but you have workarounds.

:lmao:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Well, you've got two good tweaks to make what you had work, and I tested both:

:lmao:

Thanks for the good thinking, but I really need the variable $progr. I don't yet know from which point my script will be cancelled, so the value can be anything from 0 to 100.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thanks for the good thinking, but I really need the variable $progr. I don't yet know from which point my script will be cancelled, so the value can be anything from 0 to 100.

Easy tweak, pass it as a parameter to your function:

$Progr = 10
Countdown($Progr)

Func Countdown($Delay)
    Local $t
    MsgBox(160, "Timer", "After pressing OK, the timer is set to " & $Delay & " seconds and counting down")
    For $t = $Delay To 0 Step - 1
        Sleep(1000)
    Next
    MsgBox(160, "Timer", $Delay & " Seconds passed!")
EndFunc   ;==>Countdown

Now you can call it with any number of seconds you like. This is also a style improvement as usage of Global variables inside functions should be minimalized (in my opinion).

:lmao:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks for the good thinking, but I really need the variable $progr. I don't yet know from which point my script will be cancelled, so the value can be anything from 0 to 100.

O.K. but WHY does the counter and the start value have to be the SAME variable???

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

O.K. but WHY does the counter and the start value have to be the SAME variable???

Kurt

You lost me... can you use the variable names in our examples to tell me which you are refering to?

Nevermind, I missed that post was from /dev/null instead of Harie38. :">

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0