[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Jscript
. . -> [Subject]  Triggers....in scripts

Triggers....in scripts

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Souvik   (6 posts)  [Biography] bio
Date Wed 23 Jun 2010 04:44 AM (UTC)
Message
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?
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Wed 23 Jun 2010 05:03 AM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 23 Jun 2010 05:16 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Souvik   (6 posts)  [Biography] bio
Date Reply #3 on Wed 23 Jun 2010 03:45 PM (UTC)
Message
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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #4 on Wed 23 Jun 2010 04:43 PM (UTC)
Message
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);

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Souvik   (6 posts)  [Biography] bio
Date Reply #5 on Thu 24 Jun 2010 04:08 AM (UTC)
Message
Soludra,
thanks, that takes care of my problem.

Nick,
thanks too.
[Go to top] top

Posted by Souvik   (6 posts)  [Biography] bio
Date Reply #6 on Thu 24 Jun 2010 04:35 PM (UTC)
Message
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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Thu 24 Jun 2010 05:49 PM (UTC)

Amended on Thu 24 Jun 2010 05:50 PM (UTC) by Twisol

Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Souvik   (6 posts)  [Biography] bio
Date Reply #8 on Fri 25 Jun 2010 05:51 AM (UTC)
Message
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
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #9 on Fri 25 Jun 2010 06:36 AM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Fri 25 Jun 2010 09:44 AM (UTC)
Message
Twisol is completely correct. :-)

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

http://www.gammon.com.au/scripting

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Souvik   (6 posts)  [Biography] bio
Date Reply #11 on Sat 26 Jun 2010 09:59 AM (UTC)
Message
Thanks for the help, to both!

Nick,
thanks, I'll read it immediately!
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


32,497 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]