Mushclient w/ Python scripting

by Xarcon

Back to Mechanic's Corner.

Xarcon2008-10-22 03:53:58
Okay... I need help understanding a couple of key points in Mush when it comes to Python..

First off, I haven't programmed anything seriously since I started learning Java like 10 years ago. Heck, and before that I was programming in Pascal... Amazing, how things have changed.
I started reading a couple of beginner's books on Python and decided to incorporate it into Mush. I have a basic understanding of Mush ever since I moved to it from Zmud like 3 or so years ago. (surface understanding)

I took a look at the code Nezha wrote for Mush and decided to take a simple algorithm from it and incorporated it into Python to help teach me a bit on how all this works (Much props to Nezha for sharing that with us! Thank you!)

Here is what I'm looking to do:
1) Input a number of targets - Sets up the beginning of a list of targets either one at a time or a group of names
2) Be able to cycle through the list prompting the current target selected.
Later on I would like to get more specific and be able to delete certain targets or load up a pre-made target list for certain groups of people.

At first I didn't understand the function parameters that were in the code (i.e. thename, theoutput, wildcard). Then I realized that thename refers to the Label name, theoutput refers to the alias trigger, and wildcard refers to *

My first question for this post would be: Where do I setup the targetlist List? I know if I put the list in the function it remains there locally. Should it be a global variable within Mush? And where do I setup the constants within the scripting file? I couldn't declare it in the function because every time I call it, it would reset back to the beginning.

Thank you for any help or advice.
Unknown2008-10-22 11:41:51
You can use the global keyword to tell Python that you're assigning a value to a persistent variable outside the function scope.

http://www.gammon.com.au/forum/bbshowpost....subject_id=2660
Trakis2008-10-23 12:47:11
I'm assuming you're doing this via a plugin. For targets, I think the built-in dictionary data type would probably do the job rather nicely.

In the script section of your plugin, outside of your functions, declare your constants as global, as Zarquan has said. After that, you should be able to refer to those variables from within your functions as well.
Xarcon2008-11-02 13:22:06
Thank you for the advice... Still trying to work out the code. I have what I want to do know on paper; it's just a matter of figuring out how Python interacts with MUSH.

I have another question. Scripting in Python, how do you do keybindings? For example, CTRL+# does an action in the alias. Would it be possible to call functions?

I looked through the forums and saw that someone mentioned using Accelerator function but is it available with Python or just Lua?

I'll try it out but any more advice would be great. Thanks!
Unknown2008-11-03 00:24:27
QUOTE(Xarcon @ Nov 2 2008, 08:22 AM) 578424
I looked through the forums and saw that someone mentioned using Accelerator function but is it available with Python or just Lua?


All those functions are available through any scripting language. The help files have the syntax listed for each, if you look.
Xarcon2008-11-03 03:17:58
QUOTE(Zarquan @ Nov 3 2008, 09:24 AM) 578503
All those functions are available through any scripting language. The help files have the syntax listed for each, if you look.


I took a look at the documentation page on the website and they had VBscript, Jscript, Perlscript and Lua examples posted so I thought maybe it wasn't supported with Python. From my experience, the syntax would be world.Accelerator("keybinding", "what it does"), right?

I think my problem would be establishing it as an alias... For example, would I name the alias CTRLK and then in script file write world.Accelerator("Ctrl+K", "kick rat"). Or just having it in the script file would allow me to access it?
Would it be possible for it to call a function instead of just a command?

Thanks again for the help
Unknown2008-11-03 03:24:54
QUOTE(Xarcon @ Nov 2 2008, 10:17 PM) 578527
I took a look at the documentation page on the website and they had VBscript, Jscript, Perlscript and Lua examples posted so I thought maybe it wasn't supported with Python. From my experience, the syntax would be world.Accelerator("keybinding", "what it does"), right?

I think my problem would be establishing it as an alias... For example, would I name the alias CTRLK and then in script file write world.Accelerator("Ctrl+K", "kick rat"). Or just having it in the script file would allow me to access it?
Would it be possible for it to call a function instead of just a command?

Thanks again for the help


You don't need a CTRLK alias. Set your macro with the Accelerator function, like what you described, and you'll be able to use the key combo after that.
Xarcon2008-11-03 04:19:34
QUOTE(Zarquan @ Nov 3 2008, 12:24 PM) 578530
You don't need a CTRLK alias. Set your macro with the Accelerator function, like what you described, and you'll be able to use the key combo after that.


Great! It worked. smile.gif Would it be possible to call functions within the script file using keybindings? I know the documentation mentioned something about the DoCommand 'preference'. Or would world.AcceleratorTo("keybind", "function name", 12) work?

I'll play around with it later tonight. But at least now I can set some keybindings to do some manual healing until I can learn enough to build a system.

Thanks again!
Unknown2008-11-03 12:32:15
AcceleratorTo can create macros that call script functions directly. It's a fairly recent addition, and before its arrival we had to make an alias that called the script and bind the macro to the alias.
Xarcon2008-11-03 13:32:49
QUOTE(Zarquan @ Nov 3 2008, 09:32 PM) 578574
AcceleratorTo can create macros that call script functions directly. It's a fairly recent addition, and before its arrival we had to make an alias that called the script and bind the macro to the alias.


I just saw it on the documentation page... implemented with 4.27

After a bit of trial and error, I finally realized how it works...

world.Accelerator("keybind", "functionname()", 12)

I was leaving out the parenthesis... When you place the function name in the Script box in Mushclient, you don't include the parenthesis but in the actual script file you do...

This is a good example of a noob programmer mistake imstupid.gif

Welp, I'll probably have plenty of questions later. But for now, I'll try to work out what I want to do with the info I have now...