MUSH throws an error when I try to enable scripting

Posted by Drakonik on Wed 02 May 2007 10:52 PM — 10 posts, 42,335 views.

#0
Here's the error I get: http://img123.imageshack.us/img123/8893/errorlv1.png

I don't know if I missed some step in the installation, but I'd like to finally get scripting working. Thanks in advance.
Australia Forum Administrator #1
This question comes up a few times, so I have amended the FAQ to handle it. See:

http://mushclient.com/faq -- point 42

Also see this forum posting:

http://www.gammon.com.au/forum/bbshowpost.php?id=3035
#2
Everything works now, but I've got one last question.

I know how to use triggers or aliases to pass things to a script. I just don't know how to access those inside my script.

I'm planning on using the %0 to pass the whole trigger line to my script. I don't know how to take the contents of %0 and store them in some variable in my Python.
Australia Forum Administrator #3
Take a look at the file exampscript.pys that comes with MUSHclient - that shows accessing wildcards in Python.
#4
I'm confused now. I've looked through the example script file, and it's got an example function doing essentially what I want, but nowhere in the whole file are the variables defined. Understandably, the client pukes when I try to run the function from my trigger.
Australia Forum Administrator #5
What is the function you are running, and what is the error message?
#6
I'm using a trigger to catch a line of text, and trying to get the ExampleTrigger from the very bottom of the example script page to print out the line it caught.

When I try to run it, it tells me that the variables aren't defined.

Edit: Also, I was wondering if there was some way I could put a forced wait into my aliases. Can I make an alias that will 'prepare spell\n wait three second\n Cast spell' without having to resort to using an external scripting language?
Amended on Sat 19 May 2007 04:39 PM by Drakonik
Australia Forum Administrator #7
Quote:

Can I make an alias that will 'prepare spell\n wait three second\n Cast spell'


Have you read the FAQ? ...

http://mushclient.com/faq , item 25
Australia Forum Administrator #8
I loaded up the example file and got no error. I can't help you if you don't copy and paste the actual error message.
#9
From reading this thread I think what Drakonik is doing is creating the alias/trigger as Send to: Script and putting the function call in the Send box (complete with the variables as named in ExampScript.pys).

What he wants to do is either keep the Send to: Script setting, leave the Script box at the bottom of the dialog empty and then fill the Send box in with the name of the function he wants to call, passing the % variables in quotes (as the Send box does direct substitution).

So the Send box should contain something like:
MyScriptFunction("""%0""")


I use the """ quotes as direct substitution means if I used ' or " and %0 has a ' or " in it then Bad Things will happen.

OR, ALTERNATIVELY: (And this is what I'd recommend)
Ignore the Send to: setting, leave the Send box completely blank and fill the Script box with the name (and just the name, no parentheses, no variable names) of the function you want called with the alias/trigger fires. This means, the example should have:
ExampleTrigger

in the Script box and nothing else.

Drakonik, my ExampScript.pys has:
def ExampleTrigger (thename, theoutput, wildcards):
  world.note ("Trigger " + thename + " fired.");
  world.note ("Matching line was: " + theoutput);
  for i in range (10):
    if wildcards [ i ]:
       world.note ("Wildcard " + str(i) + " = " + wildcards [ i ])

The "def" means we are defining a function, that happens to be named "ExampleTrigger". It takes exactly three arguments. The first line also tells us that whatever is actually passed to the function, the first argument should be accessible to the code within the function under the name: "thename", the second: "theoutput" and the third: "wildcards".

Those three variable names could be anything because they only have scope from the "def" until we reach the same indentation level in the file (that would be the line after what I pasted above).

For example:
def ExampleTrigger(n, o, w):

is equally valid but less useful as we don't have as much information about what we should pass to ExampleTrigger. Though in this case ExampleTrigger is actually being called from the MUSHclient main C++ thread, which will always pass the name of the trigger that matched, the last line that matched and the contents of the wildcards in the match-phrase.

If we wanted to call ExampleTrigger ourselves, we'd put on a line by itself (appropriately indented):
    ExampleTrigger("SomeRandomTrigger", "This is an example line of text that ExampleTrigger thinks we just matched.", [])


I don't know if I can elaborate further than that?

BTW, Nick, one of the reasons I love Python is that there are so many ways to do simple things. I started out thinking there had to be an easier way of writing the last three lines of ExampleTrigger, and ended up with:
    [world.Note("Wildcard %d = %s" % (i, w)) for i, w in dict(zip(range(1, len(wildcards)), wildcards[1:])).iteritems() if w]

Which works, but doesn't cover "easier" or "simpler" :D

If you ignore the number, you can get away with:
    [world.Note("Wildcard = %s" % el) for el in wildcards]

OR, for really raw output (no prefixes):
    world.Note("\n".join(wildcards))

In practise I'd enclose the actual wildcard data in repr()s to be sure a blank line was an '' rather than " " or "\n".
    world.Note("\n".join([repr(el) for el in wildcards]))


EDIT: Bah. Yet Again the Python designers are one step ahead of me :)
    [world.Note("Wildcard %d = %s" % en) for en in enumerate(wildcards)]