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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Learning to script through MUSHClient

Learning to script through MUSHClient

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


Posted by Soupy   (6 posts)  [Biography] bio
Date Sun 17 Sep 2017 11:38 AM (UTC)
Message
Hello,

I think this is a pretty basic question but it has me stuck. I'm experimenting with various triggers and scripts but not really anything productive yet. Right now, I am looking at:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=8411


I have it functioning as described by the tutorial and that is great. Now, I am trying to figure out how to get the script out of the trigger contents and into my world.lua file. As a start, I'm just trying to move the initial trigger portion over to a file:

Quote:
killed_mobs = killed_mobs or {} -- make mobs table

mob_name = "%1" -- this mob's name (first wildcard)

-- add this mob if first time

killed_mobs [mob_name] = killed_mobs [mob_name] or { count = 0 }

-- add 1 to count of mobs
killed_mobs [mob_name].count = killed_mobs [mob_name].count + 1

-- remember when we last killed it
killed_mobs [mob_name].last_time = os.time ()



When I pull it out of the trigger contents and copy and paste it into a world.lua file, it fails to compile with the following message:

Quote:
Error number: 0
Event: Run-time error
Description: [string "Script file"]:5: attempt to index field '?' (a nil value)

stack traceback:

[string "Script file"]:5: in main chunk
Called by: Immediate execution



I think that this is referring to the first time [mob_name] is referenced and it would make sense that it is nil when I initially load the script. I just have no idea how to fix it. Any help would be appreciated, even if you could just point me at a post where someone works through this challenge.


Thanks,
Soupy
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 18 Sep 2017 05:08 AM (UTC)

Amended on Mon 18 Sep 2017 05:09 AM (UTC) by Nick Gammon

Message
When using a script file things are done slightly differently. In particular the wildcards are passed as a function argument. The typical trigger function declaration is:


function myTriggerHandler (name, line, wildcards, styles)

-- your code here

end -- myTriggerHandler


Thus in your case it would be:


function myTriggerHandler (name, line, wildcards, styles)

  killed_mobs = killed_mobs or {} -- make mobs table

  mob_name = wildcards [1] -- this mob's name (first wildcard)

  -- add this mob if first time

  killed_mobs [mob_name] = killed_mobs [mob_name] or { count = 0 }

  -- add 1 to count of mobs
  killed_mobs [mob_name].count = killed_mobs [mob_name].count + 1

  -- remember when we last killed it
  killed_mobs [mob_name].last_time = os.time ()

end -- myTriggerHandler


Now the wildcards are passed down as a table (3rd argument) which you can index into like normal Lua tables.

- Nick Gammon

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

Posted by Soupy   (6 posts)  [Biography] bio
Date Reply #2 on Sat 23 Sep 2017 05:25 PM (UTC)
Message
Thanks!

This has solved the errors that happened upon loading the script. Now, it appears that if I keep the trigger content fields blank that the trigger matches--I have it set to color the triggering message yellow and underline it--but nothing happens.

The UI prompts me to send '%0' but when I do that I get the following error...

Quote:
Error number: 0
Event: Compile error
Description: [string "Trigger: "]:1: unfinished string near '<eof>'
Called by: Immediate execution



I feel like I'm pretty close, I'm just missing the piece that bridges from the trigger as configured in the UI with the function in my script file.

Is it possible to just write out the trigger in the script file itself and have it call the function there? Or is there some other best practice that makes this easy?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Sun 24 Sep 2017 07:44 PM (UTC)
Message
Quote:
The UI prompts me

Which UI?

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Soupy   (6 posts)  [Biography] bio
Date Reply #4 on Mon 25 Sep 2017 12:20 AM (UTC)
Message
The Mushclient UI in the trigger setup. Specifically when I click 'Ok' to save the trigger settings with nothing in the 'Send' field.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #5 on Tue 26 Sep 2017 12:31 AM (UTC)
Message
I think I need to see what it looks like.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Tue 26 Sep 2017 06:02 AM (UTC)
Message
If you do "send to script" then the UI wants you to send something, and thus having a blank send box is not OK.

If you change it to "send to world" that message will go away.

If that doesn't solve it, show the trigger:

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.

- Nick Gammon

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

Posted by Soupy   (6 posts)  [Biography] bio
Date Reply #7 on Sun 15 Oct 2017 01:26 PM (UTC)
Message
This is the trigger that I am attempting to use for the script... http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=8411


Where the triggering message is 'You killed *!'

Since I have moved MyTriggerHandler into the script file I don't know what to put in the text file at all.

Is there a file that explains how I pass in the arguments in the body of a trigger?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #8 on Sun 15 Oct 2017 02:55 PM (UTC)

Amended on Sun 15 Oct 2017 02:56 PM (UTC) by Fiendish

Message
Quote:
Is there a file that explains how I pass in the arguments in the body of a trigger?


https://www.gammon.com.au/scripts/doc.php?general=triggers has a section called "Trigger scripts" that may be helpful.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Soupy   (6 posts)  [Biography] bio
Date Reply #9 on Sun 15 Oct 2017 03:58 PM (UTC)
Message
Thank you, that is where I have been looking so it is good to know I am on the right track. I think I need some help actually connecting the dots here though. If my trigger is...


Quote:
<triggers>
<trigger
custom_colour="2"
enabled="y"
match="You killed *!"
sequence="100"
lowercase_wildcard="y"
>
<send></send>
</trigger>
</triggers>



And my handler, kept in the script file, is..

Quote:
function myTriggerHandler (name, line, wildcards, styles)

killed_mobs = killed_mobs or {} -- make mobs table

mob_name = wildcards [1] -- this mob's name (first wildcard)

-- add this mob if first time

killed_mobs [mob_name] = killed_mobs [mob_name] or { count = 0 }

-- add 1 to count of mobs
killed_mobs [mob_name].count = killed_mobs [mob_name].count + 1

-- remember when we last killed it
killed_mobs [mob_name].last_time = os.time ()

end -- myTriggerHandler



What do I need to put in the <send> </send> field to have that particular trigger invoke myTriggerHandler with the appropriate arguments? Is that even how it works?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #10 on Sun 15 Oct 2017 06:37 PM (UTC)

Amended on Sun 15 Oct 2017 06:39 PM (UTC) by Fiendish

Message

<trigger
custom_colour="2"
enabled="y"
match="You killed *!"
sequence="100"
lowercase_wildcard="y"
script="myTriggerHandler"
send_to="12"
></trigger>

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #11 on Sun 15 Oct 2017 08:29 PM (UTC)

Amended on Sun 15 Oct 2017 08:33 PM (UTC) by Nick Gammon

Message
See this screenshot:




  • What you want to match on goes in the Trigger box
  • Send to World (the default)
  • If there is a function name in the Script box then that function is automatically called with the default arguments (name, line, wildcards, styles). This function needs to be in the world script file.


You don't need to do anything else to get your script function called.

There is a post explaining what happens when a line of text arrives from the MUD:

http://www.gammon.com.au/forum/?id=6554

If you put stuff in the "send" box then that is sent to the MUD, in addition to the script being called. You can also colour output, log it, etc. The post above explains the exact order in which these things happen.

- Nick Gammon

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

Posted by Soupy   (6 posts)  [Biography] bio
Date Reply #12 on Mon 16 Oct 2017 11:36 AM (UTC)
Message
IT WORKS! Thank you!
[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.


28,619 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]