C.Galbraith
C.Galbraith

Reputation: 71

TaskScheduler is not working within a conditional context inside of UpdateAI

I have been adding TaskScheduler to a ScriptedAI (Raliq the Drunk for reference). Whilst out of combat he should repeatedly animate drinking/eating every 5 seconds.

Current non-working code is:

        void UpdateAI(uint32 diff) override
        {
            if (!me->IsInCombat())
            {
                _scheduler.Schedule(5s, [this](TaskContext context)
                {
                    me->HandleEmoteCommand(EMOTE_DRINK);
                    context.Repeat(5s);
                });
            }

            if (!UpdateVictim())
                return;

            _scheduler.Update(diff, [this] {
                DoMeleeAttackIfReady();
            }); 
        } 

What I think is very similar code which I have confirmed DOES work is:

        void JustEngagedWith(Unit* /*who*/) override
        {
            _scheduler
                .Schedule(5s, [this](TaskContext context)
            {
                DoCastVictim(SPELL_UPPERCUT);
                context.Repeat(15s);
            });
        };

UpdateAI does fire.

So far I have tried to follow the debugger stepping through the scheduler code, as it does reach it. I have not been able to determine from that why/where it is failing.

I cannot see why the JustEngagedWith() code block works, whilst the near identical usage within UpdateAI() does not.

Any thoughts or recommendations would be greatly appreciated!

Upvotes: 2

Views: 51

Answers (2)

Honey55
Honey55

Reputation: 425

UpdateAI happens on every tick, so ideally every 0.5s. That way you would create 10 scheduled actions every 5s for that creature. I don't think it is intended to be used that way. There might be a limit to the allowed number of scheduled events and you just keep overwriting the queue.

Upvotes: 1

C.Galbraith
C.Galbraith

Reputation: 71

_schedule.Update() was not being reached, so the scheduled task(s) were not being updated. Do not schedule tasks inside UpdateAI - it's not designed for that.

Upvotes: 2

Related Questions