Jump to content
Entkalker

Loops and Functions

Recommended Posts

Entkalker

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()

 

Share this post


Link to post
Share on other sites
Earthshine

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

 

Share this post


Link to post
Share on other sites
Entkalker
Posted (edited)

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

Share this post


Link to post
Share on other sites
Subz

You should never create a function within a loop, you basically only had to do the following:

$iVariable = 0
While 1
    $iVariable +=1
    If $iVariable = 22 Then ExitLoop MsgBox(64, 'Finished', 'Task Finished')
    Sleep(1000) ;~ Sleep 1 Second
WEnd

 

Share this post


Link to post
Share on other sites
Earthshine

why a new post?


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
Entkalker

Is there no possibility to put the function into the loop?
Or can I do jumps without functions?

Share this post


Link to post
Share on other sites
JLogan3o13

Stick to one thread, please.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)
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

 

Share this post


Link to post
Share on other sites
Entkalker
Posted (edited)

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

Share this post


Link to post
Share on other sites
Subz

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

 

Share this post


Link to post
Share on other sites
Entkalker

Ok im really new with functions, so my question is what will the script do next after finishing the function?

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

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

 

Share this post


Link to post
Share on other sites
Entkalker

ok so i only need to put the function out of the loop and call it in the loop?

And after the function ends the loop will continue after the call function line?

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

yes indeed. When Line1() returns a 1 and that exits the main loop.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
JLogan3o13

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

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Entkalker

So I got it and it works thank you all for your help

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

×