Author Topic: Looping/Repeating function  (Read 759 times)

What would the function or command be for looping things?

Depends if you want a delay between loops.

If not:

function loop(%times)
{
    for(%i = 0; %i < %times; %i++)
    {
         echo(%i);
    }
}

In the console this would work like so:
Code: [Select]
>loop(10);
0
1
2
3
4
5
6
7
8
9
10

However if you want to have a delay between loops, you can loop a function by calling itself with a schedule, like so:

function loop(%delay)
{
    cancel($schedule); //Prevent multiple loops from happening if the function is called more that once
    echo("Loop");
    $schedule = schedule(%delay, 0, loop, %delay); //Calls the function loop after %delay seconds
}


Code: [Select]
    cancel($schedule); [color=green]//Prevent multiple loops from happening if the function is called more that once[/color]
Why are you cancelling a variable that does never get set?
EDIT: Fixed

A perfect reason not to lock coding help topics!
« Last Edit: January 24, 2013, 01:34:20 PM by Fluff-is-back »

Thanks a lot! This answered my question.

Code: [Select]
    cancel($schedule); [color=green]//Prevent multiple loops from happening if the function is called more that once[/color]
Why are you cancelling a variable that does never get set?

Before canceling a schedule you should do an if(isEventPending($schedule)) check.
That checks if the schedule is active or not.

Before canceling a schedule you should do an if(isEventPending($schedule)) check.
That checks if the schedule is active or not.
Pretty sure the cancel function does this already.

Code: [Select]
    cancel($schedule); [color=green]//Prevent multiple loops from happening if the function is called more that once[/color]
Why are you cancelling a variable that does never get set?

Well, if he set $schedule to the result of the schedule call on line 5, he could cancel it that way.

Warning - while you were typing a new reply has been posted. You may wish to review your post.

Pretty sure the cancel function does this already.

Why'd you ask the question if you knew the answer? Anyway, it's generally good coding practice.



Before canceling a schedule you should do antPending($schedule)) check.
That checks if the schedule is active or not.

@your PM: How about you do it so I can enter this time :cookieMonster:

@your PM: How about you do it so I can enter this time :cookieMonster:

I wouldn't be a very good judge :panda:

Are you banned from sending PMs?

@your PM: How about you do it so I can enter this time :cookieMonster:

Talking about another 1024 byte scripting contest?

Before canceling a schedule you should do an if(isEventPending($schedule)) check.
That checks if the schedule is active or not.

While it's good coding practice in most other languages, it really doesn't matter in TorqueScript.