Shryke2005-02-27 01:19:38
I just got python for mushclient, and it works, but I can't figure out how to actually write a script for it! Do I write in python, and if so how (when I enter code it always says the syntax is wrong, or when I type something like "var_lala=input(What is lala?)" it prompts "what is lala?" If not do I write it in notepad and somehow translate it?
Thanks in advance..
Thanks in advance..
Unknown2005-02-27 02:06:51
--- Specific instructions for creating a script are at the bottom. ---
There are 3 ways of using scripts in Mushclient (actually 4 but 2 are just variations of the same method):
1. Executing scripts from the command window
2. Executing scripts from triggers/aliases
3. Executing scripts from script files
To understand how what works, you also need to know about "script spaces" or "script scopes". Mushclient has two general containers for aliases/triggers/scripts/timers/etc. These containers are the world file, which is what you open when you want to connect to a MUD with some character, and plugins. Plugins are basically small world files, but they co-exist inside one world file and can be shared by world files. Plugins inside the world file, and the world file itself, have separate "script scopes". This means that scripts loaded in one plugin can't see scripts loaded in other plugins or the world file, the same goes for the world file. Now on to the 3 methods above:
1. This is the simplest method of running a script. The script entered in the command window is executed immediately as you hit Enter and the execution takes place in the world file's script scope. In order for this to work you must have Scripting enabled, and must have selected a scripting language that you are trying to use. This is done in the World options->Scripting dialogue. To enter a script you need to prepend it with the "script prefix" character, so the client knows that you are launching a script and not issueing a regular command. The default script prefix char is "\\" and can be changed in the same Scripting dialogue.
2. You can also put a script inside a Send field of a trigger/alias. That script will be launched in the script space of the container where the trigger/alias is defined. In order for this to work you need to select "Script" from the "Send to" dropdown in the Trigger/Alias edit dialogue. No special prefixes are needed.
3. This is the fastest (in terms of performance) method. Scripts can be written in text files and attached either to a world file or a plugin and will have the scope of that world/plugin. Triggers/aliases/timers will be able to call parts of the script attached in their scope. There are two ways to call the script inside a file: triggers/aliases/timers have a "Script" option, where you can put the name of a function defined inside the script file. Mushclient calls these functions in a consistent way: it passes a certain number of arguments to them and those arguments have constant meaning. Triggers and aliases pass exactly 3 arguments to the script function, those arguments are, in order - the name of the calling trigger/alias, the contents of the Send field of the trigger/alias, a sequence (a list in Python's case) which contains all the wildcards captured by the trigger/alias in the order in which they appear in the trigger's match expression. Therefore, a script function must accept exactly 3 arguments if it must be callable this way. Here's an example of such a function:
def TestTriggerFunc(name, output, wildcs):
  world.Note("The name is %s" % name)
  world.Note("The output is %s" % output)
  world.Note("The wildcards are:")
  for w in wildcs:
    world.Note(w)
Another way is to call the function using the method 2 above (Send to script). You can simply put the function call inside the Send box and select Script for the Send to option. That way your function can accept any number of arguments, or no arguments at all. That isn't limited to functions only - triggers/aliases can manipulate the script existing in the same script space where they are defined in the same way that code inside the script file can manipulate parts of itself.
Timers are different from triggers/aliases only in that instead of 3 arguments they pass only 1 argument to functions called through the Script option: their name.
The best way to start using scripting is to create a script file with the function above, save it as test.pys (not test.py - Mushclient needs the .pys extension). Then go to the Scripting dialogue, select Python as the scripting language, and open that file from their. Add an alias that matches on something like "test * *", name it "test_alias", and put "TestTriggerFunc" in the Script field. Then input "test Python script" and see what happens.
There are 3 ways of using scripts in Mushclient (actually 4 but 2 are just variations of the same method):
1. Executing scripts from the command window
2. Executing scripts from triggers/aliases
3. Executing scripts from script files
To understand how what works, you also need to know about "script spaces" or "script scopes". Mushclient has two general containers for aliases/triggers/scripts/timers/etc. These containers are the world file, which is what you open when you want to connect to a MUD with some character, and plugins. Plugins are basically small world files, but they co-exist inside one world file and can be shared by world files. Plugins inside the world file, and the world file itself, have separate "script scopes". This means that scripts loaded in one plugin can't see scripts loaded in other plugins or the world file, the same goes for the world file. Now on to the 3 methods above:
1. This is the simplest method of running a script. The script entered in the command window is executed immediately as you hit Enter and the execution takes place in the world file's script scope. In order for this to work you must have Scripting enabled, and must have selected a scripting language that you are trying to use. This is done in the World options->Scripting dialogue. To enter a script you need to prepend it with the "script prefix" character, so the client knows that you are launching a script and not issueing a regular command. The default script prefix char is "\\" and can be changed in the same Scripting dialogue.
2. You can also put a script inside a Send field of a trigger/alias. That script will be launched in the script space of the container where the trigger/alias is defined. In order for this to work you need to select "Script" from the "Send to" dropdown in the Trigger/Alias edit dialogue. No special prefixes are needed.
3. This is the fastest (in terms of performance) method. Scripts can be written in text files and attached either to a world file or a plugin and will have the scope of that world/plugin. Triggers/aliases/timers will be able to call parts of the script attached in their scope. There are two ways to call the script inside a file: triggers/aliases/timers have a "Script" option, where you can put the name of a function defined inside the script file. Mushclient calls these functions in a consistent way: it passes a certain number of arguments to them and those arguments have constant meaning. Triggers and aliases pass exactly 3 arguments to the script function, those arguments are, in order - the name of the calling trigger/alias, the contents of the Send field of the trigger/alias, a sequence (a list in Python's case) which contains all the wildcards captured by the trigger/alias in the order in which they appear in the trigger's match expression. Therefore, a script function must accept exactly 3 arguments if it must be callable this way. Here's an example of such a function:
CODE
def TestTriggerFunc(name, output, wildcs):
  world.Note("The name is %s" % name)
  world.Note("The output is %s" % output)
  world.Note("The wildcards are:")
  for w in wildcs:
    world.Note(w)
Another way is to call the function using the method 2 above (Send to script). You can simply put the function call inside the Send box and select Script for the Send to option. That way your function can accept any number of arguments, or no arguments at all. That isn't limited to functions only - triggers/aliases can manipulate the script existing in the same script space where they are defined in the same way that code inside the script file can manipulate parts of itself.
Timers are different from triggers/aliases only in that instead of 3 arguments they pass only 1 argument to functions called through the Script option: their name.
The best way to start using scripting is to create a script file with the function above, save it as test.pys (not test.py - Mushclient needs the .pys extension). Then go to the Scripting dialogue, select Python as the scripting language, and open that file from their. Add an alias that matches on something like "test * *", name it "test_alias", and put "TestTriggerFunc" in the Script field. Then input "test Python script" and see what happens.
Shryke2005-02-27 17:11:19
When I tried to do that I went to scripting and imported it or whatever it does, then when I said okay it said
"Error -2147221005 occurred when finding CLSID of scripting language "Python":
Invalid class string"
"Error -2147221005 occurred when finding CLSID of scripting language "Python":
Invalid class string"
Unknown2005-02-27 17:50:15
You are missing the pywin32 library, which is required for Mushclient to see Python. There's a link to it from Python's download page.
Shryke2005-02-27 18:10:57
may I ask what the purpose of it is, I was using VIM for windows to script it... does it translate it or something, and by the way thanks for helping me...
Unknown2005-02-27 18:21:08
Mushclient supports most of its scripting languages through something called Windows Scripting Host (WSH). WSH is based on COM, which is Microsoft's own inter-process communication technology. pywin32 provides general compatibility with Windows for Python, and among other things - support for WSH. pywin32 also comes with its own Python IDE, that has some advanced features, like method and property calltips (show you everything you can call on an object in your script as you type), syntax checking, debugging, etc.
Unknown2005-02-27 19:55:37
I installed ActivePython, which uses WSH. It was an easy installation, and you can find it at http://www.activestate.com. (Of course, now I'm using Lua which is built right in and takes no installations. Works better if you want to use it in Wine on Linux, too.)
Unknown2005-02-27 20:15:40
ActiveState lags on updates, and you get both Python and pywin32 with it, which means that you'll lag on both. 
