I’m working on a skill system, and I realize that I need to make skills asynchronous to some degree to permit me to add interruptions in the future. My goal is that when an actor uses a skill, the skill’s function can be paused under specific situations to allow a player to react to it. I thought using C# Tasks would be the solution based on this article and what google seems to tell me, but I can’t quite seem to get it to work.
I’ve just encapsulated my method under a Task, but it doesn’t seem to wait for the completion of certain lines of code, such as a LinecastAll, or Random. Wondering what the best solution is for code on one function to pause until the completion of another function in a separate thread?
Here’s what I have as an example:
private void useStrike(Unit unit, Unit defender, MAP attackingMap)
{
Task.Factory.StartNew(() =>
{
// Various non loop code that is waited correctly
RaycastHit2D[] allHits = Physics2D.LinecastAll(defender.GetPosition(), unit.GetPosition());
// Various other code, some stuff does not wait ie Random.Range()
}).Wait();
}
I have async statements later on down the road, which once these preliminary code finishes, should allow it to pause the corresponding function.