Blockland Forums > Modification Help
Looping a function
(1/7) > >>
Aide33:
How do I loop a function?
I can't seem to figure it out!
Nexus:

--- Code: ---function loop(%code, %delay)
{
   eval(%code);
   schedule(%delay, 0, 'loop', %code, %delay);
}
--- End code ---

I think that should work just fine



Disclaiming disclaimer:
eval() well execute whatever command you put into it, and can be potentially hazardous if used incorrectly.  Be ablsolutely sure any code including eval is idiot proof and will not cause unintended functions to be called.
Greek2me:

--- Quote from: Nexus on November 17, 2011, 06:47:13 PM ---I think that should work just fine

--- End quote ---
Not quite, that'll eventually cause a crash because the schedules don't delete properly or something like that.

Anyway, you just need to cancel your schedule at the beginning of the function.

--- Code: ---function loop(%code, %delay)
{
   cancel($MySchedule);
   eval(%code);
   $MySchedule = schedule(%delay, 0, 'loop', %code, %delay);
}
--- End code ---
Uristqwerty:

--- Quote from: Nexus on November 17, 2011, 06:47:13 PM ---
--- Code: ---function loop(%code, %delay)
{
   eval(%code);
   schedule(%delay, 0, 'loop', %code, %delay);
}
--- End code ---

I think that should work just fine

--- End quote ---

Please don't use eval, except as a very last resort. It greatly increases the chance of unintentionally creating a security flaw.

Also, please don't loop like that, as that loop will NEVER stop until you close the game entirely. Put the schedule inside the function that actually loops, so that you can at least decide to stop looping within that function. Generally, you want to stop looping when the server closes, at the very least...

Something like

--- Code: ---function myLoopingFunction(%args, %are, %great)
{
    if(shouldStopLoopingNow())
    {
        return;
    }

    ... code ...

    schedule(someDelay, 0, myLoopingFunction, %args, %are, %great);
}
--- End code ---

is both more secure, and can be stopped without redefining loop() to not loop anymore.


--- Quote from: Greek2me on November 17, 2011, 07:25:52 PM ---...
Anyway, you just need to cancel your schedule at the beginning of the function.
...

--- End quote ---

Canceling the schedule helps only by ensuring that you only have one loop running at a time, rather than one loop for every time it was started, all running at once.

Of course, that is unless you also cancel it somewhere else as well, as some sort of cleanup.
Amade:

--- Quote from: Greek2me on November 17, 2011, 07:25:52 PM ---Not quite, that'll eventually cause a crash because the schedules don't delete properly or something like that.
--- End quote ---
The reason for cancelling a schedule at the start of a periodic function like that is to prevent multiple loops from being created if the function is executed more than once.


--- Quote from: Uristqwerty on November 17, 2011, 07:31:30 PM ---Please don't use eval, except as a very last resort. It greatly increases the chance of unintentionally creating a security flaw.
--- End quote ---
This is entirely false.
Navigation
Message Index
Next page

Go to full version