Global variable used within triggers.

Posted by Ductape on Wed 12 Aug 2009 07:15 AM — 3 posts, 13,979 views.

#0
Hi there, new to lua and I am trying to learn some things so i can build more advanced scripts in the future.

I am trying to use a variable in a trigger, and also in a script that send to the mud all from within a plugin.

Pastebin is here:
http://pastebin.com/m3a86d453

Line 27 I attempt to declare a global

Line 52 I attempt to use it in a trigger

The script section starting at line 98 works perfectly. What am I doing wrong for the Trigger section?

Heres the code for those who dont want to clicky pastebin:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Monday, July 27, 2009, 8:13 PM -->
<!-- MuClient version 4.40 -->

<!-- Plugin "repairgrinder" generated by Plugin Wizard -->

<muclient>
<plugin
   name="repairgrind2"
   author="Terensque"
   id="ce3de8b3aa85d6f6eba44dac"
   language="Lua"
   purpose="grind repair with tv"
   date_written="2009-07-27 20:12:59"
   requires="4.40"
   version="1.0"
   >

</plugin>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Declare a global for the triggers   -->

repairobject = "television"

<!--  Triggers  -->

<triggers>


  <trigger
   enabled="y"
   match="^It's completely destroyed.$|^Sybian curses in frustration and gives up.$|^You fixed some of the damage.$"
   regexp="y"
   name="needs_repair"
   script="repair"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="You fixed all of the damage."
   script="repair"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="^Sybian's (.*?) against (.*?) (repairobject).$|^Sybian's (.*?) puts a dent in (.*?) (repairobject).$|^Sybian's (.*?) into (.*?) (repairobject)(.*?)$|^Sybian's (.*?) scuffs (.*?) (repairobject)(.*?)$"
   regexp="y"
   name="check_repair"
   script="repair"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="^It's (.*?) damaged.$|^It's a little dinged up.$"
   regexp="y"
   name="needs_kill"
   script="repair"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="^[ You earned (.*?) IP in repair! ]$|^[ You spent (.*?) for (.*?) IP in repair! ]$"
   regexp="y"
   name="check_grind"
   send_to="10"
   sequence="100"
  >
  <send>
   score
  </send>
  </trigger>

  <trigger
   enabled="y"
   match="(.*?) you can for today(.*?)$"
   regexp="y"
   name="end_grind"
   script="repair"
   sequence="100"
  >
  </trigger>

</triggers> 
<script>

local repairobject
repairobject = "television"

function repair (sTrig)
  if sTrig == "needs_repair" then
    Send ("repair " .. repairobject)
  elseif sTrig == "needs_kill" then
    Send ("kill " .. repairobject)
  elseif sTrig == "check_repair" then
    Send ("examine " .. repairobject)
  elseif sTrig == "end_grind" then
    Print ("Repair grind complete")
  else
    Send ("kill " .. repairobject)
  end
end -- repair
</script>

</muclient>
Australia Forum Administrator #1
I assume this is the troubling line:


match="^Sybian's (.*?) against (.*?) (repairobject).$|^Sybian's (.*?) puts a dent in (.*?) (repairobject).$|^Sybian's (.*?) into (.*?) (repairobject)(.*?)$|^Sybian's (.*?) scuffs (.*?) (repairobject)(.*?)$"


First, I would be a little cautious about the brackets. I may be wrong, but I would parenthesize the various things you are checking, like this:


match="(^Sybian's (.*?) against (.*?) (repairobject).$)|(^Sybian's (.*?) puts a dent in (.*?) (repairobject).$)|(^Sybian's (.*?) into (.*?) (repairobject)(.*?)$)|(^Sybian's (.*?) scuffs (.*?) (repairobject)(.*?))$"


The much bigger problem here though, is you are confusing script variables with MUSHclient internal variables, which in any case you haven't asked it to use.

To simplify, I will look at a single branch of your match, eg.


match="^Sybian's (.*?) against (.*?) (repairobject).$"


This is going to match "repairobject" not what that variable expands to. You need to put a @ in front of it, like this:


match="^Sybian's (.*?) against (.*?) (@repairobject).$"


Then, you need to tell it to expand variables, by checking the "expand variables" checkbox.

If you had done that, I would have seen:


 expand_variables="y"


... in your trigger definition.

Next, a MUSHclient variable is not a Lua variable.
So, you might change:


local repairobject
repairobject = "television"


to:


local repairobject
repairobject = "television"
SetVariable ("repairobject", repairobject)


Now the MUSHclient variable "repairobject" is going to have "television" in it. Now the trigger will expand @repairobject to "television" and things will start to work.

#2
This worked perfect.