Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Best solution for the AI to mimic waiting?

Discussion in 'Scripting' started by larswik, Feb 6, 2018.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Trying to find the best solution to have my AI to mimic a player. Right now a player would go to the store and buy new inventory items which takes time. The computer does it in a fraction of a second and then returns control to the player. I want to delay that a few seconds. I thought I was clever using StartCouroutine() but that seemed to set it on a separate thread and then continues processing the next line, which sends it back to the openStoreAITurn(). I am wondering how other people perform intentional waiting to give the AI a more human response?

    There were 3 separate things that happened that I want spaced out every 2 seconds and this did not work. I thought at first the Coroutine waited but it seems to send it off on a different thread and continues to process.

    Code (CSharp):
    1.  
    2.  
    3. void someMethod(){
    4.        StartCoroutine(openStoreAITurn(1));
    5.        StartCoroutine(openStoreAITurn(2));
    6.        StartCoroutine(openStoreAITurn(3));
    7. }
    8.  
    9. IEnumerator openStoreAITurn(int num){
    10.      yield return new WaitForSeconds(2f);
    11.         if (num == 1) {
    12.             // perform first action
    13.         }
    14.         if (num == 2) {
    15.             // perform actions
    16.         }
    17.         if (num == 3) {
    18.             // perform last action
    19.         }
    20.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Coroutines aren't on a separate thread, your method that calls them does not block while they run. Just have the coroutine wait the few seconds and set something that the rest of your code looks for to see if the AI should move on to the next step. Or you could do that all with accumulating timers in Update. Depends on how complex you want to make this.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Thanks, I took a different approach and it worked to delay each step 2 seconds using the coroutines still.

    Code (CSharp):
    1.     IEnumerator openStoreAITurn(){
    2.         yield return new WaitForSeconds(2f);
    3.             // do something here
    4.         yield return new WaitForSeconds(2f);
    5.             // do something here
    6.         yield return new WaitForSeconds(2f);
    7.             // do something here
    8.     }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just adding to this, if you wanted each store action to wait 2 seconds, I could suggest that you simply lay them out in order in 1 coroutine (rather than 3), and add a yield wait for seconds in between each action (if that's what you had in mind).:)
    After that, as mentioned, keep track that they're busy. Perhaps by setting a variable at the beginning of the coroutine and updating at the end, when it's all over.

    Nice, your post came in 2 seconds before I finished writing :)
     
    larswik likes this.
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I had a feeling you were going to suggest this approach anyway, so I went for it! :)