[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]  Send command capture output and return collected data from function
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Send command capture output and return collected data from function

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


Posted by Lycynder   (2 posts)  [Biography] bio
Date Sun 04 Jun 2017 06:13 PM (UTC)

Amended on Sun 04 Jun 2017 06:24 PM (UTC) by Lycynder

Message
New to lua and Mushclient so if this is simple my apologies I've searched quite a bit

I would like to create a lua function that interacts with the mud but behaves like a standard function, i.e. returns data.

The example I have chosen is to gather all area info from the screen after a command and return it as a parsed table.

function capture_area_info()
  local max_captures = 500
  local captures = 0
  local areas = {}
  local area_line_regex = rex.new([[\s+(?P<min_level>\d+)\s+(?P<max_level>\d+)\s+(?:(?P<level_lock>\d+)\s+)?(?P<area_id>\w+)\s+(?P<area_name>[A-za-z0-9 ',-]+\w)\s*$]])
  Send("areas keywords")
  wait.make( --coroutine
    function()
      while captures < max_captures do
        captures = captures + 1
        local line = wait.match('*', 10)
        local a, b, matches = area_line_regex:match(line)
        if matches then
          areas[#areas+1] = {area_id=matches['area_id'], area_name=matches['area_name'], min_level=matches['min_level'], max_level=matches['max_level'], level_lock=matches['level_lock']}
          print(#areas)
        end
        if string.match(line, [[^'Lock' means you cannot enter until you are that level or higher.$]]) then
          done = true
          break
        end
      end
    end
  )
  -- I would like this to return the gathered area info but because wait is a coroutine this returns nil immediately.
  return areas
end

Thanks in advance.
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Sun 04 Jun 2017 09:34 PM (UTC)

Amended on Sun 04 Jun 2017 09:44 PM (UTC) by Fiendish

Message
.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #2 on Mon 05 Jun 2017 09:07 PM (UTC)
Message
Quote:
I would like to create a lua function that interacts with the mud but behaves like a standard function, i.e. returns data.


See my description of make.wait here to better visualize why what you're trying isn't working: https://mushclient.com/forum/?id=14004&reply=1#reply1

Personally, I think assigning a callback function is the best way to do what you want.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 05 Jun 2017 11:23 PM (UTC)
Message
Quote:

... but because wait is a coroutine this returns nil immediately ...


Exactly. The coroutine might take a few seconds to gather the data, so naturally expecting immediate results won't work.

Some kind of callback, like Fiendish suggested, could work. Or make the function (inside make.wait) display or otherwise deal with the results when they are all obtained.

- Nick Gammon

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

Posted by Lycynder   (2 posts)  [Biography] bio
Date Reply #4 on Tue 06 Jun 2017 04:56 PM (UTC)
Message
Thanks for the replies guys, I really appreciate you considering my issue.

I can certainly do the things you've suggested and I've made the adjustments to solve the specific problem I was having. Baking in the logic into the coroutine or passing a callback works but I was just hoping to get more reuse out of functions like this. I don't have much experience with event based languages so maybe I will be able to reuse it and I'm just not seeing the patterns.
[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.


1,492 views.

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.

[Home]


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]