Is there any way to Run the TaskStatus Update every X seconds, similar to a coroutine? I have a quite heavy check it needs to do, and doing it each frame is not great.
Where I must put in the return TaskStatus.Running; ?
In my IENumerator coroutine I have a while loop with yield return new WaitForSeconds
I thought with wait yield return new WaitForSeconds(0.1f); I have a nice regenration effect in the energy points bar. But the tasks stops only of the wait task in the behaviour tree.
public class OnEnergyRegain : Action
{
public float RegenerationRateWhileResting = 1.0f;
Energy _energy;
Coroutine _coroutine;
public override void OnStart()
{
_energy = gameObject.GetComponent<Energy>();
}
public override TaskStatus OnUpdate()
{
_coroutine = StartCoroutine(Regenerate());
return TaskStatus.Success;
}
IEnumerator Regenerate()
{
while (_energy.ActualEnergy < _energy.GetMaxEnergy())
{
_energy.IncreaseEnergy(RegenerationRateWhileResting * 5f * Time.deltaTime);
yield return new WaitForSeconds(0.1f);
}
}
Yes, but where I have to put in this? In OnUpdate is only TaskStatus.running. If I understand the Task.cs correct only in OnUpdate I can put in TaskStatus. Sorry for my missunderstanding.
So easy? Thank you so much. I thought that I can put return only one times in OnUpdate
I have changed the code as follows
public override TaskStatus OnUpdate()
{
// _energy.IncreaseEnergy(50);
_coroutine = StartCoroutine(Regenerate());
if (_coroutineIsActive)
return TaskStatus.Running;
if(!_coroutineIsActive)
return TaskStatus.Success;
return TaskStatus.Success;
}
and entered the _coroutineIsActive=false; after the yield return new WaitForSeconds in the Coroutine and after the while loop. Works great.