Author Topic: for statements?  (Read 1899 times)

How do I use while without crashing the server?
Same way as a for loop. It's really the same basic concept as I said.
For a while loop, you give it a condition in the middle, like while(%number < 5) and it'll continue to loop as long as %number is less than 5. So, if you don't want to make it infiniteloop, just make sure it's equal to or greater than 5 at some point.

How do I use while without crashing the server?

Logic. If what you're telling the while loop to do via code inside of the loop is infinitely true, well then, that's going to cause an infinite loop.

infinite loop:

%a=1;
while(%a==1)
{
     %b++;
}


This will result in an infinite loop because you're never changing the while condition to false.

%a=1;
while(%a==1)
{
     %a++;
}


With this example, the while loop increments %a on each iteration. Once %a is not equal 1, the condition is then false which causes the loop to break; hence while(condition is true).

What if I want to constantly update a botttomprint to someone, though?
Like, constantly display an updated variable?


Utilize the schedule method.
I tried this before, but it keeps crashing.
Sorry if I sound dumb, I'm really confused.

Paste your code here.

Quote
function servercmdgetpos(%client) {
   %long = getwords(%client.player.getposition(),0,0);
   %lat = getwords(%client.player.getposition(),1,1);
   %alt = getwords(%client.player.getposition(),2,2);
   //Let's round.
   %alt = mfloatlength(%alt,0);
   //if it's on, let's shut it off like a switch. not to be confused with switch()
   if(%client.isGPOn)
   {
      %client.isGPOn = 0;
      messageclient(%client,'',"\c6Position Bottomprint is now off");
      //we want it to stop saying stuff when the client turns it off. this makes that happen.
      %client.bottomprint('',1,1);
      return;
   }
   //if it isn't already on, let's turn it on.
   if(!%client.isGPOn) {
   %client.isGPOn = 1;
   messageclient(%client,'',"\c6Position Bottomprint is now on");
   //we aren't using an iteration, we are just looping. therefore, i will use an unused variable as the iteration
   while(%client.isGPOn) {
      schedule(10,0,%client.bottomprint,"\c3Longitude:\c6" SPC %long SPC "\c3Latitude:\c6" SPC %lat SPC "\c3Altitude:\c6" SPC %alt,1,1");
   }
   }
   
}
I also can't figure out why it wont toggle on and off, I've been staring at it.
Ignore the notes, forgot to pull some of them out.
« Last Edit: April 30, 2015, 11:56:10 PM by -Setro- »

Yikes, you don't use schedules like that, and that while loop will definitely cause a crash. This is usually how a schedule is structured in a basic function:

function functionName(%args)
{
     if(isEventPending($functionNameLoop))
          cancel($functionNameLoop);

     //code...
     //more code...

     //schedule(int milliseconds, object, function, arguments);
     $functionNameLoop = schedule(1000,0,functionName,%args);
}


This will cause the function to call itself every 1000 milliseconds, which is 1 second.
« Last Edit: May 01, 2015, 05:12:32 PM by elm »


Why doesn't break; work for stopping a loop?

Why doesn't break; work for stopping a loop?

But it does? If your code isn't working the way it's supposed to remember you have to post it for people to be able to help

I was referring to no one mentioning it, or at least as far as I noticed.