I’m trying to write a manager that “ticks” the AI (movement and attack) of the beings in my game.
I want to centralise this so I can easily slow down and speed up time (and thus the speed of gameplay)
I write my AI using coroutines and yield to save state between calls, but I don’t know how to manually continue them.
I’m used to using StartCoroutine and having Unity call them each Update step.
How can I continue yield-ed methods manually?
Here’s my code if it helps:
public class TickManager : MonoBehaviour
{
public List<TickAI> m_beings = new List<TickAI>();
public float m_updateDelay;
float m_waitLeft = 0;
void Update()
{
m_waitLeft -= Time.deltaTime;
if (m_waitLeft <= 0)
{
m_waitLeft += m_updateDelay;
Debug.Log("Tick");
for (int i = 0; i < m_beings.Count; i++)
{
m_beings*.Tick();*
-
}*
-
}*
-
}*
- }*
- public class TickAI : MonoBehaviour*
- {*
-
// Coroutine-style state machine, using yield*
-
public IEnumerator Tick()*
-
{*
-
while (true)*
-
{*
-
if (HasDestination)*
-
{*
-
while (!ReachedDestination) {*
-
MovementTick();*
-
yield return null;*
-
}*
-
}*
-
else*
-
{*
-
if (HasTarget)*
-
{ *
-
// Start multi-step behaviour*
-
StartCoroutine(AttackBehaviour());*
-
}*
-
}*
-
yield return null;*
-
}*
-
}*
- }*