• Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Quick coroutine in plugin example.

    Discussion in 'Community Developer Forum' started by mastahg, Dec 5, 2014.

    1. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      Just a quick example.
      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using Buddy.Coroutines;
      using ff14bot.AClasses;
      using ff14bot.Behavior;
      using ff14bot.Enums;
      using ff14bot.Helpers;
      using ff14bot.Managers;
      using TreeSharp;
      
      namespace BetterCoroutineExample
      {
      
          public class BetterCoroutineExample : BotPlugin
          {
              internal async Task<bool> DoSomething()
              {
      
                  foreach (var bagslot in InventoryManager.EquippedItems)
                  {
                      if (bagslot.BagId == InventoryBagId.Armory_MainHand)
                          continue;
      
                      if (bagslot.SpiritBond >= 100)
                      {
                          await CommonTasks.ConvertToMateria(bagslot);
                      }
                  }
                  //Don't block the logic below us in the tree.
                  return false;
              }
      
              public override void Dispose()
              {
              }
      
              public override void Pulse()
              {
      
              }
      
              public override string Author
              {
                  get { return "Mastahg"; }
              }
      
              public override Version Version
              {
                  get { return new Version(0, 0, 1); }
              }
      
              public override string Name
              {
                  get { return "Better Coroutine example"; }
              }
      
      
              private Composite _coroutine;
              public void OnInitialize()
              {
                  //OnInitialize only gets called once, so create the object here so that it can be properly removed later.
                  _coroutine = new ActionRunCoroutine(r => DoSomething());
              }
      
      
              public override void OnShutdown()
              {
      
              }
      
      
              public override void OnDisabled()
              {
                  //Remove our event handler and remove our coroutine from the logic tree
                  TreeHooks.Instance.OnHooksCleared -= OnHooksCleared;
                  TreeHooks.Instance.RemoveHook("TreeStart", _coroutine);
              }
      
              public override void OnEnabled()
              {
                  //Add our hook to the logic tree here and setup event handler for when the treehooks are cleared
                  //We want to add our hook here incase the user enables the plugin once the bot is already running.
                  TreeHooks.Instance.AddHook("TreeStart", _coroutine);
                  TreeHooks.Instance.OnHooksCleared += OnHooksCleared;
              }
      
              private void OnHooksCleared(object sender, EventArgs args)
              {
                  TreeHooks.Instance.AddHook("TreeStart", _coroutine);
              }
      
          }
      
      }
      
       
    2. Yasuko

      Yasuko Member

      Joined:
      Oct 28, 2010
      Messages:
      314
      Likes Received:
      6
      Trophy Points:
      18
      You mind reader you.. Thank you for posting this.
       
    3. Neverdyne

      Neverdyne Community Developer

      Joined:
      Sep 12, 2014
      Messages:
      649
      Likes Received:
      18
      Trophy Points:
      18
      If we can do that, does it mean the API is thread safe? For example, what happens if two threads call Navigator at the same time?
       
    4. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      No. Nothing is thread-safe. Doing anything in another thread will likely break things in terrible ways along with burning your house down.
       
    5. Neverdyne

      Neverdyne Community Developer

      Joined:
      Sep 12, 2014
      Messages:
      649
      Likes Received:
      18
      Trophy Points:
      18
      So that's the smoke I was smelling...
       
    6. Yasuko

      Yasuko Member

      Joined:
      Oct 28, 2010
      Messages:
      314
      Likes Received:
      6
      Trophy Points:
      18
      So I've been messing with this for a little bit and got it working in a plugin that I did, but my question is.. Is there a way to stop orderbot completely to allow it to convert, Ive tried a few ways from using botmanager.current.stop to using treeroot.stop (treeroot would stop the pulse all together, wouldnt it?).. or which would be the best way to tell orderbot to pause while converting/equiping a new piece of gear?
       
    7. Neverdyne

      Neverdyne Community Developer

      Joined:
      Sep 12, 2014
      Messages:
      649
      Likes Received:
      18
      Trophy Points:
      18
      Just to clarify, the Resume() method has to be called continuously as a tick?
       
    8. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      When you call resume it runs it until the next await returns iirc.
       
    9. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83

      As for this, what you'll want todo is hook "TreeStart" and do something like

      Code:
      new Decerator(r=>IsDisenchanting, new actionalwaysuccededdededed())
      
      and then when you start doing stuff set that to true and the rest of the tree wont execute then once your done, set it false and itll resume normal operation
       
    10. Neverdyne

      Neverdyne Community Developer

      Joined:
      Sep 12, 2014
      Messages:
      649
      Likes Received:
      18
      Trophy Points:
      18
      Thanks! This is exactly what I needed to finish my avoidance plugin.
       
    11. Yasuko

      Yasuko Member

      Joined:
      Oct 28, 2010
      Messages:
      314
      Likes Received:
      6
      Trophy Points:
      18
      Mastahg,

      I've tried to follow on how you suggested to stop the bot in order to do converts, but no matter which way I try to do it, it appears that the behavior I'm trying to call never gets called. Here is what I currently have:
      Code:
      (in OnEnabled)
                  _cache = Behavior();
                  TreeHooks.Instance.AddHook("TreeStart", _cache);
      
      and as for the composite behavior, I have:

      Code:
              private Composite Behavior()
              {
                  return new Decorator(r => timeToConvert,
                      new Action(r =>
                    Logging.Write("Behavior Called")
                    )
                    );
              }
      
      (i removed the actionalwayssuccedded to put a logging action in there to see if it gets fired or not).. I have tried to add the hook as well as inserthook.. and no matter which way I try.. it appears that the behavior never gets called.

      Forgive me for such stupid questions, as I'm not too familiar with all this.. I went over to the HB section and read about treehooks and hooking into trees.. but anything I seem to do.. just doesnt work.. do you have any advice?
       
    12. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      You need to add a event handler to
      Code:
      TreeHooks.Instance.OnHooksCleared
      
      Right now your adding a treehook but then somewhere else its being cleared so you need to readd it when that event fires.
       
    13. Yasuko

      Yasuko Member

      Joined:
      Oct 28, 2010
      Messages:
      314
      Likes Received:
      6
      Trophy Points:
      18
      Thank you very much for the fast reply! I had no idea that it was being cleared some how, as I didnt see that in any RB logging being done.. but that fixed the issue.. I appreciate your help with it all, thank you!
       
    14. wilderg99

      wilderg99 New Member

      Joined:
      May 31, 2014
      Messages:
      245
      Likes Received:
      0
      Trophy Points:
      0
      hey yasuko did you get the convert plugin working?
       
    15. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      I've gone and updated the original post with a better example. The original example wasn't the proper way things should be done.
       
    16. Exmortem

      Exmortem Community Developer

      Joined:
      Mar 28, 2010
      Messages:
      799
      Likes Received:
      16
      Trophy Points:
      18
      Might just be on my end, but this example doesn't seem to be working anymore. Pulse() should be OnPulse() and it's missing override on OnInitialize(). However, it still isn't firing off the coroutine. Possible that something has changed in a patch along the way?

      Edit: The hooks are working, i'm just derpy.
       
      Last edited: Aug 30, 2016
    17. Jokoast

      Jokoast New Member

      Joined:
      Mar 12, 2017
      Messages:
      11
      Likes Received:
      1
      Trophy Points:
      3
      Is there a "how to" about plugin dev ?
      I don't know where to start and how RB really works and i want to dive in to dev a PotD 1-10 loop plugin.

      Thx,

      regards,
       
    18. zzi

      zzi Active Member

      Joined:
      Mar 10, 2016
      Messages:
      308
      Likes Received:
      47
      Trophy Points:
      28
      take a look at the Honorbuddy forums, much of the topic there about building a plugin or botbase are similar here. you can also look at some of the existing plugins & botbases in the forum to get an idea of how to start.

      You wouldn't be building a DeepDungeon as a plugin it would be a botbase. Potd is also reallly challenging to do well (especially because there isn't very much support for the deep dungeon interfaces and data in reborn buddy right now. the navigation server doesn't support the deep dungeon maps because they don't have a proper zoneid. (zoneid = 0, but they have a rawid and the navserver just uses zoneid)

      hope that helps you get started some.
       
      y2krazy likes this.
    19. Sinbeard

      Sinbeard New Member

      Joined:
      Oct 14, 2019
      Messages:
      14
      Likes Received:
      1
      Trophy Points:
      1
      I'm a total noob with RB dev, and even VS.

      With that in mind, I fish a bunch using the Fishing botbase and tried my hand at creating a small plugin that will simply click a Collectable window yes/no depending on preset fish collectability, etc. since the Fishing bot doesn't do it automatically.

      I followed the example here and I encountered a problem a few others here have had - my coroutine doesn't seem to fire at all. My logic (mostly) works when I put it in the OnPulse method, so it's clear I'm ignorant of something here. Can someone point me in the right direction? My goal is ultimately to try my hand at a fairly robust gathering botbase, so I'm taking steps in that direction.

      Anyway, here's the code attached.
      Thanks!
       

      Attached Files:

    20. mastahg

      mastahg Administrator Staff Member

      Joined:
      Feb 27, 2011
      Messages:
      5,343
      Likes Received:
      382
      Trophy Points:
      83
      Try adding some logging to the the top of PressCollectableButton and see if its getting run. That looks like it should work. I think the sidestep plugin uses a coroutine hook internally so you could look at that for an example.
       

    Share This Page