Jump to content

Loops and Functions


Recommended Posts

Hello, 

I have a actually a script that is running in a loop until a variable gets a special number.
And this script is fully working and running. Now I need  Functions to repeat different tasks in between the full loop.

While 1

Func Line1()

 

Link to comment
Share on other sites

what functions do you need? Can we see your script? if you post it in the CODE TAGS I bet a really nice person here would probably help you write it if you spell out the requirements and show us what you've tried.

example code in CODE TAGS <>

 

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Hello, 

I have actually a script that is running in a loop until a variable gets a special number.
And this script is fully working and running. Now I need  Functions to repeat different tasks in between the full loop.
In a easy way I want it this way:

$variable =0

While 1

    Func Line1()

        If $variable = 22 Then
        MsgBox(64, 'Finished', 'Task Finished')
        ExitLoop

   EndFunc

$variable +=1
WEnd

 

But i get always this error:

What did I wrong?

 

Unbenannt.png

Edited by Entkalker
Link to comment
Share on other sites

Main()
    While 1
        loop and look for event or values
        If event or value Then 
            Func1()
        Elseif 
            Func2()
        EndIf
    WEnd
EndFunc

Func1()
    Do some func1 stuff
EndFunc

Func2()
    Do some func1 stuff
EndFunc

you don't do it like that. you create separate functions. then you can call them as you need them

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Ok thank you. I think I know how to do it now.
Can I also do the functions above the Main() or is that not possible?

Func1()
    Do some func1 stuff
EndFunc

Func2()
    Do some func1 stuff
EndFunc

Main()
    While 1
        loop and look for event or values
        If event or value Then 
            Func1()
        Elseif 
            Func2()
        EndIf
    WEnd
EndFunc

 

Edited by Entkalker
Link to comment
Share on other sites

As mentioned you should never create functions within a loop.

The issue with your script above:

  • You were just recreating the function over and over.
  • It would never have finished because you never called the function
  • You forgot to add an EndIf within your function

Recommend you use Scite for testing your scripts in Scite Click Tools » Go  or press F5

Here is an example of how you would create your script, although my method above does exactly the same thing.

Global $g_iVariable = 0

While 1
    $g_iVariable += 1
    If Line1() Then ExitLoop
    Sleep(100) ;~ Sleep 100ms between loops
WEnd

Func Line1()
    If $g_iVariable = 22 Then
        MsgBox(64, 'Finished', 'Task Finished')
        Return 1
    EndIf
    Return 0
EndFunc

 

Link to comment
Share on other sites

function ends and passes back to your loop that called it. you loop until that variable is 22 basically. your $g_iVariable is your loop counter that keeps going up and you evaluate that value with Line1

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

  • Moderators

@Entkalker pretty much every question you have asked could easily be answered by trying it for yourself. You would find you get the answers a lot faster than posting and waiting for someone to respond.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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