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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Running Large Calculation Without the Game Freezing?

Discussion in 'General Discussion' started by DockAnkh, Jan 26, 2023.

  1. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    I'm working on a board game, a replication of Othello, and I created a tree of all possible moves (with a few moves left) so that the AI I made can see the best possible move. However, the size of the tree grows exponentially with the amount of remaining moves. It can take a while to create the tree. That's fine. Even if I got the tree to work faster I would just increase its size. My problem is that the game freezes while it is running this calculation. I'd like the game to not freeze while this was happening.

    I've read some about similar problems but I'm not sure if I understand the possible solutions correctly. I think my options are: One, do the calculation for making the tree in batches (a few each frame) so that the program could still run while doing the calculations. Two, use the jobs system for some sort of multi-hreading so that the main thread of the game could keep on while another thread built the tree. Am I understanding my options correctly? Do you have any advice where I can look for an overview of either method? For example, how do you do only some of the calculations each frame? Thank you.
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,799
    Break the additions up to handle so many calcs per frame. It is turn based so if you did 20 a frame at one sec at 60FPS you could calculate 1200 nodes.
     
    Ryiah likes this.
  3. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,492
    That should be the easiest. Pack your calculations into a Monobehavior where your variables you'd normally have in a loop or recursive call, are member variables instead. Then you just have the loop content in the Update() method.
    Another, more concise option would be to use Coroutines.

    The Job System would provide you actual multithreading and the powerful burst compiler. A speedup of factor 30 would not be surprising. However that is a significantly different way of thinking and unfortunately especially data structures like trees are not easy to represent (you cannot use classes or references - everything needs to be pretty much linear with the exception of the NativeHashMap).

    One more option that is there: "Normal threads". It's only the Unity API (aka all methods of Unity's built in classes) which strictly forbids being called from foreign threads. As long as you stick to regular C# classes of your own (and stuff like List<>) you still have all regular tools of C# at your disposal. Here's some introduction to that: https://www.knowledgehut.com/tutorials/csharp/csharp-multithreading
    You could start a singular thread just for your calculations, or employ multithreading with several custom threads if the work can be split up well (note: do not go overboard, unlike with Jobs where hundreds are okay, you shouldn't open more than a dozen threads).
     
    Last edited: Jan 26, 2023
    Ryiah and DockAnkh like this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Rather than using the (bloated) Thread namespace you could simply write an async/await-able method that runs in the background. That'll be the easiest route. Of course you'll be responsible for any race conditions etc whereas the Job system protects you from those, by enforcing strict coding guidelines which may seem counterintuitive at first.
     
    DockAnkh likes this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    coroutines, threads or async/await.
     
  6. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    I'm a bit confused by what you mean by "Normal threads." Do you mean just using threads without using Unity's whole Job System?
     
  7. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,492
    Exactly, just like you would do it in any other C# application.
     
    DockAnkh likes this.
  8. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    To anyone finding this thread later: I ended up using "normal threads" as DragonCoder suggested and it was much easier than I was worried it would be. This tutorial was helpful in going over the basics as well
     
    stain2319 likes this.