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 Parallelizing map generation?

Discussion in '2D' started by Dabonez, Nov 1, 2022.

  1. Dabonez

    Dabonez

    Joined:
    Feb 6, 2016
    Posts:
    6
    Hello!

    I'm currently working on a fairly standard 2D map generation algorithm. The map is divided into chunks and generates new chunks infinitely if the player keeps on moving towards the edges.

    My issue is that the creation of a chunk halts the game for a fraction of a second. I would like it to smoothly continue, because the chunk creation is triggered early enough so that the player should never walk into an ungenerated chunk. I've never worked with parallel computing, so I started reading about multithreading, and found dozens of pages talking about how poorly Unity handles threads as well as the risks/difficulties. From my understanding I could also use coroutines to split the work into tiny pieces and split it over a quantity of frames, but that seems like it would only fraction the halts into many smaller halts, as the coroutine runs on the main thread.

    I also ran into the IJob interface, which appears to be a safe way to multithread. Before delving deeper into the material I wanted to ask for some feedback. Which option would you choose for my situation?

    Thanks a lot!
    David
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Ted_Wikman and Dabonez like this.
  3. Dabonez

    Dabonez

    Joined:
    Feb 6, 2016
    Posts:
    6
    Alright, I'll try my hands on that. Thanks for the confirmation and the links!
     
  4. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    you need to first find what is causing your spike, your math or the generation of your visual graphics

    because you can thread your math but you cant thread your visual graphics

    and its unlikely that just math will cause your game to spike

    you should use coroutines
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    As the saying goes, be aware, opinions are free and worth every penny. ;) It doesn't mean it's true. There's nothing fundamentally wrong with jobs, Unity just wouldn't function if that were the case.

    Use the profiler, figure out what is causing the problem in detail. It might be simple to rectify it without a completely new strategy or you having to learn a whole bunch of stuff. Processing data in jobs is super simple and safe in Unity and if you use the mathematics package and Burst, you'll get massive performance boosts.

    The trick is to understand what is actually taking the time on the main-thread before you consider doing it in a job. Just running main-thread code in a job that isn't using multiple cores isn't going to gain you much if you're then just blocking on it on the main-thread.
     
    Ted_Wikman likes this.