Page 1 of 1

Scripting a pause in triggers?

Posted: 08 Jan 2021 05:13
by Arsonist
I have made many aliases and triggers. Some of these get to be very long without a pause (traveling to distant lands), adding a pause would allow my computer some time to think between requests.
I know a little of the JS and have an adapted wait function from a previous forum page (that is long and tiresome to copy). Is there a simple wait function that I am to blind to see? Thanks for your help!

Re: Scripting a pause in triggers?

Posted: 08 Jan 2021 10:53
by nils
Use do-paths.

"do n, n, n, n, n, w, w, knock on gate, w, n, nw, climb tree, ne, n"

Re: Scripting a pause in triggers?

Posted: 08 Jan 2021 11:17
by Zhar
If you want to add a single pause: https://www.w3schools.com/jsref/met_win_settimeout.asp
If you want to add pause between each step: https://www.w3schools.com/jsref/met_win_setinterval.asp

So, for a path to execute with 1 second delays you could simply put it into an array and then iterate over it with setInterval().

Example:

Code: Select all

let i = 0;
const ary = ["ne", "ne", "e", "e", "se", "nw", "ne", "ne", "se"];
var myIter;

function iter() {
   console.log(ary[i]);
   
   i += 1;
}

function startIter() {
  i = 0;
  myIter = setInterval(iter, 1000);
}

function stopIter() {
   clearInterval(myIter);
}

Re: Scripting a pause in triggers?

Posted: 08 Jan 2021 14:43
by Arsonist
Thank you for the links to the webpages! it helped a lot!

Re: Scripting a pause in triggers?

Posted: 10 Jan 2021 20:51
by Callimachus
That's great! Thanks, Zhar!

But... Isn't clearInterval()buggy in the webclient? I use it in another alias, but it doesn't seem to actually clear the flag.

Re: Scripting a pause in triggers?

Posted: 10 Jan 2021 22:39
by cotillion
Zhar wrote:
08 Jan 2021 11:17
If you want to add a single pause: https://www.w3schools.com/jsref/met_win_settimeout.asp
If you want to add pause between each step: https://www.w3schools.com/jsref/met_win_setinterval.asp

So, for a path to execute with 1 second delays you could simply put it into an array and then iterate over it with setInterval().

Example:

Code: Select all

let i = 0;
const ary = ["ne", "ne", "e", "e", "se", "nw", "ne", "ne", "se"];
var myIter;

function iter() {
   console.log(ary[i]);
   
   i += 1;
}

function startIter() {
  i = 0;
  myIter = setInterval(iter, 1000);
}

function stopIter() {
   clearInterval(myIter);
}
This seems a bit convoluted, try:

Code: Select all

/* jshint ignore:start */
const path = ["ne", "ne", "e", "e", "se", "nw", "ne", "ne", "se"];  

for (const cmd of path) {
  mud.connection.send(cmd)
  await wait(0.5)   
}

Re: Scripting a pause in triggers?

Posted: 10 Jan 2021 23:41
by Zhar
Sorry, JS is not really my forte.

Callimachus, to be able to clear an interval you need to assign it to a variable first. But what Cotillion posted is indeed much more clear in its intent.