• Visit Honorbuddy
  • Visit Rebornbuddy
  • Visit Demonbuddy
  • Visit Exilebuddy
  • Visit Hearthbuddy
  • Visit Pokefarmer
  • Visit Buddystore

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:
    3,654
    Likes Received:
    102
    Trophy Points:
    63
    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 New Member

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

    Neverdyne Community Developer

    Joined:
    Sep 12, 2014
    Messages:
    605
    Likes Received:
    13
    Trophy Points:
    0
    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:
    3,654
    Likes Received:
    102
    Trophy Points:
    63
    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:
    605
    Likes Received:
    13
    Trophy Points:
    0
    So that's the smoke I was smelling...
     
  6. Yasuko

    Yasuko New Member

    Joined:
    Oct 28, 2010
    Messages:
    313
    Likes Received:
    6
    Trophy Points:
    0
    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:
    605
    Likes Received:
    13
    Trophy Points:
    0
    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:
    3,654
    Likes Received:
    102
    Trophy Points:
    63
    When you call resume it runs it until the next await returns iirc.
     
  9. mastahg

    mastahg Administrator Staff Member

    Joined:
    Feb 27, 2011
    Messages:
    3,654
    Likes Received:
    102
    Trophy Points:
    63

    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:
    605
    Likes Received:
    13
    Trophy Points:
    0
    Thanks! This is exactly what I needed to finish my avoidance plugin.
     
  11. Yasuko

    Yasuko New Member

    Joined:
    Oct 28, 2010
    Messages:
    313
    Likes Received:
    6
    Trophy Points:
    0
    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:
    3,654
    Likes Received:
    102
    Trophy Points:
    63
    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 New Member

    Joined:
    Oct 28, 2010
    Messages:
    313
    Likes Received:
    6
    Trophy Points:
    0
    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:
    3,654
    Likes Received:
    102
    Trophy Points:
    63
    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:
    791
    Likes Received:
    10
    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:
    3
    Likes Received:
    0
    Trophy Points:
    1
    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 Member

    Joined:
    Mar 10, 2016
    Messages:
    43
    Likes Received:
    1
    Trophy Points:
    8
    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.

Share This Page