Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Coroutines and plugins

Coroutines and plugins

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


Posted by Edly   (5 posts)  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.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
Top

Posted by Edly   (5 posts)  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.
Top

Posted by Fiendish   USA  (2,533 posts)  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
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.


12,638 views.

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

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.