Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Plugins ➜ Accessing Lua variables from plugin triggers

Accessing Lua variables from plugin triggers

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


Posted by Twisol   USA  (2,257 posts)  Bio
Date Mon 23 Mar 2009 06:00 AM (UTC)

Amended on Mon 23 Mar 2009 06:05 AM (UTC) by Twisol

Message
I'm getting mixed signals on how Lua variables within triggers work... In my plugin, I define a table in the code section. I also have a trigger that checks the size of this table (it's a vector). And then I have another trigger that calls a function to add another element to the table.

This all worked fine until I got rid of the function and moved it into the trigger body itself. For some strange reason, it's acting like the other table never even existed. I didn't change the code at all except for moving it from the function to the trigger body. And what makes it more disconcerting than usual is that the first trigger, the one that checks how many elements are in the table, works fine, and I haven't changed it at all.


  <!-- Parses PLANTS for the list of herbs present -->
  <trigger enabled="n" group="harvest_scan2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^[A-Za-z' ]+ \(([a-z' ]+)\)\s+\w+$" >
    <send>
      -- This is the line that -was- in a function.
      table.insert(toharvest, (herbs[herb] or herb))
    </send>
  </trigger>

  <!-- Ends the parsing of PLANTS and acts on the list of plants -->
  <trigger enabled="n" group="harvest_scan2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^(?:\d+h, )?(?:\d+m,? )?(?:\d+e,? )?(?:\d+w,? )?c?e?x?k?d?b?@? ?(?:Vote)?- ?$" >
    <send>
      EnableGroup("harvest_scan2", false)
      if (#toharvest > 0) then
        EnableGroup("harvest_act", true)
        DoHarvest()
      else
        Note("There aren't any plants to harvest!")
        running = false
        Send()
      end
    </send>
  </trigger>


Any idea what's going on? I've been trying to clean up the plugin a bit, and I don't like straggling functions that are only used in one place...

MUSHclient version 4.35, by the way.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 23 Mar 2009 07:53 PM (UTC)
Message
If you move something to a trigger like that, the only real issue is the order in which things happen. Triggers and the script file share the same script space. If in a plugin, the space is shared between the plugin triggers and the plugin script part.


Now in the code you posted, the toharvest table is not actually created, so it not being found when the trigger fires is no huge surprise.

One approach is to have a defensive creation line like this:


toharvest = toharvest or {}  -- create table if necessary

table.insert(toharvest, (herbs[herb] or herb))


However if you believe the table is in fact created, you need to show how and where that is done. Of course, the creation needs to be in the plugin, not in the main world script file, as they have separate script spaces.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #2 on Mon 23 Mar 2009 08:49 PM (UTC)

Amended on Tue 24 Mar 2009 05:33 AM (UTC) by Twisol

Message
Right, the toharvest table is created in the plugin script space, along with a few other things I use in the script:


toharvest = {} -- Plants in the room to harvest
harvcount = {} -- Count of harvested plants

running = false


I'm positive the table is created first thing, because after all, it's in the main script (and not in a function or some other lexical scope). Neither of the two functions (EDIT: triggers, rather) create a table, either. And like I said, the one that just checks #toharvest always worked.

In short: script file executed first, should have variables loaded/defined already. Triggers execute "later", should have access to said variables. One trigger does, one tigger doesn't. Decidedly odd.

EDIT: typofix: functions -> triggers

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #3 on Tue 24 Mar 2009 06:00 PM (UTC)
Message
Hahaha, oh wow, I am an idiot. 'herb' is a parameter to the function I had before, but since I moved that code out of the function, 'herb' was nil. Replacing herb with "%1" fixed it.

Sorry! >_<

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
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.


10,819 views.

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

Go to topic:           Search the forum


[Go to top] top

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