Search Unity

How to turn this loop into a job?

Discussion in 'Entity Component System' started by vsnry, Dec 9, 2018.

  1. vsnry

    vsnry

    Joined:
    May 4, 2018
    Posts:
    35
    I'm trying to add multithreading to my game as it's suffering from some performance issues by turning all of the loops into jobs, but I can't figure out how to actually do it. I tried everything I could think of. How would I turn this loop into a job?

    Code (CSharp):
    1. for (int i = 0; i < chunkPositions.Length; i++) {
    2.     //translate the player position and array position into chunk position
    3.     WorldPos newChunkPos = new WorldPos(
    4.         chunkPositions[i].x * Chunk.chunkSize + playerPos.x,
    5.         0,
    6.         chunkPositions[i].z * Chunk.chunkSize + playerPos.z
    7.         );
    8.  
    9.     //Get the chunk in the defined position
    10.     Chunk newChunk = world.GetChunk(
    11.         newChunkPos.x, newChunkPos.y, newChunkPos.z);
    12.  
    13.     //If the chunk already exists and it's already
    14.     //rendered or in queue to be rendered continue
    15.     if (newChunk != null
    16.         && (newChunk.rendered || updateList.Contains(newChunkPos)))
    17.         continue;
    18.  
    19.     //load a column of chunks in this position
    20.     for (int y = -4; y < 4; y++) {
    21.         buildList.Add(new WorldPos(newChunkPos.x, y * Chunk.chunkSize, newChunkPos.z));
    22.     }
    23.     return;
    24. }
    The latest error I got was "findChunkJob.world is not a value type. Job structs may not contain any reference types." so I'm confused on how I'm meant to pass things into it?
     
  2. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    148
    Antypodish likes this.