Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

World generation / Data storing advice?

Discussion in 'Scripting' started by HellsPlumber, Feb 8, 2019.

  1. HellsPlumber

    HellsPlumber

    Joined:
    Aug 17, 2014
    Posts:
    42
    Hi!
    I've reached a small hurdle and was hoping someone would be able to give me some advice or point me in the right direction.

    Basically I'm generating a small 3D world using 1 x 1 meshes in a 1 x 1 grid, by placing a random mesh in each grid space. This should happen when the game is first run, then it should be saved for the next time it's played. Think Animal Crossing, where your world is generated the first time you play, then remains the same there after.

    The issue is that I've never dealt with such a large amount of data before, so I have no idea how to manage the save/load. Adding each individual mesh prefab along with it's Transform details to a list seems extremely messy, and when I look at generated worlds such as MineCraft I know there's no way storing that much data can be as simple as a list.

    Most of my coding background consists of reading single entries from MySQL databases or looping through a list of names, so that's all I really know when it comes to storage.
    I'd massively appreciate some advice and pointer to learning resources.

    Thanks!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The easiest way to do this (and the way eg. Minecraft does it) is to store the seed used for the random generation.

    In essence, if you do this:

    Code (csharp):
    1. Random.InitState(12345);
    2. GenerateWorld();
    Your world should come out looking exactly the same, each time. This is because the random number generator gives the exact same sequence of "random" numbers from the same seed.

    So whenever you generate a world, save the state of the Random class to file, and then re-use that state when you load that same world. API

    Then, if you want to save changes the player makes to the world, you only save the changes. Eg. in Mincraft - if the player mines out a cave and builds a house, you save the deletion of the cave blocks and the addition of the house blocks. To load that world, first load the seed, and then apply all of the changes.
     
  3. HellsPlumber

    HellsPlumber

    Joined:
    Aug 17, 2014
    Posts:
    42
    Interesting!
    If that's the case for MineCraft I understand that a seed would be able to re-generate the originally generated world, but how would any changes that the player made to it be saved?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Probably in a very smart way.

    But the naive approach is just that whenever a cell is updated, you add that to a list of changed cells. Then you optimize from there.