Asynchronous tasks in Unity ?

Hello, I am trying to create a trading card game in Unity, using MVC.
My problem is that my model can be waiting for controller answers, without any time system within the model.

For exemple, when a card is summoned, I call a controller function which asks to the player where he wants to summon it and returns this tile to the model.

This controller have a function automatically called when a tile is choosen. But I don’t know how to wait for this function to be called. I thought this was possible with async and await but I didn’t found Tasks in Unity (maybe because it’s not C#4.0, that point is really annoying though).

For those who want to know how my code works, here’s the model part :

public void Summon (Card card, bool fromHand) {

    Auto.Notify ("character.summon.before", this, card, fromHand);
    //Waits for controller answer
    card.location = Control.ChooseEmptyTile ();
    if (card.location == null)
        Auto.Notify ("character.summon.failed", this, card, fromHand);
    else
        Auto.Notify ("character.summon.resolved", this, card, fromHand, card.location);
}

And here’s the controller part :

//Function automatically called when the tile is choosen
public void OnNotification (string notification, object sender, params object[] data) {

    if (notification == "tile.choose")
        choosenTile = data [0] as Tile;
}

public Tile ChooseEmptyTile () {

    //How to wait for OnNotification() call ?
}

async and await is not in the version of mono we’re in. Though you can add it (there’s a thread out there somewhere on adding it), or if you use the latest beta version.

Though, this doesn’t really sound like something you need async/await for.

You could just use a callback delegate or something.

I searched but didn’t really understood how to implement this. I already used delegates but not this way. Is it really possible to wait for another thread using this ? How should I use it ?

Why does it have to be another thread?

From your description it sounds like you’re waiting for an event… this doesn’t necessarily imply threading.

BRB, I’ll write up a basic little example of what I mean.

//edit back

the scenario is the ScriptA waits for player to press A key, it notifies ScriptB. ScriptB now waits for a mouse click, and responds back.

public class ScriptA : MonoBehaviour
{
  
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            this.GetComponent<ScriptB>().Notify(this.OnScriptBReturn);
        }
    }
  
    private void OnScriptBReturn()
    {
        Debug.Log("DONE");
    }
  
}

public class ScriptB : MonoBehaviour
{
  
  
    public void Notify(System.Action callback)
    {
        this.StartCoroutine(this.Routine(callback));
    }
  
    private System.Collection.IEnumerator Routine(System.Action callback)
    {
        while(true)
        {
            if(Input.GetMouseButtonDown(0))
            {
                callback();
                yield break;
            }
            yield return null;
        }
    }
  
}

Of course this is a generic example.

But the idea is that ScriptB may take a while to wait for some event.

In your case it’s the controller requesting information from the player, and waiting for the player to click something, and then returning that information to the model.

Although I will say this is a weird communication between model and controller.

In MVC it’s usually the controller that manipulates the model. The model doesn’t usually do anything to the controller.

This is the general interaction flow in MVC (from wikipedia):

Yeah, I think the problem is here.
I want my model to receive data from the controller, but the model isn’t in a routine.

So I can’t just make a call card.location = Control.ChooseEmptyTile (); and asking for input when this method is called.
I thought my game using the usual MVC, but I was asking data from the controller and I didn’t prepared this.

I think I have to choose my tile in the controller before calling the card Summon() method in the model.

Thank you for your replies, by the way ^^