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

Log on
Register forum user name Search FAQ

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]  Coroutines and plugins

Coroutines and plugins

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


Posted by Edly   (5 posts)  [Biography] bio
Date Sun 27 Dec 2020 02:02 PM (UTC)
Message
In my scripts, I make extensive use of coroutines to handle complex tasks. For example, here's my abbreviated "navigate" function that moves the player to a destination, handling any unexpected events as they arise (eg aggro mobs, closed doors, etc.)


function navigate(room_id, command)
  while true do
    if tonumber(gmcp("room.info").num) == tonumber(room_id) then
      break
    end

    EnableTriggerGroup("travel_group", true)
    local interrupted_reason = coroutine.yield()
    EnableTriggerGroup("travel_group", false)

    -- handle various interrupted_reason values
    if interrupted_reason == "combat" then
      ...
    elseif ...
      ...
    end

    Execute("mapper resume")
  end
end


And then I have various triggers to detect what interrupted us and resume the coroutine.

This works really well, except now I have a giant monolithic plugin, and I'd like to break it up. For example, I'd like to bundle navigate along with its triggers into a separate navigator plugin that can be used via CallPlugin.

The problem with this is that CallPlugin just doesn't seem to play nicely with coroutine.yield. What I want is to be able to write code like:


...
-- navigate to our destination
CallPlugin(navigator_id, navigate, destination)
-- CallPlugin blocks until we're at our destination
do_something_at_destination()
...


But what happens is that CallPlugin returns as soon as navigate hits the first coroutine.yield, so do_something_at_destination happens right away. It seems that each plugin runs in its own separate lua interpreter, so there's no way for coroutines to communicate across plugin boundaries.

I tried getting around this with callbacks - eg, passing a command for navigate to execute only once it's actually finished, but that quickly devolved into a mess. For example:


...
-- navigate to our destination
CallPlugin(navigator_id, navigate, destination, "original_plugin resume")
-- wait for "original_plugin resume" command to resume us
coroutine.yield()
do_something_at_destination()
...


This pattern only works if navigate is *guaranteed* to yield. But, if it can maybe return without yielding (because eg we were already at the destination to begin with), then the callback gets called before CallPlugin returns, so the calling function yields and never resumes. You can have navigate return a value to indicate whether it completed without yielding, but then you lose the ability to return some other useful value. And you end up with extra boilerplate around every call to navigate.

Any thoughts? I think what I'd really like is a CallPluginSynchronous or something like that that will only return when the target function is completely finished, but not if it just yields temporarily.
[Go to top] top

Posted by Nick Gammon   Australia  (22,556 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 28 Dec 2020 09:30 PM (UTC)
Message
Quote:

It seems that each plugin runs in its own separate lua interpreter, ...


Yes it does. Plugins don't even have to be written in Lua, so each plugin has its own script space.

Quote:

This works really well, except now I have a giant monolithic plugin, and I'd like to break it up.


I suggest to achieve that objective you simply make multiple Lua files, and "dofile" them into your plugin. For example, in your main plugin file replace all the script section with:



<!--  Script  -->

<script>
    dofile (GetPluginInfo (GetPluginID (), 20) .. "My_Navigate_Script.lua")
</script>



The script part now goes into that Lua file. That has the added advantage that text editors do a better job of syntax highlighting if the entire file is Lua. Then, if you have so much script that it is still bulky, "dofile" extra files.

- Nick Gammon

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

Posted by Edly   (5 posts)  [Biography] bio
Date Reply #2 on Tue 29 Dec 2020 12:16 AM (UTC)
Message
Thanks, that's a good workaround. It has the disadvantage of not being able to bundle the required triggers in, but I might want to create them on the fly via lua anyway.
[Go to top] top

Posted by Fiendish   USA  (2,315 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Tue 29 Dec 2020 04:36 PM (UTC)

Amended on Wed 30 Dec 2020 01:11 AM (UTC) by Fiendish

Message
It seems like your previous thread on extending plugins would help you here. https://mushclient.com/forum/bbshowpost.php?bbsubject_id=14563

https://github.com/fiendish/aardwolfclientpackage
[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.


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