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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Lua Tables
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

Lua Tables

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

Posted by Wyd   (14 posts)  [Biography] bio
Date Wed 27 Sep 2006 07:04 AM (UTC)
Hi all.

I am trying to write a script with uses a queue system to control the comands that are sent to the mud.

I want to be able to enter a alias, whith a parameter, such that the alias puts the required commands into the queue, another alias then gets the the first command from the queue, and sends it to the mud.

Im guessing to do this, I need to be use Lua tables (as I am programming it in Lua). From reading around the site and other Lua sites, i have worked out how to get the commands to come out in the right order (in theory, cant get it to show though), and such, but I am having problems with two things:

1, putting a string into the table which contains variables
-- Such as "Tell @person go north"

2, getting the script to output the string it contains
-- Sending the above to the mud

Hope this makes sense


[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #1 on Wed 27 Sep 2006 08:24 AM (UTC)

Amended on Wed 27 Sep 2006 08:30 AM (UTC) by Ked

Lua can't interpret Mushclient variables, like Mushclient itself can. But what you want can still be accomplished, although with some hoops to jump.

You can make your queue hold tables, instead of strings. Each table should have a boolean field, called "interp" for example. So that a plain string that should be sent directly to the MUD without any processing has field "interp" set false, while a table with a string that needs to have variables expanded or contains a script has "interp" set true.

Further, when retrieving a table from a queue, you check the "interp" field and if it is true then you add a temporary alias, set its Send property to the table's string, set it to send to scripting, set expand variables, and Execute that alias. It'll probably make more sense in code (I've also added an option to send to scripting):

queue = {}

table.insert(queue, "say Hello!")
table.insert(queue, {interp=true, text="tell @person Hello!"})
table.insert(queue, {interp=true, script=true, text="Send('tell @person Hello!')"})

function remove_alias(name)
  EnableAlias(name, false)
  DoAfterSpecial (1, "DeleteAlias ('" .. name .. "')", 12) -- delete it

function sendQueue()
  local tosend = table.remove(queue)
  -- if its a plain string then we don't need to do anything special
  if type(tosend) == "string" then
  -- if its a table then we need to execute it through an alias
  elseif type(tosend) == "table" then
    -- set the default flags for the alias
    local flags = alias_flag.Enabled + alias_flag.Temporary
    -- if the "interp" option is set then alias needs to expand variables
    flags = tosend.interp and (flags + alias_flag.ExpandVariables) or flags
    -- get a unique number to use for alias' match and name
    local alias_id = GetUniqueID()
    -- create the alias
    AddAlias("queue_alias_" .. alias_id, alias_id, tosend.text, flags, "remove_alias")
    -- if the "script" option is set then the alias must also send to scripting
    if tosend.script then
      SetAliasOption("queue_alias_" .. alias_id, "send_to", "12")
    -- execute the alias

-- test it
[Go to top] top

Posted by Nick Gammon   Australia  (21,608 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 27 Sep 2006 08:45 AM (UTC)

Amended on Wed 27 Sep 2006 08:50 AM (UTC) by Nick Gammon

I was going to do it a bit differently, both methods no doubt have their merits. :)

First, we need to replace variables in the command you type, I did it with a string.gsub, which calls a function to do the replacement:

   match="queue *"
  <send>queue = queue or {}  -- make table if it doesn't exist

  local command = [[%1]]  -- get wildcard 1
  local errors =  {} -- no errors yet
  -- function to do the replacements for string.gsub
  local function replace_variables (s)
    s = string.sub (s, 2)  -- remove the @
    replacement = GetVariable (s)    -- look up variable
    if not replacement then  -- not there, add to errors list
      table.insert (errors, s)
    end -- not there
    return replacement  -- return variable
  end -- replace_variables 
  -- replace all variables starting with @
  command = string.gsub (command, "@%a[%w_]*", replace_variables)
  -- report any errors
  if table.getn (errors) &gt; 0 then
    for k, v in ipairs (errors) do
      ColourNote ("white", "red", "Variable '" .. v .. "' does not exist")
    end -- for
  end -- error in replacing
  -- add to queue
  table.insert (queue, command)
end  -- do

This will gradually add things you queue into "queue" table, which it creates if it doesn't exist yet.

Now we need a timer to pull them out of the queue:

  <timer enabled="y" second="2.00"    send_to="12"
  <send>if type (queue) ~= "table" then
end -- if no queue

if table.getn (queue) == 0 then
end -- if nothing in queue

-- get head of queue, send to MUD
Send (table.remove (queue, 1))</send>


This timer I set to fire every 2 seconds, just adjust that to your requirements.

This pulls the head of the queue out and sends it to the MUD.

- Nick Gammon

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

Posted by Wyd   (14 posts)  [Biography] bio
Date Reply #3 on Thu 28 Sep 2006 10:12 PM (UTC)

Thats really helpful, and exaclty what I needed. One question though, as both scripts have a different way of handling things, are they better in different situations? Not really needed, but it'll be interesting to know.

[Go to top] top

Posted by Nick Gammon   Australia  (21,608 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Fri 29 Sep 2006 08:29 PM (UTC)
Well, Ked's version creates aliases on-the-fly to let MUSHclient's internal routines expand variables, and then sets a timer to delete them a moment later.

He also supports scripting in the queue.

Mine is a bit simpler, but doesn't have the support for scripting. I suppose it depends which you find easier to use.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


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.


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 FutureQuest]