[Question] How to use FSM.State?

Post Reply
DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

[Question] How to use FSM.State?

Post by DartKotik » Fri Jan 25, 2019 11:43 am

Hi, Jarod. Could you help me?

I want to create my own State class. I wrote some code

Code: Select all

    class Identify : State
    {
        public override int Priority => 100;
        public override string DisplayName => GetType().Name;
        public override bool NeedToRun => true;
        public override int CheckInterval => 1;
        public override bool StopNavigator => false;

        public override void Run()
        {
            Debug.WriteLine(DisplayName + ": tick with Priority=" + Priority + ", CheckInterval=" + CheckInterval);
            //Do Anything
        }
    }
Then in class Core:Plugin I add this to MainEngine:

Code: Select all

    public class Core : Plugin
    {
        public override void OnLoad()
        {
            Astral.Quester.API.Engine.AddState(new States.Identify());
            Astral.Quester.API.Engine.OnEnterState += Engine_OnEnterState;
        }
        private void Engine_OnEnterState(object sender, Astral.Logic.Classes.FSM.Engine.EngineArgs e)
        {
            Debug.WriteLine("OnEnter to State: " + e.state.DisplayName);
        }
    }
And get message in Logger:

Code: Select all

14:28] Identify state loaded (p100)
Then every time the Identify timer ticks, I expect to receive a message in the log. But I do not see him among all other states.

Code: Select all

[14:41] OnEnter to State: CheckAction
[14:41] OnEnter to State: Combat
[14:41] Attack : Бес
[14:41] OnEnter to State: CheckAction
[14:41] MoveTo completed (success)
[14:41] Action set to : MoveTo
[14:41] OnEnter to State: CheckAction
[14:41] OnEnter to State: CheckAction
[14:41] Town enabled.
[14:41] OnEnter to State: CheckAction
[14:41] OnEnter to State: Vendor
[14:41] Sell 'Бесценный платиновый таз' ...
What am I doing wrong?
Sorry for my English. I translate with Google.
Quester Assistant plugin

User avatar
Jarod46
Administrator
Administrator
Posts: 1880
Joined: Fri Aug 13, 2010 12:31 pm

Re: [Question] How to use FSM.State?

Post by Jarod46 » Fri Jan 25, 2019 12:25 pm

State is cleared/reloaded on role start, you have to use Astral.Quester.API.FireBeforeStartEngineEvent event to add your state.
Only account support via private messages
Low English level, sorry

DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

Re: [Question] How to use FSM.State?

Post by DartKotik » Fri Jan 25, 2019 12:45 pm

Thank you.

Next question. How to manage states with Priority and CheckInterval? I have set 80 and 2000 and expected the event will be every 2 seconds. But I got this spam less than half a second:

Code: Select all

[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
[15:37] Identify: tick with Priority=80, CheckInterval=2000
If I set the priority 50 and the interval 50, then I get too low ticks. I understand that a larger number has a higher priority and the tick time is specified in milliseconds. How to set the required behavior?
Sorry for my English. I translate with Google.
Quester Assistant plugin

DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

Re: [Question] How to use FSM.State?

Post by DartKotik » Fri Jan 25, 2019 1:35 pm

Is seems that after execution Run() in Engine the timer CheckTO isn't resetting. In previos log I got same spam with "OnEnter to State: CheckAction" message.
Adding CheckTO.Reset() into the end of Run() solves the problem, but I think it should be done at Engine level.
Sorry for my English. I translate with Google.
Quester Assistant plugin

User avatar
Jarod46
Administrator
Administrator
Posts: 1880
Joined: Fri Aug 13, 2010 12:31 pm

Re: [Question] How to use FSM.State?

Post by Jarod46 » Fri Jan 25, 2019 4:30 pm

This is how CheckInterval work but you can easily implement your own timer in your state.
Only account support via private messages
Low English level, sorry

DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

Re: [Question] How to use FSM.State?

Post by DartKotik » Fri Jan 25, 2019 5:31 pm

Where State.CheckTO is resetting?
Sorry for my English. I translate with Google.
Quester Assistant plugin

User avatar
Jarod46
Administrator
Administrator
Posts: 1880
Joined: Fri Aug 13, 2010 12:31 pm

Re: [Question] How to use FSM.State?

Post by Jarod46 » Fri Jan 25, 2019 7:40 pm

You can simply doing something like that

Code: Select all

class AState : Logic.Classes.FSM.State
{
    public override int Priority => 100;

    public override string DisplayName => "AState";        

    public override int CheckInterval => 0;

    public override bool StopNavigator => true;

    Astral.Classes.Timeout checkTimeout = new Astral.Classes.Timeout(2000);
    public override bool NeedToRun
    {
        get
        {
            return checkTimeout.IsTimedOut;
        }
    }

    public override void Run()
    {
        Astral.Logger.WriteLine("Doing something");
        checkTimeout.Reset();
    }
}
Only account support via private messages
Low English level, sorry

DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

Re: [Question] How to use FSM.State?

Post by DartKotik » Fri Jan 25, 2019 8:24 pm

Of coarse I found such solution. I talk about that your code never resets all timers of states. Or I cant found that. In ends we get list of states wich every need to run. State with highest priority hangs up the queue in Engine. Perhaps this is cause of freezing on campfire.
Sorry for my English. I translate with Google.
Quester Assistant plugin

User avatar
Jarod46
Administrator
Administrator
Posts: 1880
Joined: Fri Aug 13, 2010 12:31 pm

Re: [Question] How to use FSM.State?

Post by Jarod46 » Fri Jan 25, 2019 8:54 pm

Interval timeout is only reset when needToRun return false, I originally added checkInterval property to reduce cpu usage of some states which doesn't need to be checked at every engine pulses and for my usage this is more convenient to have this behavior.
Only account support via private messages
Low English level, sorry

DartKotik
Contributor
Contributor
Posts: 1062
Joined: Sat Jan 07, 2017 4:00 pm

Re: [Question] How to use FSM.State?

Post by DartKotik » Tue Jan 29, 2019 9:37 am

Thanks for answers!
Sorry for my English. I translate with Google.
Quester Assistant plugin

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests