Search Unity

How to save and load objects in a large open world?

Discussion in 'Scripting' started by Lesnikus5, Mar 26, 2020.

  1. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    I want to create a large world of 64 square km with dynamic loading and saving objects in a certain radius from the player. Objects must remember their position if it was changed, even after restarting the game. I thought of some aspects, but with some I need help.

    There is a save file. It contains the ID and position of each object in the game world. When a player moves, objects appear on a certain radius from the player based on data from the save file. ID determines the prefab used, position is the location of the object's spawn. When a player moves away from objects, they are destroyed. But before that, if the player moved them, the objects save their new position back to the file. That is, their updated position is saved.

    QUESTION 1:

    Probably to store millions of objects, you need to use a Dictionary, nested one in another. This is due to the fact that 2 numbers will be used as a key: X and Y positions of the object.
    Is this the right choice? As far as I understand, access will be faster if you use one Dictionary, and not two, since the access time to the cell is always the same regardless of the number of objects inside the Dictionary. Accordingly, in my case, the time will be doubled compared to the use case of 1 Dictionary.

    How can I use a 2D or 3D position as a key for a single Dictionary?

    QUESTION 2:

    How to organize the loading and saving of objects located only in a certain radius around the player?

    Option 1. Use the position of the object as a key to access its data using Dictionary <TKey, TValue> .ContainsKey (TKey). But how to specifically implement this? I am having a problem in spawning objects. How can I load an object if its key is a position that I do not know? I initially do not know if something is contained in every discrete point in space. I can’t go through the keys, adding 0.00000001 each time to find out if this key is correct and it will give me an object or nothing exists here. Maybe the key should be something else?

    Option 2: divide the space into cells measuring 10x10 meters. Each cell contains a list of all objects in a given area. This will be implemented as Dictionary <float, LinkedList <GameObject>>. Float - cell position, LinkedList - a list of all objects inside the cell space.

    For the appearance of objects, you just need to get the keys of the unloaded cells next to the player and spawn all the objects from the lists of received cells. To remove objects, you need to measure the distance from the player to each cell. The problem is that although the appearance of objects is easy to realize, their destruction causes difficulties. It is necessary to determine which objects are inside the cell being cleaned, to save the positions of the objects and destroy them. How to do it? I don’t want to use triggers, it's a stupid way.

    Option 3: your option.

    What can you advise?