How to call a variable inside this function.

Posted by CrazyPaladin on Thu 19 Mar 2015 05:50 PM — 8 posts, 30,256 views.

#0
Hello! This is my modified version of Nick's chat script. Just needing to know how I can call my "port" variable in the script portion, so that I'll get...

(portvariable)<string>

...over in my chats window.

Thank you for your help!


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Saturday, June 30, 2007, 10:48  -->
<!-- MuClient version 4.13 -->

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

<!--
Edit plugin and change "chat_world" variable to be the name of the 
world you want chats to go to.
-->

<muclient>
<plugin
   name="Chat_Redirector"
   author="Nick Gammon"
   id="cb84a526b476f69f403517da"
   language="Lua"
   purpose="Redirects chat messages to another world"
   date_written="2007-06-30 10:45:35"
   requires="4.08"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Redirects chats to the specified world.

Add or modify "chat" triggers to capture different sorts of message.

Change the variable "chat_world" to be the name of the world chats are to go to.
]]>
</description>

</plugin>

<!--  Triggers  -->

<triggers>

  <trigger
   enabled="y"
   match="(tell|tells) the group '.+'$"
   omit_from_output="n"
   regexp="y"
   script="redirect"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="(^|>)([A-Za-z]+|A cloaked figure|a cloaked figure) (tell|tells|reply|replies|mutter|mutters|answer|answers|question|questions|yell|yells|whine|whines|gossip|gossips|auction|auctions).+\'.+\'"
   omit_from_output="n"
   regexp="y"
   script="redirect"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="(^|>)\(ck\) ([A-Za-z]+|A cloaked figure|a cloaked figure) (tell|tells|reply|replies|mutter|mutters|answer|answers|question|questions|yell|yells|whine|whines|gossip|gossips|auction|auctions).+\'.+\'"
   omit_from_output="n"
   regexp="y"
   script="redirect"
   sequence="100"
  >
  </trigger>

</triggers>

<!--  Aliases  -->

<aliases>

  <alias
   match="port *"
   enabled="y"
   variable="port"
   send_to="9"
   sequence="100"
  >
  <send>%1</send>
  </alias>

</aliases>


<!--  Script  -->


<script>
<![CDATA[
chat_world = "RoD chats"
local first_time = true

function redirect (name, line, wildcards, styles)

  -- try to find "chat" world
  local w = GetWorld (chat_world)  -- get "chat" world

  -- if not found, try to open it
  if first_time and not w then
    local filename = GetInfo (67) .. chat_world .. ".mcl"
    Open (filename)
    w = GetWorld (chat_world)  -- try again
    if not w then
      ColourNote ("white", "red", "Can't open chat world file: " .. filename)
      first_time = false  -- don't repeatedly show failure message
    end -- can't find world 
  end -- can't find world first time around

  if w then  -- if present
    for _, v in ipairs (styles) do
      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    v.text)  
    end -- for each style run
    w:Note ("")  -- wrap up line

  end -- world found

end -- function redirect 

]]>
</script>


</muclient>
Amended on Thu 19 Mar 2015 05:55 PM by CrazyPaladin
Australia Forum Administrator #1
Template:function=GetVariable
GetVariable

The documentation for the GetVariable script function is available online. It is also in the MUSHclient help file.

#2
So...


if w then  -- if present
    local port
    port = GetVariable("port")
      if port == nil then
        Note ("port is not defined")
      end
    for _, v in ipairs (styles) do
      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    v.text)
      end -- for each style run
    w:Note ("")  -- wrap up line

  end -- world found


But how do I go about getting it to add to the beginning of the "v.text". I do not know lua, and have only thus far made more triggers for the Triggers section. I don't even begin to understand this part, so just trying to muddle through. I'm sure its easy to you, but I am lost.
Australia Forum Administrator #3
You concatenate with "..", so it might look like this:


      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    port .. v.text)
#4
Its getting there, but it now puts the "port" variable at the beginning of each color change.


  if w then  -- if present
    local port
    port = GetVariable("port")
      if port == nil then
        Note ("port is not defined")
      end
    for _, v in ipairs (styles) do
      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    port .. v.text)
      end -- for each style run
    w:Note ("")  -- wrap up line

  end -- world found


Gives this, after making the "port" variable "Live". Left side is output from the MUD, right side is the RoD Chats window the script is dumping too.

http://imgur.com/uu5EWWn
Australia Forum Administrator #5
Oh yeah, well just move things around:



  if w then  -- if present
    local port
    port = GetVariable("port")
      if port == nil then
        Note ("port is not defined")
      end
    Tell (port)
    for _, v in ipairs (styles) do
      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    v.text)
      end -- for each style run
    w:Note ("")  -- wrap up line

  end -- world found

#6
Almost, but managed to figure out the last bit. "Tell (port)" puts the port in the MUD window, so I did "w:Tell (port)" and it WORKED!!! Thank you so very much, Nick! Much appreciated!


  if w then  -- if present
    local port
    port = GetVariable("port")
      if port == nil then
        Note ("port is not defined")
      end
    w:Tell (port)
    for _, v in ipairs (styles) do
      w:ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    v.text)
      end -- for each style run
    w:Note ("")  -- wrap up line

  end -- world found
Australia Forum Administrator #7
That sounds right, glad it's working.