Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

World generation / Data storing advice?

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

  1. HellsPlumber

    HellsPlumber

    Joined:
    Aug 17, 2014
    Posts:
    33
    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:
    4,748
    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:
    33
    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:
    4,748
    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.
     
unityunity