Blockland Forums > Modification Help
Looping a function
<< < (5/7) > >>
Red_Guy:

--- Quote from: Aide33 on November 17, 2011, 06:43:06 PM ---How do I loop a function?
I can't seem to figure it out!

--- End quote ---

Why make things so complicated?

--- Code: ---function doSomething()
 {
  echo("doing something");
 }

function doLoop(%count)
  {
   for (%a=0; %a < %count; %a++)
    {
      doSomething();
    }
  }

--- End code ---


but if you INSIST on using schedule and other such complications, you can do this:

--- Code: ---function doLoop(%count)
  {
   if (%count < 1)
     return;

   doSomething();
   schedule(100, 0, doLoop, %count - 1);
  }

--- End code ---
Nexus:

--- Quote from: Red_Guy on November 18, 2011, 02:12:22 AM ---Why make things so complicated?

--- Code: ---function doSomething()
 {
  echo("doing something");
 }

function doLoop(%count)
  {
   for (%a=0; %a < %count; %a++)
    {
      doSomething();
    }
  }

--- End code ---

--- End quote ---

Huh, it never occurred to me that this might be what the OP meant.  I think he could have been a little more specific about what he needed it for.
Aide33:

--- Quote from: Red_Guy on November 18, 2011, 02:12:22 AM ---Why make things so complicated?

--- Code: ---function doSomething()
 {
  echo("doing something");
 }

function doLoop(%count)
  {
   for (%a=0; %a < %count; %a++)
    {
      doSomething();
    }
  }

--- End code ---

--- End quote ---
How can I set certain seconds of interval between each loop?
Uristqwerty:
Whenever you need to do something after a delay, you need to use schedule().

Schedule causes a specific function to be run later on. the format is schedule(<delay>, 0, <function>, <0 or more arguments for <function>>);

The 0 (second parameter) has a purpose, but it is not important. The delay is the number of thousandths of a second to wait (1000 means one second). <function> is the name of the function, as a string.

So, if you wanted to call foo() in two seconds, you could use


--- Code: ---schedule(2000, 0, "foo");
--- End code ---

while if you wanted to call bar(42, 7, "hi, mom") five and a half seconds later, you could use


--- Code: ---schedule(5500, 0, "bar", 42, 7, "hi, mom");
--- End code ---


The function name may not need quotes, but that seems to be more of a feature of the engine than of schedule() itself, as echo(hi); does the same thing as echo("hi");. I, personally, wouldn't rely on it without specifically reading it as a feature of the language, though, just in case it causes some subtly different, and unexpected, result.


Edit: Just to be clear, when you use schedule(), the function that called schedule() continues immediately; it doesn't wait until the scheduled function finished. Also, schedule() returns a number that can be given to cancel() to stop the scheduled function from running (but only if it hasn't run yet).

Edit again: Using schedule(), you can create features that aren't otherwise provided, such as repeating a function every N seconds (have the function schedule itself), or stopping in the middle of a function and continuing after a delay (though that tends to require passing a lot of parameters. Using a ScriptObject can help here, either simply as a place to store somewhat more persistant data (creating a new ScripObject and storing data that would otherwise need to be passed as parameters on it, then just passing the ScriptObject), or in a more object-oriented way, by calling the functions on an instance of the ScriptObject itself).
Destiny/Zack0Wack0:

--- Quote from: Nexus on November 17, 2011, 11:48:03 PM ---Jesus, its like an autistic kid with insecurities about his intelligence learned to script.


Don't use eval?

My brief example code has bad namespace?

Never use recursion?


These are all examples of terrible advice.  Ever heard of a default function called messageboxyesno(%title, %text, %yes, %no)  ?
Guess what it does!
eval(%yes); and eval(%no);
Hundreds of blocklanders die every day because of the dangerous side effects of using such a wild and exploitable functions.  Very Tragic.
Do not waste your time on such large paragraphs, no one respects will respect what you have to say about coding after this.

--- End quote ---
Screw you man, qwerty is a loving coding legend. He is one of the people that made me gain interest in coding.

You should NEVER EVER suggest eval to anyone who is new to ANY programming language. Every single programming language ever can be completely forgeted over by someone having no idea what they're doing and using eval. You should only use it if you absolutely have no other way to do it. This guy is new, he'd probably just copy paste it into a mod and release it not realising that it could forget the server over completely.

Your track record for stuff namespaces is not a good one either, so even though qwerty brought you up on an example function, he's not wrong. Understandably Blockland doesn't always need unique namespaces (and I've got my own fair share of mods that probably have generic function names) but from what I recall some of your scripts had some pretty terrible function naming.
Navigation
Message Index
Next page
Previous page

Go to full version