Sign in to follow this  
Followers 0
eastmus

reference to Func in a While loop

7 posts in this topic

how do i refer back to Func ?

example:

$loop=True

Func timer()
    For $i=600 to 0 Step -1 ;600 times/sec = 10mins
        ToolTip($i,0,0) ;count down
        Sleep(1000)  ;sync each turn per sec
    Next
    ToolTip("10 mins has passed",0,0)
EndFunc

While True
    If $loop = True Then
        timer() ;how do i refer back to Func timer() ???
    EndIf
WEnd

the point is to have a script in a while loop to keep jumping up and down in case this or that fails so it would go over this one step again until it succeeds.

Instead of having the whole while loop to end and restart all over...

The script it pretty long, so to save time, i dont want it to start from the top again, if something in the middle of the script went wrong.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Could you please explain what timer() is supposed to do. All it does in that script is set a few tooltips and wait 10 minutes. Also, I think you should take a look at timerinit and timerdiff.

Edited by Hawkwing

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

Could you please explain what timer() is supposed to do. All it does in that script is set a few tooltips and wait 10 minutes. Also, I think you should take a look at timerinit and timerdiff.

timer is not the subject.

thats just an example i am giving here, it could be anything.

the question is : how to call/refer to a func within a while loop

Share this post


Link to post
Share on other sites

You could use a function with parameters

For $i = 0 To 10
    MsgBox(0, "", multiplyby2($i))
Next

Func multiplyby2($var)
    $var *= 2
    Return $var
EndFunc

or you could have a function declare a global variable

declarevar(2)

For $i = 0 To 10
    MsgBox(0, "", $var * $i)
Next

Func  declarevar($var2)
    Global $var = $var2
EndFunc

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

k, im gonna try to explain this better...

with images :D

Posted Image

we control the stickman and he will be doing a lot of actions (POINT A in script below),

then when he gets to POINT B he MUST turn on the light,

if he cant find the light,

he must keep looking again until he found the light and turned it on.

and then continue to POINT C of the script.

#include <ImageSearch.au3> ;i'll be using this to check wether light is on or off
$loop=True

While True
    If $loop = True Then
        ;LOTS of other actions ---- POINT A
        ;LOTS more of other actions
        $lightoff = _ImageSearch("lightoff.bmp",1,$x1,$y1,0) ;check if light is off --- POINT B
        ToolTip("Is the light off?",0,0)
        Sleep(2000) ;2 secs wait
        ToolTip("")
        if $lightoff=1 Then
            MouseClick("left",$x1,$y1) ;mouse left click on the lightoff image
            ToolTip("turning the light back on",0,0)
            Sleep(2000) ;2 sec
            ToolTip("")
        Else
            ;goto POINT B (but GOTO is not in AutoIt anymore!! we have to use Func !
        EndIf
        ;LOTS of other actions
        ;Many more actions coming after --- POINT C
    EndIf
WEnd

The problem with my long-ass script is,

that it sometimes skips the POINT B script and it would continue the script to POINT C.

And I cannot make it wait until it loops again from POINT A and then to check again for the light and click there.

Meanwhile the stickguy is in a total different environment because the script continued and makes the stickman

do many more other actions.

So i need to make a small loop inside this big While loop at POINT B,

to make sure he turns on the light before executing the rest of the script.

I hope its clearer now :D

The helpfile from AutoIt:

4. Where is the "goto" command?

Gone. It's evil. No, you can't ask why - it just is.

It's like that lump of rock they find in the microwave at the end of the film Time Bandits :D

AutoIt v3 features most of the common "loops" in use today and with these Goto is no longer required. Look up While, Do, For, ExitLoop, ContinueLoop and Functions for the modern way of doing things :D And while you are looking at help file sections check out these on loops, conditional statements and functions.

===> bottom of line:

i am working with Functions now (check first post),

but i want to be able to refer to a function so i can make it loop...

inside a loop ...

I need to recall my functions aarrgghhh

thx :D

Edited by eastmus

Share this post


Link to post
Share on other sites

You don't even need functions for this.

Have a look at the following code. Ofcourse it's an abstraction of what you're doing, but you indicated you wanted this level of abstraction.

#include <ImageSearch.au3> ;i'll be using this to check wether light is on or off
$loop=True

While True ; This is completely and utterly useless.
    If $loop = True Then
        ;LOTS of other actions ---- POINT A
        ;LOTS more of other actions
        
        ; Let's think about what we're doing here.
        ; We want to keep doing things and then check if the light is off, until the light is off
        ; This behavior indicates a loop, because you want to keep doing things
        
        $lightoff = 0
        Do
            $lightoff = _ImageSearch("lightoff.bmp",1,$x1,$y1,0) ;check if light is off --- POINT B
            ToolTip("Is the light off?",0,0)
            Sleep(2000) ;2 secs wait
            ToolTip("")
        Until $lightoff = 1
        
        ; When we've reached this part of the script, we know that the light is off
        
        MouseClick("left",$x1,$y1) ;mouse left click on the lightoff image
        ToolTip("turning the light back on",0,0)
        Sleep(2000) ;2 sec
        ToolTip("")
        
        ;LOTS of other actions
        ;Many more actions coming after --- POINT C
    EndIf
WEnd

Share this post


Link to post
Share on other sites

I'm hoping you can give us a better example than that, as it's not complete code so we can't really show you the best way to do it with this code.

Using your example though:

#include <ImageSearch.au3> ;i'll be using this to check wether light is on or off
Global $loop = True, $skipA = 0

While True
    If $loop = True Then
        If $skipA <> 1 Then Acode()
        ToolTip("Is the light off?", 0, 0)
        Sleep(2000) ;2 secs wait
        ToolTip("")
        If LightOff() = 1 Then
            MouseClick("left", $x1, $y1) ;mouse left click on the lightoff image
            ToolTip("turning the light back on", 0, 0)
            Sleep(2000) ;2 sec
            ToolTip("")
            Ccode()
            $skipA = 0
        Else
            $skipA = 1
        EndIf
    EndIf
WEnd

Func LightOff()
    Return _ImageSearch("lightoff.bmp", 1, $x1, $y1, 0) ;check if light is off --- POINT B
EndFunc   ;==>LightOff

Func Acode()
    ;LOTS of other actions ---- POINT A
    ;LOTS more of other actions
EndFunc   ;==>Acode

Func Ccode()
    ;LOTS of other actions
    ;Many more actions coming after --- POINT C
EndFunc   ;==>Ccode

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