LightLoop

Author Topic: LightLoop  (Read 460 times)

I was just going to see what happens if I make my light just keep turning
on and off...Well I expected it to do that, but it didn't happen. It just made
me crash. Here's the code:

Code: [Select]
function LightLoop()
{
commandToServer('Light');
schedule(1000,0,LightLoop());
}

And whenever I type LightLoop(); into the console, I just crash.
Anyone know why?

Remove the () after LightLoop in the schedule

Remove the () after LightLoop in the schedule
Oh wow, thanks.

One more thing,
 
                     I tried stopping it by saying LightLoop(0); but it wouldn't stop.
                                   
                                 Does that mean I have to make a new function for that?

You schedule the function every time you call it.
What you need to do is to cancel that schedule.
Right now, you have no way of doing that because the schedule isnt stored anywhere. What you're going to want to do is take the schedule, put it in a variable, and then use the cancel() function later on the schedule to delete it. The flow of your program would kinda look like this .

Code: [Select]
//All code is loaded up and ready to go.
lightloop(); //The first call

//Now we're running LightLoop
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop); //We set the newly created schedule to our variable
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop); //Old schedule expired and we have a new one, so put this on in the variable now
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop);
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop);
//Sometime before the schedule expires and the function runs
cancel($lightLoopSched);
//The function doesnt run again because you stopped the schedule from expiring and running the function again.
//Code ends here

A few more things to note.
You can put the cancel function in another function if you want, it might make things easier or it might not.
Its good practice if you have a function that loops itself by schedule, whenever you enter, it cancels any outstanding schedules of itself. If you identically call the function twice you may end up getting two schedule loops so it helps to prevent any of that from ever occurring.
« Last Edit: February 28, 2013, 11:49:57 PM by DYLANzzz »

You schedule the function every time you call it.
What you need to do is to cancel that schedule.
Right now, you have no way of doing that because the schedule isnt stored anywhere. What you're going to want to do is take the schedule, put it in a variable, and then use the cancel() function later on the schedule to delete it. The flow of your program would kinda look like this .

Code: [Select]
//All code is loaded up and ready to go.
lightloop(); //The first call

//Now we're running LightLoop
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop); //We set the newly created schedule to our variable
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop); //Old schedule expired and we have a new one, so put this on in the variable now
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop);
//After 1 second
commandToServer('Light');
$lightLoopSched = schedule(1000,0,LightLoop);
//Sometime before the schedule expires and the function runs
cancel($lightLoopSched);
//The function doesnt run again because you stopped the schedule from expiring and running the function again.
//Code ends here

A few more things to note.
You can put the cancel function in another function if you want, it might make things easier or it might not.
Its good practice if you have a function that loops itself by schedule, whenever you enter, it cancels any outstanding schedules of itself. If you identically call the function twice you may end up getting two schedule loops so it helps to prevent any of that from ever occurring.
Hm. I was thinking just package it, but this looks good. Thanks.

Btw if you replace that 1000 with a 1 you will create ghosting and the host of the server will ban you.

Btw if you replace that 1000 with a 1 you will create ghosting and the host of the server will ban you.

Not after r1808.

  • Minor flood protection added to /light command



Btw if you replace that 1000 with a 1 you will create ghosting and the host of the server will ban you.

Why 1?

0 => next frame
1 => nearest frame after 16 ms
2 => nearest frame after 16 ms
3 => nearest frame after 16 ms
...
15 => nearest frame after 16 ms
16 => nearest frame after 16 ms
17 => nearest frame after 17 ms
18 => nearest frame after 18 ms
« Last Edit: March 01, 2013, 03:15:04 AM by Port »