Accessing Lua variables from plugin triggers

Posted by Twisol on Mon 23 Mar 2009 06:00 AM — 4 posts, 12,025 views.

USA #0
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.
Amended on Mon 23 Mar 2009 06:05 AM by Twisol
Australia Forum Administrator #1
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.

USA #2
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
Amended on Tue 24 Mar 2009 05:33 AM by Twisol
USA #3
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! >_<