Search Unity

Allow players to create their custom maps and storing them

Discussion in 'Game Design' started by DragonIJK, May 30, 2023.

  1. DragonIJK

    DragonIJK

    Joined:
    May 30, 2023
    Posts:
    2
    Hello, Im planning of making a game where It allows players to create their own custom maps, but I also need to give them permission to publish their work on the game's menu so everyone can play on their maps.
    After searching I found that to save player's work, I have to save it as XML. Also, to give them permission to publish their maps, I have to store their work as XML in a database, but I found that I need NoSQL database (MongoDB for instance) to save and load XML data on game's menu.

    What type of tools do I need to use in this case? Is there any other efficient way to do it ? Thanks in advance.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Uhm ... no.

    You can save it in any format you like. Json is far simpler and it's built-in (JsonUtility). And of course there is binary serialization, for example with Unity Serialization. In any case, you'll want to compress the resulting file (or byte array) since sharing means up- and downloading these files thus they should be as small as possible.

    You can also use any database you want, transactional would also work as would a list of files and a file mapping in a database. In fact, you want to push that decision (which database to use) as far back as possible because you can easily abstract that interface (save to / load from a database) and make it work with local files first. Then maybe add SQLite support, then Firebase, then MySQL. At the end you can easily test various databases and then decide somewhere near the end of the project.

    PS: You probably didn't mean permission (granting them the right to do so) but ability (enabling them to do so).
     
  3. DragonIJK

    DragonIJK

    Joined:
    May 30, 2023
    Posts:
    2
    Hi, thanks for your answer.
    Can you elaborate the idea of compressing file and how to store the compressed file in a database ?
    for example how can I compress XML file or JSON file and store it in a db ?
    Thank you.

    Yes, I meant to say to give them ability to share their maps with others.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    I use these compression/decompression extension methods for byte[]:

    Code (CSharp):
    1.     public static class ByteArrayExt
    2.     {
    3.         /// <summary>
    4.         ///     Compresses a byte array and returns the GZip compressed buffer.
    5.         /// </summary>
    6.         /// <param name="uncompressedBuffer"></param>
    7.         /// <returns></returns>
    8.         public static Byte[] Compress(this Byte[] uncompressedBuffer)
    9.         {
    10.             using (var memoryStream = new MemoryStream())
    11.             using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress))
    12.             {
    13.                 zipStream.Write(uncompressedBuffer, 0, uncompressedBuffer.Length);
    14.                 zipStream.Close();
    15.                 return memoryStream.ToArray();
    16.             }
    17.         }
    18.  
    19.         /// <summary>
    20.         ///     Decompresses a GZip byte array and returns the uncompressed buffer.
    21.         /// </summary>
    22.         /// <param name="compressedBuffer"></param>
    23.         /// <returns></returns>
    24.         public static Byte[] Decompress(this Byte[] compressedBuffer)
    25.         {
    26.             using (var compressedStream = new MemoryStream(compressedBuffer))
    27.             using (var unzipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    28.             using (var uncompressedStream = new MemoryStream())
    29.             {
    30.                 unzipStream.CopyTo(uncompressedStream);
    31.                 return uncompressedStream.ToArray();
    32.             }
    33.         }
    34.     }
    call it like
    Code (CSharp):
    1. var compressedBytes = someByteArray.Compress();
     
    SeerSucker69 likes this.