Author Topic: How would you delay something in a script  (Read 2287 times)

Hello, could someone tell me how you would delay something in a script?

schedule(time, 0, function name, args);

/find phflack, delayed by 1.5 seconds, would be
(commandtoserver(find, phflack);)
schedule(1500, 0, commandtoserver, find, phflack);
« Last Edit: June 20, 2012, 06:25:12 PM by phflack »


as a terrible alternative:

Code: [Select]
function pause(%x)
{
     %start = getrealtime();

     while(getrealtime() - %start < %x)
          continue;
     echo("done.");
}

as a terrible alternative:
Note to OP: Do not use this, it would cause the server to hang until it's finished.

as a terrible alternative:

Code: [Select]
function pause(%x)
{
     %start = getrealtime();

     while(getrealtime() - %start < %x)
          continue;
     echo("done.");
}

= lag the main thread!

Knows all about the main thread...

someone's alt?

Knows all about the main thread...

someone's alt?

We've established that already.

do you even need the continue? or could you just have empty brackets?

do you even need the continue? or could you just have empty brackets?

Empty.

so, what's the point of continue then?
how can it be used where {} can't be?

Well, if you're asking in general, how continue is used, here is an example:

function aiPlayer::findClosestPlayer(%this)
{
   %tp = %this.position;
   %temp = 0;
   
   for(%i=0;%i<clientGroup.getCount();%i++)
   {
      %c = clientGroup.getObject(%i);
      %cp = %c.player;
      
      if(!isObject(%cp)) continue;
      
      %ps = %cp.position;
      %vd = vectorDist(%tp,%ps);
      %tvd = vectorDist(%temp.position,%tp);
      
      if(%temp $= "0")
         %temp = %cp;
      else
      if(%vd < %tvd)
         %temp = %cp;
         
   }

   return %temp;
}


If the client does not have a player, it just goes back to the top, and doesnt run any code below it.

so, what's the point of continue then?
how can it be used where {} can't be?
Continue lets you skip 1 iteration of a while or for loop. In Elm's code, this would do the exact same thing as continue:
Code: [Select]
if(isObject(%cp))
{
    //remaining code
}
In my opinion it's better to use just because it's one line and isn't tedious, and you're using for the exact purpose it was created for

oh right, the skip thing, forgot about that
seems like everything could be done without it though

oh right, the skip thing, forgot about that
seems like everything could be done without it though
Not without some sort of a goto, no.