Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Make 2D World Generation asynchronous with the game

I'm just watching a small tutorial how to create a loading screen, it loaded my scene correctly. But the problem is the progress is printed as 100%, it wait (lag the game) for the world generating to execute. I wonder how would I make the world generation asynchronous with the game so that I can add the progress bar while waiting for the world to load (like Terraria did)

World generation code skeleton:

void GenerateWorld() {    
            Vector3Int bottomLeftOrigin = GetBottomLeftOrigin();            
            int treeSpace = TreeMinimunDistance;

            Tilemap tilemap = WorldControllerScript.instance.solidTilemap;

            Random.InitState(terrainSeed);

            TerrainTile tileStone = Resources.Load<TerrainTile>(TerrainTilePath + TileID.Tile_Stone);
            TerrainTile tileDirt = Resources.Load<TerrainTile>(TerrainTilePath + TileID.Tile_Dirt);
            TerrainTile tileGrass = Resources.Load<TerrainTile>(TerrainTilePath + TileID.Tile_Grass);

            //Generating stone, dirt, grass layer
            for (int x = 0; x < worldTileSize.y; x++) {
                //Calculate stone, dirt, grass layer
                for (int y = 0; y < worldTileSize.y; y++) {
                      //Set blocks
                }
            }
        }

Best Answer

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited August 2020 Accepted Answer

    I hab similar problem with my 3d maze generation then i cane up with 3 solutions.


    1). Just get every information in the start and then visualize it in the update function and used a int to count the iterations to show the progress in the loading bar


    2).recursion of coroutines


    3).make everything in start and then add a fake loading bar

Answers

  • edited August 2020

    I've tried to use Coroutine but idk how to use it... Should I use it with something like yield return null?

  • Yes.

    A coroutine without a yield statement, is just like a normal function, and will not run asynchronously.

Sign In or Register to comment.