Author Topic: how would i slow a for statement?  (Read 1207 times)

for some reason in my for statements, they go quickly, even though i have a delay in the main part, is there a way to slow it down?

Well, you COULD use the extremely complicated way and do:
%counter = 0;
%counterLimit = how long to run the For loop for...;
if(%counter < %counterLimit)
{
schedule(timeToLoopMS,0,commandcontainingthis,args);
%counter ++;
}
etc.

EDIT : Okay, i'll explain it above...
You put that block in your function or whatever.
%counter = 0; keep this
%counterLimit = *IN HERE, put how many loops you want it to do.*;
if(%counter < counterLimit) If the counter has reached the loop maximum, stop.
{
schedule(*HERE:Put how long you want it to be, in MS, between loops.*,0,*The command calling this in the first place.*Any args incmd*);
%counter ++; auto-increment. handy, no?
}
etc. rest of code

example edit:
function serverCmdDelayedCounterLoop(%numToLoop,%delayzor) {
if(%started = 0) //safety first.
{
%counter = 0;
}
%counterLimit = %numtoloop;
if(%counter < %counterLimit)
{
schedule(%delayzor,0,serverCmdDelayedCounterLoop)
%started = 1;
commandToServer("A counter has reached "@%counter@"!");
}
}
when a player in your server says /DelayedCounterLoop 15 100, it will say an incrementing number every 1sec or so for 15 numbers.
« Last Edit: August 27, 2010, 03:51:31 PM by ThinkInvisible »

Code: [Select]
function blahCommand(%stuff) {
if((%s = strLen(%stuff)) > 4)
cycleCommand(%stuff, 0, %s);
}

function cycleCommand(%stuff, %i, %s) {
if(%i <= %s) {
doStuff(%stuff);
schedule(500, 0, cycleCommand, %i++, %s);
}
else
echo("doneStuff");
}

so basicaly you can't delay a for loop or something?


ahh, thanks, is there anyway to stop a function while it's running? as i'm using this in the console to auto eat for the stranded gamemode, as i'm building and i don't want to die when i forget, would this work?
function eat(%i){commandtoserver('consume', "purifiedwater");commandtoserver('consume', "banana");if(%i != 5){schedule(10000, 0, eat);}else{break;}}
so, when i type eat(); it would loop, when i type eat(5);, it would stop?

edit: forgot ask in the OP, how would i put a message before a variable when talking, when using this: commandtoserver('messagesent', %i);

ahh, thanks, is there anyway to stop a function while it's running? as i'm using this in the console to auto eat for the stranded gamemode, as i'm building and i don't want to die when i forget, would this work?
function eat(%i){commandtoserver('consume', "purifiedwater");commandtoserver('consume', "banana");if(%i != 5){schedule(10000, 0, eat);}else{break;}}
so, when i type eat(); it would loop, when i type eat(5);, it would stop?

you would do this:

Code: [Select]
function eat(%i) {
  commandtoserver('consume',"purifiedwater");
  commandtoserver('consume',"banana");
  if(%i !="5") {
    $eatsched=schedule(10000,0,eat);
  }
  else
  {
     cancel($eatsched);
  }
}
This would make it so when %i is anything but 5, it will eat and purifiedwater every 10 seconds. If %i is 5, then the schedule will stop.

but, it will update when i use 5, right? and by the way, i'm just using this for in the console, not for a client_eatcommand thing
edit: thanks, it worked, also, what is the $ sign for? i figured out % is for a variable
« Last Edit: August 27, 2010, 06:04:03 PM by phflack »

but, it will update when i use 5, right?

yes

and by the way, i'm just using this for in the console, not for a client_eatcommand thing
i would suggest just making it as a function in a script so you won't have to retype the whole thing over and over again, which is always a pain. that way, you could call it at any time without having to type the full function.

true, i might go do that, basicaly that's the client.cs, and then add a description.txt and put it in client_eatcommand?
edit: i can put multiple functions in the same client.cs, right?
« Last Edit: August 27, 2010, 06:24:20 PM by phflack »

true, i might go do that, basicaly that's the client.cs, and then add a description.txt and put it in client_eatcommand?
edit: i can put multiple functions in the same client.cs, right?
i don't think you need to have it in a client.cs, it doesn't matter. It's still a function whether it's in a server file or a client file.

Yes, you can have multiple functions; make sure they're separated.

you can just call it "Script_eatcommand", but you could call it client_eatcommand. It's all up to you.


so, i have this in client_commands, and for some reason it won't work
Code: [Select]
function eat(%i)
{
commandtoserver('consume',"purifiedwater");
commandtoserver('consume',"banana");
if(%i != "stop")
{
$eatsched=schedule(10000,0,eat);
}
else
{
cancel($eatsched);
}
}

function countdown(%a, %b)
{
if(%b >= 0)
{
commandtoserver('messagesent', %b);
%b--;
if(%a >= 1)
{
$countdownsched=schedule(5000, 0, countdown);
}
else
{
cancel($countdownsched);
}
}
}

function bottlesofspam(%a, %b)
{
if(%b >= 0)
{
if(%c >= 3)
{
%c = 1;
%b--;
}
else
{
%c++;
}
switch(%c)
{
case 1:
commandtoserver('messagesent', %b @ " bottles of spam on the wall.");
break;
case 2:
commandtoserver('messagesent', %b @ " bottles of spam!);
break;
case 3:
commandtoserver('messagesent', "Take one down, unleash the spam...");
break;
}
if(%a >= 0)
{
schedule(%a, 0, bottlesofspam);
}
}
}
the eat thing i think works fine, but the count down only displays %b, then spams commandtoserver('messagesent');, and then the bottles of spam won't work at all, and it says it has errors in the code

[
Code: [Select]
function eat(%i)
{
commandtoserver('consume',"purifiedwater");
commandtoserver('consume',"banana");
if(%i != "stop")
{
$eatsched=schedule(10000,0,eat,%i);
}
else
{
cancel($eatsched);
}
}

function countdown(%a, %b)
{
if(%b >= 0)
{
commandtoserver('messagesent', %b);
%b--;
if(%a >= 1)
{
$countdownsched=schedule(5000, 0, countdown,%a,%b);
}
else
{
cancel($countdownsched);
}
}
}

function bottlesofspam(%b, %c)
{
if(%b >= 0)
{
if(%c >= 3)
{
%c = 1;
%b--;
}
else
{
%c++;
}
switch(%c)
{
case 1:
commandtoserver('messagesent', %b @ " bottles of spam on the wall.");
break;
case 2:
commandtoserver('messagesent', %b @ " bottles of spam!);
break;
case 3:
commandtoserver('messagesent', "Take one down, unleash the spam...");
break;
}
if(%b >= 0)
{
schedule(1000, 0, bottlesofspam, %b, %c);
}
}
}
Think that should work
« Last Edit: August 31, 2010, 07:02:41 PM by Scout31 »

Edit: Phail double post