Trying to script a fairly simple trigger in python

Posted by LupusFatalis on Mon 22 May 2006 04:39 AM — 8 posts, 34,448 views.

#0
... Well, I'm new to python, and what I'm trying to do is make a trigger on:

* You think your (some symbols) skill has improved. *

so in the trigger section I put:
^\* You think your * skill has improved\. \*$

and for send:
%0

Now, I wrote a little script in Python for it, or tried to...

def OnSkillGain(TriggerName, trig_line, wildcards):


VariableName += ''.join(['x' for x in wildcards])

VariableName = VariableName.replace(' ', '_').replace('\'', '').replace('-', '_').replace('#', '_')
VariableValue = world.GetVariable(VarName)

if not VariableValue:
world.SetVariable(VarName, 1)
else:
world.SetVariable(VarName, VariableValue+1)

Basically, what I'm trying to do, is read in the name of the skill, which in some cases can contain symbols I want to replace with other symbols... I get the following error.

Error number: -2147221005
Event: finding CLSID of scripting language "Python"
Description: Error -2147221005 occurred when finding CLSID of scripting language "Python":

Invalid class string

Called by:

Anyway, I'd appreciate any help. Thanks.
Australia Forum Administrator #1
See:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=3035
#2
Thank you very much. Everything is now working well after some tweaking, incase anyone wanted the trigger and corresponding script, here it is.

Trigger:
<triggers>
<trigger
enabled="y"
lines_to_match="2"
match="^\* You think your (.*) skill has improved\. \*$"
regexp="y"
script="OnSkillGain"
sequence="100"
>
</trigger>
</triggers>

Script, written in Python:

def OnSkillGain(TriggerName, trig_line, wildcards):

VariableName = ''.join([wildcards[x] for x in range(len(wildcards)-1)])

VariableName = VariableName.replace(' ', '_').replace('\'', '').replace('-', '_').replace('#', '_')

if not world.GetVariable(VariableName):
VariableValue = 1
else:
VariableValue = int(world.GetVariable(VariableName)) + 1

world.SetVariable(VariableName, str(VariableValue))
world.note(VariableName + ': ' + str(VariableValue))

#3
Alright... Now, I finished the script that does all the skill handling on the mud. I want to convert it now to one coherant plugin. When I do this, the new variables that the scripts were creating as world variables are no longer world variables. I would like them to still be world variables. Or at the very least they don't show up as world variables. In fact, I wanted to see where they showed up, they aren't in the xml file either. Yet when I close the client and open it their values are stored. I don't know where. Now, if I can't keep them as world variables, thats fine, I could easily write a script to print them to the client when I want them. But I really would like to know where they are stored. I used the plugin wizard to create the plugin, it consists of two triggers and some scripts. I'll add to it later, but for now I just wanted to see how creating a plugin worked. As I am both new to Mushclient and Python... Though not new to programming. In any case, Thanks for your assistance.
Australia Forum Administrator #4
Variables are private to each plugin, in case two plugin authors happen to use the same variable name (like "counter").

They are saved to a plugin "state" file which is in the State subdirectory, and is named by concatenating the world ID and the plugin ID.

For example, one of mine is named:


b55ac52c9b44ccc7f4f41562-982581e59ab42844527eec80-state.xml

World ID is: b55ac52c9b44ccc7f4f41562
Plugin ID is: 982581e59ab42844527eec80


The reason for using the World ID as well is to stop clashes with state files if you use the same plugin in multiple worlds.

You can easily view the current state file (which will be when it was last saved) by RH-double-clicking the name of the plugin in the plugins list in MUSHclient.

If you want to see the current plugin variables do this:


/world.Debug "plugins"


Alternatively write an alias that on demand will list the plugin variables and their contents.
Amended on Mon 22 May 2006 09:07 PM by Nick Gammon
#5
... Alright, I'm down to the very last stage, got everything else working, everything works nicely as scripts triggers, and aliases. But when I tried to use the wizard and add them all, I get the following error...


Error number: -2147352567
Event:        Execution of line 147 column 40
Description:  unindent does not match any outer indentation level
Line in error: 
   if len(v) > 6 and v[:6] == 'skill_':
Called by:    Immediate execution

Line  102: Error parsing script (problem in this file)


...I haven't a clue why this is executing immediately, if that is, in fact, what is going on. The piece of my script it is looking at is right here... Which is something I modified from one of your examples:


def DisplayVariables(TriggerName, trig_line, wildcards):

	world.notecolour = 16
	variablelist = world.GetVariableList
	if (variablelist ):
  		for v in variablelist:
			if len(v) > 6 and v[:6] == 'skill_':
				world.tell(v[6:])
			else:
				world.tell(v)
			world.Note (' = ' + world.GetVariable(v))
	world.notecolour = 4


Thank you.
Amended on Tue 23 May 2006 04:42 AM by Nick Gammon
Australia Forum Administrator #6
I edited your post to add the code tag so we could see the indentation.

My Python skills are not very good, perhaps someone else can help here?
#7
Alright, thanks very much for your help, I got everything working fine as a plugin. You did a very nice job with this client. I did want to note one thing though. When I try to import everything from a script file, I get an error that the script tag is not in use or some such. And I can't figure out how to make it go away, even after I delete all the imported things. Now, this isn't much of a problem as I was just importing everything to make a modification to the plugin, then make it a plugin again. And as those were the only things I had in the world so far, I just made a new world. But I just thought it would be something you'd might want to change, or add support for importing files from plugins, etc... Anyway, thanks again. Now to contemplate what project I'm going to tackle next, heh!