Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Kalie
Beginner


Joined: 23 Jun 2008
Posts: 22

PostPosted: Mon Jun 23, 2008 6:25 pm   

Passing trigger varible %1 to python scripts
 
I am trying to figure out how to pass regex trigger variables to python scripts. In zScript you can just use %1-%99 but those are special characters in python so the script bombs out. My trigger looks like this:

^[A-Z].*(@{bad_guys}).*\.$

It looks in each line that starts with a capital letter and ends with a period for one of the bad guys in my list. The trigger works and I get %1 equal to a bad guy if it's in the list. But I can't use %1 in a python script (or rather I haven't figured out how to do it). Any ideas? I know I could probably rewrite the trigger so it isn't a regex trigger and then I could do {@var1 %1} at the end to define var1 as the temp variable. But this problem will probably crop up again in another context so I'd like to figure out how to do it. Thanks guys.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Jun 23, 2008 6:55 pm   
 
You can't yet. The only part of CMUD that's exposed to WSH languages is the zvar object, which lets you access and change the values of zScript variables. Beyond that, you're stuck.

So it's possible with a complicated system of levers and pulleys that looks something like:

#var Temp_1 %1
#function SomeFunctionWithAPythonScript()

and then use zvar.temp_1 or whatever the python syntax is to access the value of @Temp_1. You could use an expression trigger to run commands:

#trig {@PythonSaysDoSomething} {#if (@PythonSaysDoSomething) {#exec @PythonSaysDoSomething;#var PythonSaysDoSomething ""}} "" {exp}

But it's a lot of hassle.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Kalie
Beginner


Joined: 23 Jun 2008
Posts: 22

PostPosted: Mon Jun 23, 2008 7:12 pm   
 
Thanks! I'll look into the levers and pulleys solution. Very Happy
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Mon Jun 23, 2008 7:36 pm   
 
I haven't tried but I wonder if if you couldn't enclose the %1 etc in a quotes to have python receive it as a string

someVar = "%1"
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Jun 23, 2008 7:47 pm   
 
The zScript compiler never even looks at a script that's marked Python, so it won't ever see that kind of thing. If you were using the %mss function it might be possible, but I'm not very well-versed in that function.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Kalie
Beginner


Joined: 23 Jun 2008
Posts: 22

PostPosted: Tue Jun 24, 2008 12:00 am   
 
I'm embarrassed to confess that I can't figure out how to get this working. The part I'm stuck on is getting a function to compile with a python script. If I were to make a function in python is would look like this:

def pythonfunc(var):
result = var + 1 #or whatever - just some process
return result

In the Cmud editor I'm not sure what to put in. The name must be what the function is called (e.g. pythonfunc) but do I put '()' after it? Do I put the default variables in it? Then do I need to define the function like above or is that assumed to be done and I just launch into the body of the function? I'm extremely new at this and I'm pretty excited to be able to use python with cmud. I just haven't gotten it to go yet.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Jun 24, 2008 1:51 pm   
 
You need to think of zScript and python as totally different things. zScript objects are able to hold python code (if you have Python selected from the script drop-down) but CMUD doesn't run that code itself. Anything you do in python only matters to Python, except where you use the special zvar interface to make changes.

So Python has its own namespace, and anything you create (like functions) should hang around in the namespace as long as CMUD is running. When exactly things are deleted will be up to whatever you installed to put Python into the WSH (presumably ActivePython), but it'll probably be when CMUD closes.

You can define functions in python like that, from a python script inside an alias or something, and they'll keep existing when you call anything else that uses Python. I'm assuming that ActivePython works the same as other WSH languages normally do, which it might not.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Zenethian
Beginner


Joined: 12 Jan 2009
Posts: 10

PostPosted: Sun Jan 18, 2009 8:02 am   
 
Reading this, it seems that having any language other than zScript is entirely useless. Why support Python if you can't do anything with it?

To be honest, I'd much rather program all of my stuff in Python, so having that ability is a great boon for me! Has any progress been made on this? I can't find anything that even hints about how to program in CMUD/ZMUD with any of the languages other than zScript.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Sun Jan 18, 2009 9:39 am   
 
It is pretty useless - the rudimentary support was put in to see how well it'd work, with the intention of expanding the interface later on. That's still on the todo list, but other things have taken priority in the interim. The Lua interface has been completed, but exposing the same interface to the WSH languages hasn't been finished.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 3926
Location: Pensacola, FL, USA

PostPosted: Sun Jan 18, 2009 10:42 am   
 
I have yet to find something i couldn't find some way of managing with zscript.
_________________
Discord: Shalimar#3679
Reply with quote
Zenethian
Beginner


Joined: 12 Jan 2009
Posts: 10

PostPosted: Sun Jan 18, 2009 10:45 am   
 
Well, that's not really the point. zScript is a fine and capable language, but, I'm a die-hard snake-kissin Python-lovin' programmer. To have Python as a fully enabled way to script CMud would make me one happy little camper.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Tue Jan 20, 2009 6:14 pm   
 
Keep in mind that the Windows Scripting host is very limited in what it can support for these other languages. Microsoft does a better job with the built-in VBScript and JScript support. In addition to the zvar object that Fang mentioned, you can also call CMUD via COM functions. See the COM in zMUD and zMUD COM Server articles and change references from zMUD to CMUD. The %session and %zmud (%cmud) COM objects are predefined in all Microsoft Scripting languages. For example, in VBScript you can do this:

msgbox NumVars

and it will properly display the number of variables. Or doing:

msgbox VarNum(0).Value

will display the value of the first variable. Or

msgbox GetVar("a","").Value

will display the value of the variable named "a".

So, as long as your scripting language can access COM object references, then you can still access a large part of CMUD from them. But these Microsoft Scripting Languages are never going to be as integrated as zScript, or even Lua (which doesn't use MSS). Using these MSS languages is also always going to be much slower than using zScript or Lua, again because of how Microsoft implements the scripting system.

Note that the Microsoft Scripting system even isn't officially supported anymore in future versions of Windows, even though Microsoft will probably still allow it to work...they just are not working on making it any better or easier...we are stuck with what we have from them.

Even though the COM interface isn't as nicely documented as the Lua interface and is missing some of the interface to stuff like %1, it isn't like it's not there. You can still do a lot with it.

But honestly, you will be much better in the end if you just learn zScript. It isn't that hard, and it's probably easier than learning all of the COM object interface from Python to CMUD.
Reply with quote
Zenethian
Beginner


Joined: 12 Jan 2009
Posts: 10

PostPosted: Wed Jan 21, 2009 5:13 am   
 
Ahh, thank you for the COM reference.

Though, in the end, I may indeed just focus on learning zScript.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net