# Time AI

## Add Coroutine To AI

The default behaviour of AI is execute immediately when it's on their turn. However, a delay, before they starts their calculation can be done through steps below.



**Steps 1 - Remove unnecessary code**

In ChessGameAIGUI script, there are numerous line of code contains macro `#if (UNITY_WEBGL && !UNITY_EDITOR)`, uncomment or remove them entirely. Do the same for the **content** inside the `#else` statement, if there's any. In the end, what remains will be those code within the if statement.&#x20;

\*EXCEPT those macro in `RequestAIMove()` function, just don't do this step in the function, otherwise, screen will freeze while AI is calculating.



**Steps 2 - Migrate code to next steps**

In the `RequestAIMove()` function,  remove the line `AITurnEffect(isAIMoving);`&#x20;



**Steps 3 - Making the request to AI move function a timed coroutine**

In the IEnumerator `AITurn()` function, the code within should become something like

```
        //Letting AI turn effect starts spinning before AI starts it's calculation
        AITurnEffect(!HasGameFinished());

        //The seconds should be the time to delay AI before it starts calculation
        yield return new WaitForSeconds(seconds); 

        RequestAIMove();
```

