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. Dismiss Notice

Game not printing or updating when AI is doing large calculations

Discussion in 'Editor & General Support' started by eliasjgnilsson, Feb 6, 2022.

  1. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Hi,

    I have a game where AI is doing large calculations when it is its turn. It searches the position to depth 1, then 2, then 3 etc. Between each depth I want to Instantaite a Gameobject with info about the depth to UI. The problem is that nothing happens until the AI is completely finished, then all items are added at once. Here is some code to explain better:

    Code (CSharp):
    1. private void AIMakeMove()
    2. {
    3.     for (int currentDepth = 1; currentDepth < maxDepth + 1; currentDepth++)
    4.     {
    5.         SearchPosition(currentDepth);
    6.     }
    7. }
    8.  
    9. private void SearchPosition(int _currentDepth)
    10. {
    11.     // Search the position to the given depth
    12.     score = Negamax(_currentDepth);
    13.  
    14.      // Print things   PROBLEM HERE
    15.     GameObject printItem = Instantiate(printItemPrefab, printItemParent.transform);
    16.     Debug.Log(_currentDepth);
    17. }
    I also tried just a simple Debug.Log instead of Instantiate but same thing happens then, all prints to console happens after the AI is done with its thinking process.

    Why is my UI not updating with information? I tell it to create some things after it run the first iteration with depth 0 but it skips this step and goes on depth 2 instead. Can someone please let me know how to get information out between each depth?
     
  2. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    Not exactly sure what you're meaning. First I thought you were doing some recursive function (which can take a look time if you're not careful) but I see you aint

    I tried your code with maxDepth = 5; and it printed out as expected near istantly

    1
    2
    3
    4
    5

    With the printing, I'm not 100% sure how unity does it, but I assume it buffers up all your Debug.Log stuff until it has a chance to print it, i.e. if you call Debug.Log(_currentDepth); it just sticks this on a TODO list and at the end of the frame goes now what shall I print down and then it prints everything at once, i.e. it does NOT stop whever its doing and then print something onscreen, and then continues executing the code
     
  3. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yes it prints it out immediately for you since the functions barely does anything. My Negamax function takes around a second for depth 4 and then say 5 seconds for depth 5.

    I don't see why it just ignores the print in between each depth and just keeps going. Why doesn't it print it immediately when I say and instead save up on all prints until all depths are finished?