Triggers....in scripts

Posted by Souvik on Wed 23 Jun 2010 04:44 AM — 12 posts, 50,982 views.

#0
I've only just started scripting....so I'm a complete newbie.

Here's my problem:

I have a trigger that looks like this ->" * says, "*" ". I want to extract the value of the first * and store it in a variable 'NAME'. All this will be done through JScript.

How do I do it?
USA #1
If this is being done through the trigger dialog, and not through a script callback, then you can use %1 in the script and it will be replaced inline with the matched name. You can do something like this quite easily:

world.SetVariable("NAME", "%1");


Make sure you set "Send to" to Script.
Australia Forum Administrator #2
To set a single variable you can also just send to "variable" and put in the variable name. Then just put %1 in the Send box.
#3
I can do the whole thing fine using the trigger dialog. My problem is that I can't do it through scripting.

I was trying to use the AddTriggerEx to send to script engine but something's not right....please correct me:

world.AddTriggerEx("trig1","* says, "*"","world.SetVariable("name","%1");",1,-1,0,"","",12,100);

The part ~~world.SetVariable("name","%1");~~ I think is bound to cause an error (and it did), but that is what I write in the trigger dialog when I send_to script through the dialog box in MUSH.

What I want to do is get this entire process done through a script and not through MUSHClient's dialog boxes.

Thank you.
USA #4
Souvik said:

I can do the whole thing fine using the trigger dialog. My problem is that I can't do it through scripting.

I was trying to use the AddTriggerEx to send to script engine but something's not right....please correct me:

world.AddTriggerEx("trig1","* says, "*"","world.SetVariable("name","%1");",1,-1,0,"","",12,100);

The part ~~world.SetVariable("name","%1");~~ I think is bound to cause an error (and it did), but that is what I write in the trigger dialog when I send_to script through the dialog box in MUSH.

What I want to do is get this entire process done through a script and not through MUSHClient's dialog boxes.

Thank you.


Oh, I see. The problem is this: the " character is used by JScript (and almost every common language these days) to delimit strings. So what happens if you (try) to put a " inside the string? It thinks the string is cut short, and it parses whatever's after that " thinking it's more code.

You have two options here (again). One, you can escape the quotes inside the string. This would look like:

world.AddTriggerEx("trig1","* says, \"*\"","world.SetVariable(\"name\",\"%1\");",1,-1,0,"","",12,100);


Two, you can use single quotes in opportune places. It's okay to put double quotes in single quotes, or single quotes in double quotes.

world.AddTriggerEx("trig1",'* says, "*"','world.SetVariable("name","%1");',1,-1,0,"","",12,100);
#5
Soludra,
thanks, that takes care of my problem.

Nick,
thanks too.
#6
How do I use the scripts to do some simple math functions with values and display the result on the screen?

Something like this:

world.AddTriggerEx("stat","*h, *m, *e, *w *","",1024,-1,0,"","statup",12,100);

function statup()
{
//math part here
}

I tried GetVariable() and SetVariable() inside 'statup()' and then using JScript variables (not MUSH variables) to do the math and then reconverting them to MUSH equivalents and displaying them.

Well, it didn't work. Can someone show me how to get this done?

Thank you.
USA #7
The fundamental issue is that MUSHclient variables are strings. Adding strings (in JScript at least, if I recall correctly) concatenates them, so "1" + "2" is "12".

You'll need a function that converts strings to numbers. parseInt() seems like what you want here.

function statup()
{
  var foo = parseInt(GetVariable("foo"));
  foo += 10;
  world.Note(foo + ""); // convert to string with + ""
  SetVariable("foo", foo + "");
}



Keep in mind, it's always more useful for us to see something you've tried, rather than a blank function. I'm really just making a educated guess at what the issue is here.
Amended on Thu 24 Jun 2010 05:50 PM by Twisol
#8
Sorry for the blank function, but it worked.

Here's another example:

world.AddTriggerEx("stat","*h, *m, *e, *w *","",1,-1,0,"","statup",12,100);

function statup()
{
world.SetVariable("hp","%1");
world.SetVariable("mp","%2");
}

//this one above doesn't work, instead of the '*'s, the %1 gets stored. Can you so me how to get this done? I mean, I know that we can use the 'response text' argument to send the commands, but isnt it possible to do the same using a function?

Thanks
USA #9
Ah, yes. Your callbacks can receive three optional parameters (four in Lua). The third happens to be the list of wildcards.

function statup(name, line, matches)
{
  world.SetVariable("hp", matches[1])
  world.SetVariable("mp", matches[2])
}


The reason %1 doesn't work is because the send-text for an alias or trigger is special. Basically, every time an alias or trigger is fired, it scans over the send text and replaces %1, @var, etc. with their actual values before actually sending/running it. MUSHclient can't do this with functions somewhere else, because they're already defined and ready by the time you give it its name. So it passes the parameters above (alias/trigger label, full line matched, and an array of matches) instead.
Australia Forum Administrator #10
Twisol is completely correct. :-)

Read this, it explains a lot of that sort of thing:

http://www.gammon.com.au/scripting
#11
Thanks for the help, to both!

Nick,
thanks, I'll read it immediately!