Scripting with Lua in Mushclient

by Akraasiel

Back to Mechanic's Corner.

Akraasiel2005-06-30 06:01:31
Well, since it appears that people are converting to Mushclient en masse, it wont do any good if they are turned off by the abomination of a script language that is VBscript, so Im gonna run a few tutorials on working with lua in mushclient.

The biggest feature in lua, and the reason I love it so, is the ease of table manipulation.

To use lua in mushclient youll need to set up you script file, its not to hard, you can write it up in notepad (I prefer to use Wordpad though). If you want to get all fancy with lua later, Id also recommend getting LuaIDE, but its not necessary.


the format for making tables in a script is quite simple, and there are two ways to do it, string tables, and numeric tables (you can also nest string tables within numeric ones)

Say for instance you wanted to create a prioritized list of afflictions, so a that another function could go down the list, and cure the first one that's active in the order, for this we need to have a numeric table, because thats the only sort that can be prioritized.

It would look kinda like this:

herbcures = {
{name = "anorexia", cure = coltsfootpipe, active = false },
{name = "slickness", cure = "calamus", active = false },
{name = "hypochondria", cure = "wormwood", active = false },
{name = "impatient", cure = coltsfootpipe, active = false },
{name = "stupidity", cure = "pennyroyal", active = false }
}

See how there isnt anything in front of the tables? Lua notices that and assumes that its in a numerical order.

Now, lua treats all tables as key value pairs, but in this case we have one table, with our numbers, 1-5 (though we didnt show the numbers) those are our keys, and inside each of the brackets are a bunch of other tables, but inside of the big table, all the stuff in the brackets are our values.

Now if we wanted a function that when called goes down the list and picks the first active aff (where active = true) then we have to make a function for that.

-- those two little lines represent the comment indicator, and text after them
-- isnt parsed by lua

function cureherbafflictions () -- our function to go down the list and cure the
-- first active instance
for key, value in herbcures do -- this uses the for command, which goes down
-- the herbcures table and checks each set of key value pairs for our next if
if v.active == true then -- this means that we will get a hit on instances
-- where the value in our table is true, ie, we are afflicted with it.
Send ("outr " .. v.cure) -- this tells lua to send the text "outr" to
-- Mushclient, followed by the text in the value of cure in the same table that
-- our active = true is
Send ("eat " .. v.cure) -- this does the same as the last but eats it
break -- this stops the for loop from evaluating, otherwise it would
-- keep going and try to cure every active case at once, rather than just the
-- highest on the list
end -- closes our earlier if statement
end -- closes our earlier for statement
end; -- closes the function

now if we stick cureherbafflictions in the little box where it says "script" in our triggers dialouge, whenever one of the active values in the table is true and we see the herb balance message, it will outr and eat the herb to cure it.

Just this little bit of code wont work alone, but coupled with the other lessons, it will help you figure out for yourself how to write a good curing script in lua. (I'd also recommend adding something to the table to mark whether or not youre attempting to cure it, to safeguard against illusions, but thats a later lesson)
Akraasiel2005-07-01 01:34:05
The next lesson is a small one, but very nice to know. It covers two commands. DoAfter, and DoAfterSpecial.

You can find more info on these two in the mushclient help database.

DoAfter sends a command directly to the mud, much like Send ("outr kafe") would, the difference is, it creates a one shot timer, essentially delaying the command in the () by a certain amount of time.

Say youre making a script to cure aeon and anorexia at once

first you would want to cure anorexia, so you would Send ("smokecoltsfootpipe"), which is our alias for smoking our pipe with coltsfoot in it
then we have to wait abit for aeon to let it through, so our next line will be a DoAfter.

DoAfter (2, "drink phlegmatic)

this says to the script, wait 2 seconds, then send this command.

DoAfters can be incrimented up to 1 decimal point, but to do this you have to change the timer interval in the preferences to 0.

DoAfterSpecial is like DoAfter, but you know how in the trigger window you can select where you want the trigger to send the info in the send box? You can use a number correlating with each of those to send the info in a doafter special command like that too.

If you wanted a doafter to use another script after a small pause, you would use

DoAfterSpecial (2, "scriptfunction ()", 12)

2 is the number of seconds it delays, scriptfunction is the function we are sending, and 12 tells lua to send it to the script engine.
Sekreh2005-07-01 01:58:29
Oh dear! Are you giving away our (or really your) evil MUSHclient secrets to the masses?
Akraasiel2005-07-01 02:51:18
Im just going over the tools that anyone who works with mush/lua will find after a few weeks, this is mainly just to make it a bit easier to learn the language.