Page 1 of 1

JavaScript: Sending multiple commands at once

Posted: 06 Oct 2018 16:46
by Kwevin
So I'm trying to make an herbing script for the web client, and I need it to send multiple commands at the same time. Right now my code looks something like this:

gwc.connection.send("disembark");
if (gwc.gmcp.data.room.id == "6fTEU.") {
gwc.connection.send("n");
gwc.connection.send("n");
gwc.connection.send("search here for herbs");
}

In Mudlet, all I'd have to do is use "sendAll". Is there any way I can condense potentially hundreds of commands into one line of JS?

Re: JavaScript: Sending multiple commands at once

Posted: 06 Oct 2018 23:50
by cotillion
uhm?

[ 'n', 'e', 'w' ].map((word) => gwc.connection.send)

Re: JavaScript: Sending multiple commands at once

Posted: 08 Oct 2018 18:12
by Shanoga
Another way would be something like this...

Code: Select all

if (gwc.gmcp.data.room.id == "6fTEU.") {
  actions = ['n', 'n', 'search here for herbs'];
}

actions.forEach(function(action) {
  gwc.connection.send(action);
});
What you do here is you create an array with each of the commands you would like to send, then you use a forEach function on that array to execute the gwc.connection.send function on each element of that array. If you have multiple room IDs you want to check, then have a long series of if--elseif--else conditions and use the last few lines at the end of the script.