Author Topic: Looping a function  (Read 3175 times)

Eval is an advanced function - people encouraging it's usage in this thread for a loop are being handicapped and should stop trying to "teach" people. soba and Superb (sick cunt) have the right idea here.
« Last Edit: November 18, 2011, 12:07:59 AM by Ephialtes »

I guess I just like recursion scripts that can handle more kinds of input too much.  It's hard to be recursive without eval.
And I assumed that he meant looping an existing function, not editing a function to be looped.

So yes, if possible the best solution would be to add a schedule like soba's in a situation where the schedule will cause itself to be triggered again.  Just make sure you can stop the loop.
I do not believe that cancelling the schedule is needed unless it is important to the way the function works, because that will otherwise likely cause problems if the function should be able to loop independantly of other loops.

Anything I missed?
Glad this didnt spiral completely out of control...
« Last Edit: November 19, 2011, 01:49:20 PM by Nexus »

And I assumed that he meant looping an existing function, not editing a function to be looped.
In which case the guy was probably right, call would work better. But whatever, hopefully the OP learned something.

In which case the guy was probably right, call would work better. But whatever, hopefully the OP learned something.

waitwaitwait

call() is a function?  I thought he was just using something ambiguous as a placeholder to show that he would be calling some function.

I didn't realize you could do something like call('functionname');
I thought keybinds and schedule and servercommands did something like
Code: [Select]
eval(%command @ "(" @ %args @ ");");except less obviously flawed, like stripping %command of spaces and symbols and such.

Thats actually really useful!

So yea I guess alot of us were partially wrong, or at least not entirely correct.
I feel like facepalming
« Last Edit: November 18, 2011, 12:54:24 AM by Nexus »

Sorry about the ranty tangents, not nearly enough sleep recently. It seems like just about every terrible post I have ever made (here and elsewhere) was late at night without enough sleep, and I never realize it at the time.

How do I loop a function?
I can't seem to figure it out!

Why make things so complicated?
Code: [Select]
function doSomething()
 {
  echo("doing something");
 }

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


but if you INSIST on using schedule and other such complications, you can do this:
Code: [Select]
function doLoop(%count)
  {
   if (%count < 1)
     return;

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

Why make things so complicated?
Code: [Select]
function doSomething()
 {
  echo("doing something");
 }

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

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.

Why make things so complicated?
Code: [Select]
function doSomething()
 {
  echo("doing something");
 }

function doLoop(%count)
  {
   for (%a=0; %a < %count; %a++)
    {
      doSomething();
    }
  }
How can I set certain seconds of interval between each loop?
« Last Edit: November 18, 2011, 08:10:35 PM by Aide33 »

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: [Select]
schedule(2000, 0, "foo");
while if you wanted to call bar(42, 7, "hi, mom") five and a half seconds later, you could use

Code: [Select]
schedule(5500, 0, "bar", 42, 7, "hi, mom");

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).
« Last Edit: November 18, 2011, 08:18:45 PM by Uristqwerty »

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.
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.



We already went over this.

He was wrong to an extent.

I was wrong to an extent.

You are also wrong to an extent.  You have a vague memory of one mod I released as a beta test looking for feedback with a poorly named SO.  I can clearly see that he is proficient at scripting, but that post of your was unjustified and is stirring up stuff that already died away.  Get out, stay out, and let this end.

You are also wrong to an extent.  You have a vague memory of one mod I released as a beta test looking for feedback with a poorly named SO.  I can clearly see that he is proficient at scripting, but that post of your was unjustified and is stirring up stuff that already died away.  Get out, stay out, and let this end.
No, you didn't confirm the reason eval is bad so I'd thought I'd reinforce it. I'm pretty sure I'm allowed to contribute to any debate in a topic, and I only just saw this today. It's not like this thread is going to be buried instantly in millions of other topics asking for help.

It's not like this thread is going to be buried instantly in millions of other topics asking for help.
It's true, coding help is not too popular.

No, you didn't confirm the reason eval is bad so I'd thought I'd reinforce it. I'm pretty sure I'm allowed to contribute to any debate in a topic, and I only just saw this today. It's not like this thread is going to be buried instantly in millions of other topics asking for help.

I didn't mean the topic, I meant the arguing.

I really like eval for almost everything I do because it makes it universal.  Sure if you take malicious advice and put it in a place where it can be exploited, that is not good, but I am not going to confirm that eval is bad.  I say the earlier a scripter learns about how to use eval, the better.

The guy was looking for a way to loop a function he probably would make himself.
He wasn't looking for a universal loop that he can just slap into his code and everything can work.


Just admit that your example was really bad and that's what the whole argument was about.