Handling task AI

(Sorry if this question is blindingly idiotic, but I’m a designer trying to feel my way through programming)

So I’m prototyping a game that has simulated villages. The town centre handles all the thinking and delegates tasks to villagers, who blindly do as they are told and then wait for more instructions unless their task gets interrupted by the player.

A task at the moment is defined as a class containing an async method that is overridden from an abstract task class. The task is created by the building that requires the villager and added to the villager’s task list.

Now. Needing to cancel a task is where things are going wrong. Is it better to just use coroutines or attempt to get async to work? I’ve never used async until now and am just wanting to play around with it. I was also unsure whether Co-routines would be an issue, given every villager could potentially be running one at the same time.

Something to consider when using Async is that, unlike Coroutine, Async does not respect object lifetime. When an object is destroyed, its associated Coroutines will destroy as well, but Async processes will keep going and you will have to use Cancellation Tokens.

Also, Async tasks will have scenarios where they will mute exceptions, and will make them more difficult to debug. If you want to use Async, it’s best to go a little in depth with how they work.

Generally, it’s safer to work with Unity’s Coroutines.